From 81f730ea689148a7662429521e54c52406a77eee Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Thu, 31 Jan 2019 23:05:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=84=E7=A7=8D=E9=80=82=E9=85=8D=E6=96=B0An?= =?UTF-8?q?droid=20Api=EF=BC=8C=E4=BF=AE=E5=A4=8D=E5=90=84=E7=A7=8DBug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gift.py | 14 +++++--- Lottery.py | 7 ++-- User.py | 8 +++-- WinMain.py | 29 +++++++--------- api.py | 85 +++++++++++++++++++++++++++-------------------- liveDownloader.py | 9 ++--- 6 files changed, 83 insertions(+), 69 deletions(-) diff --git a/Gift.py b/Gift.py index 942c1cd..09888aa 100644 --- a/Gift.py +++ b/Gift.py @@ -16,7 +16,11 @@ class Gift: def parse(self, json): self.user = User(json) - if "extra" in json: + if "common" in json and json["common"] is not None: + if Gift.roomID != int(json["common"]["room_id"]): + Gift.roomID = int(json["common"]["room_id"]) + self.update() + if "extra" in json and json["extra"] is not None: if "present_info" in json["extra"] and json["extra"]['present_info'] is not None: self.ID = int(json["extra"]['present_info']['id']) self.count = json["extra"]['present_info']['repeat_count'] @@ -25,11 +29,11 @@ class Gift: self.count = json["extra"]['present_end_info']['count'] if self.ID in self.giftList: self.amount = self.giftList[self.ID]["Price"] * self.count + else: + self.update() - @staticmethod - def update(roomID): - Gift.roomID = roomID - p = requests.get("https://i.snssdk.com/videolive/gift/get_gift_list?room_id={roomID}".format(roomID= roomID)) + def update(self): + p = requests.get("https://i.snssdk.com/videolive/gift/get_gift_list?room_id={roomID}".format(roomID = self.roomID)) d = p.json() if "gift_info" not in d: print("错误:礼物更新失败") diff --git a/Lottery.py b/Lottery.py index fb89f08..5e4a430 100644 --- a/Lottery.py +++ b/Lottery.py @@ -48,10 +48,11 @@ class Lottery: if self.isFinished: ret = "恭喜以下中奖用户:\n" for i in self.luckyUsers: - ret += "\t{} {}\n".format(i,self.prizeName) - ret += "\t参与人数:{}".format(self.joinedUserCount) + ret += "> {} {}\n".format(i,self.prizeName) + ret += "> 参与人数:{}".format(self.joinedUserCount) return ret elif self.isActive: - return "正在抽奖中。。。参与人数:{}".format(self.joinedUserCount) + return "正在抽奖中。。。\n" \ + "> 参与人数:{}".format(self.joinedUserCount) else: return "抽奖已失效" diff --git a/User.py b/User.py index 97d9c58..f2253f3 100644 --- a/User.py +++ b/User.py @@ -23,10 +23,14 @@ class User: self.type = json["extra"]["user_room_auth_status"]["user_type"] self.block = json["extra"]["user_room_auth_status"]["is_block"] self.mute = json["extra"]["user_room_auth_status"]["is_silence"] - elif "room" in json: - if "user_info" in json["room"]: + elif "room" in json and json["room"] is not None: + if "user_info" in json["room"] and json["room"]["user_info"] is not None: self.ID = json["room"]['user_info']['user_id'] self.name = json["room"]['user_info']['name'] + elif "anchor" in json and json["anchor"] is not None: + if "user_info" in json["anchor"] and json["anchor"]['user_info'] is not None: + self.ID = json["anchor"]['user_info']['user_id'] + self.name = json["anchor"]['user_info']['name'] if self.type is None: self.type = 0 if isinstance(self.level, str): diff --git a/WinMain.py b/WinMain.py index 364e200..33eb671 100644 --- a/WinMain.py +++ b/WinMain.py @@ -31,7 +31,9 @@ def readInput(caption, default, timeout: int = 5): msvcrt.putch(b"\b") if len(input) == 0: start_time = time.time() - elif 32 <= ord(chr) <= 126: # space_char + elif 32 > ord(chr) or 255 > ord(chr) > 126: # space_char + continue + else: input += chr.decode("utf8") if len(input) == 0 and (time.time() - start_time) > timeout: break @@ -119,7 +121,7 @@ class WinMain(Api): def onJoin(self, user: User): set_cmd_text_color(BACKGROUND_WHITE | FOREGROUND_BLACK) - print("感谢", user, "加入了粉丝团") + print("欢迎", user, "加入了粉丝团") resetColor() def onSubscribe(self, user: User): @@ -171,24 +173,14 @@ def warning(*args): if __name__ == "__main__": - room = 97621754276 # 永恒 - # room = 75366565294 - # room = 83940182312 #Dae + name = "永恒de草薙" resetColor() print("西瓜直播弹幕助手 by JerryYan") if len(sys.argv) > 1: - if sys.argv[-1] == "a": - SHOW_ALL = True - try: - room = int(sys.argv[1]) - except: - pass + name = sys.argv[1] else: - try: - room = int(readInput("请输入房间号,默认为永恒的房间号", room, 3)) - except ValueError: - pass - api = WinMain.findRoomByUserId(room) + name = readInput("请输入主播用户名(请用拼音字母),默认为", name, 3) + api = WinMain(name) print("进入", api.roomLiver, "的直播间") if not api.isValidRoom: input("房间不存在") @@ -198,7 +190,10 @@ if __name__ == "__main__": while True: if api.isLive: os.system("title {}".format(api.getTitle())) - api.getDanmaku() + try: + api.getDanmaku() + except Exception as e: + print(e.__str__()) time.sleep(1) else: set_cmd_text_color(FOREGROUND_RED) diff --git a/api.py b/api.py index 5ab35b8..3515424 100644 --- a/api.py +++ b/api.py @@ -17,6 +17,7 @@ class XiGuaLiveApi: isLive: bool = False isValidRoom: bool = False _rawRoomInfo = {} + name: str = "" roomID: int = 0 roomTitle: str = "" roomLiver: User = None @@ -25,10 +26,9 @@ class XiGuaLiveApi: _updRoomCount:int = 0 lottery:Lottery = None - def __init__(self, roomId: int = 6651493149011086094): - self.roomID = roomId + def __init__(self, name: str = "永恒de草薙"): + self.name = name self.updRoomInfo() - Gift.update(self.roomID) def _updateRoomInfo(self, json): if "extra" in json: @@ -63,7 +63,7 @@ class XiGuaLiveApi: print("消息 :", user, "关注了主播") def onJoin(self, user: User): - print("感谢", user, "加入了粉丝团") + print("欢迎", user, "加入了粉丝团") def onMessage(self, msg: str): print("消息 :", msg) @@ -78,27 +78,45 @@ class XiGuaLiveApi: print("中奖消息 :", i) def updRoomInfo(self): - p = s.post("https://i.snssdk.com/videolive/room/enter?version_code=730" - "&device_platform=android", - data="room_id={roomID}&version_code=730" - "&device_platform=android".format(roomID=self.roomID), - headers={"Content-Type":"application/x-www-form-urlencoded"}) - d = p.json() - self.isValidRoom = d["base_resp"]["status_code"] == 0 - if d["base_resp"]["status_code"] != 0: - return False - if "room" not in d and d["room"] is None: - self.apiChangedError("Api发生改变,请及时联系我") - return False - self._rawRoomInfo = d["room"] - self.isLive = d["room"]["status"] == 2 - self.roomLiver = User(d) - self.roomTitle = d["room"]["title"] - self.roomPopularity = d["room"]["user_count"] - l = Lottery(d) - if l.isActive: - self.lottery = l - return True + if self.isLive: + p = s.post("https://i.snssdk.com/videolive/room/enter?version_code=730" + "&device_platform=android", + data="room_id={roomID}&version_code=730" + "&device_platform=android".format(roomID=self.roomID), + headers={"Content-Type":"application/x-www-form-urlencoded"}) + d = p.json() + self.isValidRoom = d["base_resp"]["status_code"] == 0 + if d["base_resp"]["status_code"] != 0: + return False + if "room" not in d and d["room"] is None: + self.apiChangedError("Api发生改变,请及时联系我") + return False + self._rawRoomInfo = d["room"] + self.isLive = d["room"]["status"] == 2 + self.roomLiver = User(d) + self.roomTitle = d["room"]["title"] + self.roomPopularity = d["room"]["user_count"] + l = Lottery(d) + if l.isActive: + self.lottery = l + return True + else: + p = s.get("https://security.snssdk.com/video/app/search/live/?version_code=730&device_platform=android" + "&format=json&keyword={}".format(self.name)) + d = p.json() + if "data" in d: + for i in d["data"]: + if i["block_type"] != 0: + continue + if len(i["cells"]) == 0: + return + self.isLive = i["cells"][0]["anchor"]["user_info"]["is_living"] + self.roomID = int(i["cells"][0]["anchor"]["room_id"]) + self.roomLiver = User(i["cells"][0]) + if self.isLive: + return self.updRoomInfo() + else: + return False @staticmethod def findRoomByUserId(userId:int): @@ -119,7 +137,7 @@ class XiGuaLiveApi: d = p.json() if "data" in d: for i in d["data"]: - if i["block_type"] != 2: + if i["block_type"] != 0: continue for _i in i["cells"]: ret.append(_i["room"]) @@ -170,7 +188,7 @@ class XiGuaLiveApi: else: pass self._updRoomCount += 1 - if self._updRoomCount > 30 or len(d['data']) == 0: + if self._updRoomCount > 120 or len(d['data']) == 0: if self.lottery is not None: self.lottery.checkFinished() if self.lottery.isFinished: @@ -182,18 +200,13 @@ class XiGuaLiveApi: if __name__ == "__main__": - room = 97621754276 # 永恒 - # room = 75366565294 - # room = 83940182312 #Dae - if len(sys.argv) > 1: + name = "永恒de草薙" + if len(sys.argv) > 2: if sys.argv[-1] == "d": DEBUG = True - try: - room = int(sys.argv[1]) - except ValueError: - pass + name = sys.argv[1] print("西瓜直播弹幕助手 by JerryYan") - api = XiGuaLiveApi.findRoomByUserId(room) + api = XiGuaLiveApi(name) if not api.isValidRoom: print(api.roomID) input("房间不存在") diff --git a/liveDownloader.py b/liveDownloader.py index 5653385..a5942cf 100644 --- a/liveDownloader.py +++ b/liveDownloader.py @@ -135,18 +135,15 @@ b = Bilibili() b.login(config["b_u"], config["b_p"]) if __name__ == "__main__": - room = 97621754276 # 永恒 + name = "永恒de草薙" # room = 75366565294 # room = 83940182312 #Dae # room = 5947850784 #⑦ # room = 58649240617 #戏 if len(sys.argv) > 1: - try: - room = int(sys.argv[1]) - except ValueError: - pass + name = sys.argv[1] print("西瓜直播录播助手 by JerryYan") - api = downloader.findRoomByUserId(room) + api = downloader(name) print("进入", api.roomLiver, "的直播间") if not api.isValidRoom: input("房间不存在")