From 4270612a98ffbc608038a19e8a788450daf367ac Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Tue, 22 Jan 2019 21:37:44 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=A4=A7=E9=83=A8=E5=88=86?= =?UTF-8?q?=E5=88=A4=E6=96=AD=EF=BC=8CWinMain=E5=8A=A0=E5=85=A5=E5=85=A8?= =?UTF-8?q?=E9=83=A8=E6=98=BE=E7=A4=BA=E5=8F=82=E6=95=B0=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WinMain.py | 34 ++++++++++++++++++++++++++-------- api.py | 36 ++++++++++++++++-------------------- 2 files changed, 42 insertions(+), 28 deletions(-) diff --git a/WinMain.py b/WinMain.py index 351fb07..aa29725 100644 --- a/WinMain.py +++ b/WinMain.py @@ -11,6 +11,7 @@ from api import XiGuaLiveApi as Api import msvcrt import ctypes +SHOW_ALL = False def readInput(caption, default, timeout: int = 5): start_time = time.time() @@ -126,19 +127,26 @@ class WinMain(Api): resetColor() def onSubscribe(self, user: User): - return + if SHOW_ALL: + set_cmd_text_color(FOREGROUND_DARKGRAY) + print("用户", user, "关注了主播") + resetColor() def onEnter(self, msg:MemberMsg): - if msg.type > 1: + if SHOW_ALL: set_cmd_text_color(FOREGROUND_DARKGRAY) - print("提示 : ", msg) + print("提示 :", msg) resetColor() def onChat(self, chat: Chat): - print(chat) + if SHOW_ALL: + print(chat) def onPresent(self, gift: Gift): - return + if SHOW_ALL: + set_cmd_text_color(FOREGROUND_DARKGRAY) + print("连击 :", gift) + resetColor() def onPresentEnd(self, gift: Gift): set_cmd_text_color(BACKGROUND_WHITE | FOREGROUND_BLACK) @@ -146,7 +154,10 @@ class WinMain(Api): resetColor() def onLike(self, user: User): - return + if SHOW_ALL: + set_cmd_text_color(FOREGROUND_DARKGRAY) + print("用户", user, "点了喜欢") + resetColor() def onLeave(self, json: any): return @@ -163,10 +174,15 @@ if __name__ == "__main__": resetColor() print("西瓜直播弹幕助手 by JerryYan") if len(sys.argv) > 1: - room = int(sys.argv[1]) + if sys.argv[-1] == "a": + SHOW_ALL = True + try: + room = int(sys.argv[1]) + except: + pass else: try: - room = int(readInput("请输入房间号,默认为永恒的直播间", room, 3)) + room = int(readInput("请输入用户ID号,默认为永恒的ID号", room, 3)) except ValueError: pass api = WinMain(room) @@ -185,6 +201,8 @@ if __name__ == "__main__": warning(e) time.sleep(1) else: + set_cmd_text_color(FOREGROUND_RED) print("主播未开播,等待1分钟后重试") + resetColor() time.sleep(60) api.updRoomInfo() diff --git a/api.py b/api.py index f5d4dc9..5c9407a 100644 --- a/api.py +++ b/api.py @@ -28,15 +28,16 @@ class XiGuaLiveApi: Gift.update(self.roomID) self._enterRoom() - def notLiveError(self): - print("主播未开播") - def _updateRoomInfo(self, json): if "Msg" in json: if "member_count" in json["Msg"]: self.roomMember = json["Msg"]["member_count"] if "popularity" in json["Msg"]: self.roomPopularity = json["Msg"]["popularity"] + elif "data" in json: + if "popularity" in json["data"]: + self.roomPopularity = json["data"]["popularity"] + def apiChangedError(self, msg: str, *args): print(msg) @@ -85,22 +86,19 @@ class XiGuaLiveApi: if DEBUG: print(p.text) d = p.json() - if "data" not in d: + if "data" not in d or "title" not in d["data"] or "id" not in d["data"]: self.apiChangedError("无法获取RoomID,请与我联系") return self.isValidRoom = True self._rawRoomInfo = d["data"] self.roomLiver = User(d) - self.roomTitle = self._rawRoomInfo["title"] - if "id" in d["data"]: - self.roomID = d["data"]["id"] - else: - self.apiChangedError("无法获取RoomID,请与我联系") - if "FinishTime" in d["data"]: - self.isLive = False - self.notLiveError() - else: + self.roomTitle = d["data"]["title"] + self.roomID = d["data"]["id"] + self._updateRoomInfo(d) + if "status" in d["data"] and d["data"]["status"] == 2: self.isLive = True + else: + self.isLive = False def getDanmaku(self): if not self.isValidRoom: @@ -111,17 +109,15 @@ class XiGuaLiveApi: cursor=self._cursor )) d = p.json() - if "data" not in d: - self.apiChangedError("数据结构改变,请与我联系", d) - return - if "Extra" not in d["data"]: - self.apiChangedError("数据结构改变,请与我联系", d) - return - if "Cursor" not in d["data"]["Extra"]: + if "data" not in d or "Extra" not in d["data"] or "Cursor" not in d["data"]["Extra"]: + if DEBUG: + print(d) self.apiChangedError("数据结构改变,请与我联系", d) return else: self._cursor = d["data"]["Extra"]["Cursor"] + if DEBUG: + print("Cursor",self._cursor) if "LiveMsgs" not in d["data"]: return for i in d['data']['LiveMsgs']: