From 372953b3e07603629ccb07531095f4a9409d4022 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Tue, 31 Dec 2019 14:10:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=83=A8=E5=88=86=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=EF=BC=8C=E5=BE=85=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common.py | 4 ++-- Struct/Gift.py | 21 ++++++++++++++------- api.py | 50 ++++++++++++++++++++++++++++++++------------------ 3 files changed, 48 insertions(+), 27 deletions(-) diff --git a/Common.py b/Common.py index bca4c40..d2b448e 100644 --- a/Common.py +++ b/Common.py @@ -317,9 +317,9 @@ class downloader(XiGuaLiveApi): doClean() super(downloader, self).updRoomInfo(force) - def _updateRoomOnly(self): + def _updateUserOnly(self): global broadcaster, isBroadcasting, updateTime - super(downloader, self)._updateRoomOnly() + super(downloader, self)._updateUserOnly() updateTime = datetime.strftime(datetime.now(), dt_format) broadcaster = self.roomLiver isBroadcasting = self.isLive diff --git a/Struct/Gift.py b/Struct/Gift.py index 64d11bb..e07e290 100644 --- a/Struct/Gift.py +++ b/Struct/Gift.py @@ -3,14 +3,14 @@ from .User import User class Gift: - ID = 0 - count = 0 roomID = 0 giftList = {} - amount = 0 - user = None def __init__(self, json=None): + self.ID = 0 + self.count = 0 + self.amount = 0 + self.user = None if json: self.parse(json) @@ -34,7 +34,7 @@ class Gift: def update(self): p = requests.get("https://i.snssdk.com/videolive/gift/get_gift_list?room_id={roomID}" - "&version_code=730&device_platform=android".format(roomID = self.roomID)) + "&version_code=800&device_platform=android".format(roomID=self.roomID)) d = p.json() if "gift_info" not in d: print("错误:礼物更新失败") @@ -48,7 +48,14 @@ class Gift: giftN = self.giftList[self.ID]["Name"] else: giftN = "未知礼物[{}]".format(self.ID) - return "{user} 送出的 {count} 个 {name}".format(user= self.user, count= self.count, name= giftN) + return "{user} 送出的 {count} 个 {name}".format(user=self.user, count=self.count, name=giftN) def __unicode__(self): - return self.__str__() \ No newline at end of file + return self.__str__() + + def __repr__(self): + if self.ID in self.giftList: + giftN = self.giftList[self.ID]["Name"] + else: + giftN = "未知礼物" + return "西瓜礼物【{}(ID:{})】".format(giftN, self.ID) diff --git a/api.py b/api.py index 67674ea..ccf1064 100644 --- a/api.py +++ b/api.py @@ -45,13 +45,17 @@ class XiGuaLiveApi: lottery = None s = requests.session() - def __init__(self, name: str = "永恒de草薙"): + def __init__(self, name=None): """ Api类 Init Function - :param name: 主播名 + :param name: class:str|User: 主播名 """ - self.name = name + if type(name) == User: + self.roomLiver = name + self.name = name.name + else: + self.name = str(name) self.s.headers.update(COMMON_HEADERS) self._updRoomAt = datetime.now() self.updRoomInfo(True) @@ -188,9 +192,9 @@ class XiGuaLiveApi: if len(_results) > 0: self.isValidRoom = True self.roomLiver = _results[0] - return self._updateRoomOnly() + return self._updateUserOnly() - def _updateRoomOnly(self): + def _updateUserOnly(self): """ 获取用户信息 :return: @@ -199,7 +203,11 @@ class XiGuaLiveApi: return False _formatData = {"COMMON": COMMON_GET_PARAM, "TIMESTAMP": time.time() * 1000, "userId": self.roomLiver.ID} _url = USER_INFO_API.format_map(_formatData).format_map(_formatData) - p = self.s.get(_url) + try: + p = self.s.get(_url) + except Exception as e: + self.apiChangedError("更新用户信息接口请求失败", e.__str__()) + return False try: d = p.json() except Exception as e: @@ -216,6 +224,7 @@ class XiGuaLiveApi: self.isLive = d["user_info"]["is_living"] self._rawRoomInfo = d["user_info"]['live_info'] if self.isLive: + self.roomID = d["user_info"]['live_info']['room_id'] # 处理抽奖事件 l = Lottery(self._rawRoomInfo) if l.isActive: @@ -232,25 +241,30 @@ class XiGuaLiveApi: return self.isLive self._updRoomAt = datetime.now() if self.isLive: - return self._updateRoomOnly() + return self._updateUserOnly() else: return self._forceSearchUser() @staticmethod - def findRoomByUserId(userId: int): + def getUserInfoByUserId(userId): """ - 通过UserId查找用户的房间号(已弃用) + 通过UserId查找用户的房间号 :param userId: 用户ID :return: XiGuaLiveApi """ - p = requests.get("https://live.ixigua.com/api/room?anchorId={room}".format(room=userId)) - if DEBUG: - print(p.text) - d = p.json() - if "data" not in d or "title" not in d["data"] or "id" not in d["data"]: - XiGuaLiveApi.apiChangedError("网页接口已更改,除非你是开发者,请不要用这个方法", d) - return XiGuaLiveApi() - return XiGuaLiveApi(d["data"]["id"]) + _formatData = {"COMMON": COMMON_GET_PARAM, "TIMESTAMP": time.time() * 1000, "userId": userId} + _url = USER_INFO_API.format_map(_formatData).format_map(_formatData) + try: + p = requests.get(_url, headers=COMMON_HEADERS) + except Exception as e: + XiGuaLiveApi.apiChangedError("更新用户信息接口请求失败", e.__str__()) + return None + try: + d = p.json() + except Exception as e: + XiGuaLiveApi.apiChangedError("更新房间接口错误", e.__str__()) + return None + return XiGuaLiveApi(User(d)) @staticmethod def searchUser(keyword): @@ -286,7 +300,7 @@ class XiGuaLiveApi: self.updRoomInfo() return p = self.s.get("https://i.snssdk.com/videolive/im/get_msg?cursor={cursor}&room_id={roomID}" - "&version_code=730&device_platform=android".format( + "&version_code=800&device_platform=android".format( roomID=self.roomID, cursor=self._cursor ))