From 3c872b04844ad944814eeba31d2c324d0f917d89 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Sun, 31 Mar 2019 08:06:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DApi=E6=9B=B4=E6=94=B9?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E7=9A=84=E6=9C=AA=E5=BC=80=E6=92=AD=E6=97=B6?= =?UTF-8?q?=E6=8A=A5=E9=94=99=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api.py | 36 ++++++++++++++++++++++++++++-------- liveDownloader.py | 14 ++++++++++---- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/api.py b/api.py index 66908b5..98261c1 100644 --- a/api.py +++ b/api.py @@ -53,7 +53,8 @@ class XiGuaLiveApi: :param args: """ print(msg) - print(*args) + if DEBUG: + print(*args) def onPresent(self, gift: Gift): """ @@ -143,13 +144,22 @@ class XiGuaLiveApi: "&device_platform=android".format(roomID=self.roomID), headers={"Content-Type":"application/x-www-form-urlencoded"}) d = p.json() - except: + except Exception as e: + if DEBUG: + print("ReqError@UpdRoomInfo") + print(e.__str__()) + if p: + print(p.status_code) + print(p.text) return False self.isValidRoom = d["base_resp"]["status_code"] == 0 if d["base_resp"]["status_code"] != 0: + if DEBUG: + print("CodeIsnot0@UpdRoomInfo") + print(d) return False if "room" not in d and d["room"] is None: - self.apiChangedError("Api发生改变,请及时联系我") + self.apiChangedError("Api发生改变,请及时联系我", d) return False self._rawRoomInfo = d["room"] self.isLive = d["room"]["status"] == 2 @@ -161,12 +171,16 @@ class XiGuaLiveApi: 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)) try: + 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() except json.decoder.JSONDecodeError as e: - print(p.text) + if DEBUG: + print(e.__str__()) + if p: + print(p.status_code) + print(p.text) return if "data" in d and d["data"] is not None: for i in d["data"]: @@ -175,8 +189,14 @@ class XiGuaLiveApi: if len(i["cells"]) == 0: return self.isValidRoom = True - self.isLive = i["cells"][0]["anchor"]["user_info"]["is_living"] - self.roomID = int(i["cells"][0]["anchor"]["room_id"]) + if "is_living" in i["cells"][0]["anchor"]["user_info"]: + self.isLive = i["cells"][0]["anchor"]["user_info"]["is_living"] + else: + self.isLive = False + if "room_id" in i["cells"][0]["anchor"]: + self.roomID = int(i["cells"][0]["anchor"]["room_id"]) + else: + self.isLive = False self.roomLiver = User(i["cells"][0]) if self.isLive: return self.updRoomInfo() diff --git a/liveDownloader.py b/liveDownloader.py index 47e8c2e..aa5972f 100644 --- a/liveDownloader.py +++ b/liveDownloader.py @@ -156,18 +156,18 @@ if __name__ == "__main__": et.setDaemon(True) et.start() _count = 0 + _count_error = 0 while True: if api.isLive: if d is None: d = datetime.strftime(datetime.now(), "%Y_%m_%d") if not t.is_alive(): - _count = 0 + _count_error += 1 _preT = api.playlist t = threading.Thread(target=download, args=(_preT,)) t.setDaemon(True) t.start() if not ut.is_alive(): - _count = 0 ut = threading.Thread(target=upload, args=(d,)) ut.setDaemon(True) ut.start() @@ -179,11 +179,14 @@ if __name__ == "__main__": try: api.updRoomInfo() _count = 0 + _count_error = 0 except Exception as e: print(e.__str__()) time.sleep(10) - _count += 1 + _count_error += 1 continue + if _count_error > 15: + api.isLive = False _count += 1 time.sleep(20) else: @@ -196,4 +199,7 @@ if __name__ == "__main__": from config import config # print("主播未开播,等待1分钟后重试") time.sleep(60) - api.updRoomInfo() + try: + api.updRoomInfo() + except Exception as e: + print(e.__str__())