This repository has been archived on 2022-05-30. You can view files and clone it, but cannot push or open issues or pull requests.
XiguaLiveDanmakuHelper/GiftStruct.py
2019-01-17 11:42:04 +08:00

43 lines
1.4 KiB
Python

import requests
class GiftStruct:
ID = 0
count = 0
giftList={10001: {"Name": "西瓜", "Price": 0}}
amount = 0
def __init__(self, json=None):
if json:
self.parse(json)
def parse(self, json):
if "Msg" in json:
if "present_end_info" in json["Msg"]:
self.ID = json["Msg"]['present_end_info']['id']
self.count = json["Msg"]['present_end_info']['count']
elif "present_info" in json["Msg"]:
self.ID = json["Msg"]['present_info']['id']
self.count = json["Msg"]['present_info']['repeat_count']
if self.ID in self.giftList:
self.amount = self.giftList[self.ID]["Price"] * self.count
@staticmethod
def update(roomID):
p = requests.get("https://live.ixigua.com/api/gifts/{roomID}".format(roomID= roomID))
d = p.json()
if isinstance(d, int) or "data" not in d:
print("错误:礼物更新失败")
else:
for i in d["data"]:
GiftStruct.giftList[i["ID"]] = {"Name": i["Name"], "Price": i["DiamondCount"]}
def __str__(self):
if self.ID in self.giftList:
giftN = self.giftList[self.ID]["Name"]
else:
giftN = "未知礼物[{}]".format(self.ID)
return "{count}{name}".format(count= self.count, name= giftN)
def __unicode__(self):
return self.__str__()