diff --git a/Struct/Gift.py b/Struct/Gift.py index 622cd72..21cc0d0 100644 --- a/Struct/Gift.py +++ b/Struct/Gift.py @@ -25,7 +25,7 @@ class Gift: _message.ParseFromString(raw) self.user = User(_message.user) self.ID = _message.giftId - self.count = _message.combo + self.count = _message.repeated self.isFinished = _message.isFinished self.backupName = _message.commonInfo.displayText.params.gifts.gift.name @@ -43,12 +43,12 @@ class Gift: self.ID = int(json["extra"]['present_end_info']['id']) self.count = json["extra"]['present_end_info']['count'] if self.ID != 0 and self.ID in self.giftList: - self.amount = self.giftList[self.ID]["Price"] * self.count + self.amount = self.giftList[self.ID]['diamond_count'] * self.count else: self.update() - @staticmethod - def update(): + @classmethod + def update(cls): p = requests.get("https://i.snssdk.com/videolive/gift/get_gift_list?room_id={roomID}" "&version_code=800&device_platform=android".format(roomID=Gift.roomID)) d = p.json() @@ -56,15 +56,21 @@ class Gift: print("错误:礼物更新失败") else: for i in d["gift_info"]: - _id = int(i["id"]) - Gift.giftList[_id] = {"Name": i["name"], "Price": i["diamond_count"], "Type": i["type"]} + cls.addGift(i) def isAnimate(self): - return self.ID != 0 and self.ID in self.giftList and self.giftList[self.ID]["Type"] == 2 + if self.ID != 0 and self.ID in self.giftList: + if 'combo' in self.giftList[self.ID]: + return self.giftList[self.ID]["combo"] == False + elif 'meta' in self.giftList[self.ID] and 'combo' in self.giftList[self.ID]['meta']: + return self.giftList[self.ID]['meta']["combo"] == False + elif 'type' in self.giftList[self.ID]: + return self.giftList[self.ID]["type"] == 2 + return False def _getGiftName(self): if self.ID in self.giftList: - return self.giftList[self.ID]["Name"] + return self.giftList[self.ID]["name"] elif self.backupName is not None: return self.backupName else: @@ -78,3 +84,10 @@ class Gift: def __repr__(self): return "西瓜礼物【{}(ID:{})】".format(self._getGiftName(), self.ID) + + @classmethod + def addGift(cls, _gift): + if 'id' not in _gift: + return + _id = int(_gift["id"]) + cls.giftList[_id] = _gift diff --git a/WinMain.py b/WinMain.py index a71cfa6..c3f998e 100644 --- a/WinMain.py +++ b/WinMain.py @@ -203,6 +203,12 @@ if __name__ == "__main__": print("OK") else: print("FAIL") + print("更新房间礼物信息", end="\t", flush=True) + __res = api.updGiftInfo() + if __res < 0: + print("FAIL") + else: + print('OK\n礼物种数:', __res) print("=" * 30) while True: if api.isLive: diff --git a/api.py b/api.py index 284538a..7f23819 100644 --- a/api.py +++ b/api.py @@ -32,6 +32,8 @@ ROOM_INFO_API = ("https://webcast3.ixigua.com/webcast/room/enter/?room_id={roomI "&webcast_language=zh&webcast_locale=zh_CN&pack_level=4{COMMON}") DANMAKU_GET_API = ("https://webcast3.ixigua.com/webcast/room/{roomId}/_fetch_message_polling/?webcast_sdk_version=1350" "&webcast_language=zh&webcast_locale=zh_CN{COMMON}") +GIFT_DATA_API = ("https://webcast.ixigua.com/webcast/gift/list/?room_id={roomId}&fetch_giftlist_from=2" + "&webcast_sdk_version=1350&webcast_language=zh&webcast_locale=zh_CN{COMMON}") COMMON_HEADERS = { "sdk-version": '1', "User-Agent": "Dalvik/2.1.0 (Linux; U; Android 9) VideoArticle/8.1.6 cronet/TTNetVersion:b97574c0 2019-09-24", @@ -319,7 +321,6 @@ class XiGuaLiveApi: self._updRoomAt = datetime.now() self._updateRoomPopularity(d) Gift.roomID = self.roomID - Gift.update() return self.isLive def updRoomInfo(self, force=False): @@ -334,6 +335,23 @@ class XiGuaLiveApi: else: return self._getRoomInfo(force) + def updGiftInfo(self): + self.updRoomInfo() + _formatData = {"COMMON": COMMON_GET_PARAM, "TIMESTAMP": time.time() * 1000, "roomId": self.roomID} + _url = GIFT_DATA_API.format_map(_formatData).format_map(_formatData) + d = self.getJson(_url) + Gift.roomID = self.roomID + if d is None or d["status_code"] != 0: + Gift.update() + elif 'pages' not in d["data"]: + Gift.update() + else: + for _page in d["data"]['pages']: + if 'gifts' in _page: + for _gift in _page['gifts']: + Gift.addGift(_gift) + return len(Gift.giftList) + def getDanmaku(self): """ 获取弹幕 @@ -355,7 +373,7 @@ class XiGuaLiveApi: self.onPresentEnd(_gift) elif _each.method == "WebcastChatMessage": _chat = Chat(_each.raw) - self.onChat(_chat) + # self.onChat(_chat) elif _each.method == "WebcastFansclubMessage": _fansClubMessage = FansClubMessage() _fansClubMessage.ParseFromString(_each.raw) @@ -392,6 +410,12 @@ if __name__ == "__main__": print("OK") else: print("FAIL") + print("更新房间礼物信息", end="\t", flush=True) + __res = api.updGiftInfo() + if __res < 0: + print("FAIL") + else: + print('OK\n礼物种数:', __res) print("=" * 30) while True: if api.isLive: