修复Api更改导致的未开播时报错问题

This commit is contained in:
Jerry Yan 2019-03-31 08:06:29 +08:00 committed by JerryYan
parent 5f949ccf34
commit 3c872b0484
2 changed files with 38 additions and 12 deletions

26
api.py
View File

@ -53,6 +53,7 @@ class XiGuaLiveApi:
:param args: :param args:
""" """
print(msg) print(msg)
if DEBUG:
print(*args) print(*args)
def onPresent(self, gift: Gift): def onPresent(self, gift: Gift):
@ -143,13 +144,22 @@ class XiGuaLiveApi:
"&device_platform=android".format(roomID=self.roomID), "&device_platform=android".format(roomID=self.roomID),
headers={"Content-Type":"application/x-www-form-urlencoded"}) headers={"Content-Type":"application/x-www-form-urlencoded"})
d = p.json() 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 return False
self.isValidRoom = d["base_resp"]["status_code"] == 0 self.isValidRoom = d["base_resp"]["status_code"] == 0
if d["base_resp"]["status_code"] != 0: if d["base_resp"]["status_code"] != 0:
if DEBUG:
print("CodeIsnot0@UpdRoomInfo")
print(d)
return False return False
if "room" not in d and d["room"] is None: if "room" not in d and d["room"] is None:
self.apiChangedError("Api发生改变请及时联系我") self.apiChangedError("Api发生改变请及时联系我", d)
return False return False
self._rawRoomInfo = d["room"] self._rawRoomInfo = d["room"]
self.isLive = d["room"]["status"] == 2 self.isLive = d["room"]["status"] == 2
@ -161,11 +171,15 @@ class XiGuaLiveApi:
self.lottery = l self.lottery = l
return True return True
else: else:
try:
p = s.get("https://security.snssdk.com/video/app/search/live/?version_code=730&device_platform=android" p = s.get("https://security.snssdk.com/video/app/search/live/?version_code=730&device_platform=android"
"&format=json&keyword={}".format(self.name)) "&format=json&keyword={}".format(self.name))
try:
d = p.json() d = p.json()
except json.decoder.JSONDecodeError as e: except json.decoder.JSONDecodeError as e:
if DEBUG:
print(e.__str__())
if p:
print(p.status_code)
print(p.text) print(p.text)
return return
if "data" in d and d["data"] is not None: if "data" in d and d["data"] is not None:
@ -175,8 +189,14 @@ class XiGuaLiveApi:
if len(i["cells"]) == 0: if len(i["cells"]) == 0:
return return
self.isValidRoom = True self.isValidRoom = True
if "is_living" in i["cells"][0]["anchor"]["user_info"]:
self.isLive = i["cells"][0]["anchor"]["user_info"]["is_living"] 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"]) self.roomID = int(i["cells"][0]["anchor"]["room_id"])
else:
self.isLive = False
self.roomLiver = User(i["cells"][0]) self.roomLiver = User(i["cells"][0])
if self.isLive: if self.isLive:
return self.updRoomInfo() return self.updRoomInfo()

View File

@ -156,18 +156,18 @@ if __name__ == "__main__":
et.setDaemon(True) et.setDaemon(True)
et.start() et.start()
_count = 0 _count = 0
_count_error = 0
while True: while True:
if api.isLive: if api.isLive:
if d is None: if d is None:
d = datetime.strftime(datetime.now(), "%Y_%m_%d") d = datetime.strftime(datetime.now(), "%Y_%m_%d")
if not t.is_alive(): if not t.is_alive():
_count = 0 _count_error += 1
_preT = api.playlist _preT = api.playlist
t = threading.Thread(target=download, args=(_preT,)) t = threading.Thread(target=download, args=(_preT,))
t.setDaemon(True) t.setDaemon(True)
t.start() t.start()
if not ut.is_alive(): if not ut.is_alive():
_count = 0
ut = threading.Thread(target=upload, args=(d,)) ut = threading.Thread(target=upload, args=(d,))
ut.setDaemon(True) ut.setDaemon(True)
ut.start() ut.start()
@ -179,11 +179,14 @@ if __name__ == "__main__":
try: try:
api.updRoomInfo() api.updRoomInfo()
_count = 0 _count = 0
_count_error = 0
except Exception as e: except Exception as e:
print(e.__str__()) print(e.__str__())
time.sleep(10) time.sleep(10)
_count += 1 _count_error += 1
continue continue
if _count_error > 15:
api.isLive = False
_count += 1 _count += 1
time.sleep(20) time.sleep(20)
else: else:
@ -196,4 +199,7 @@ if __name__ == "__main__":
from config import config from config import config
# print("主播未开播等待1分钟后重试") # print("主播未开播等待1分钟后重试")
time.sleep(60) time.sleep(60)
try:
api.updRoomInfo() api.updRoomInfo()
except Exception as e:
print(e.__str__())