优化更新时间间隔

This commit is contained in:
Jerry Yan 2019-11-25 09:34:06 +08:00
parent 9b2f1a744f
commit 9fb552e6dc
2 changed files with 17 additions and 23 deletions

22
api.py
View File

@ -9,6 +9,7 @@ from Struct.Chat import Chat
from Struct.Lottery import Lottery
import requests
import time
from datetime import datetime, timedelta
DEBUG = False
@ -24,7 +25,6 @@ class XiGuaLiveApi:
roomLiver = None
roomPopularity = 0
_cursor = "0"
_updRoomCount = 0
lottery = None
s = requests.session()
@ -35,7 +35,8 @@ class XiGuaLiveApi:
:param name: 主播名
"""
self.name = name
self.updRoomInfo()
self._updRoomAt = datetime.now()
self.updRoomInfo(True)
def _updateRoomPopularity(self, _data):
"""
@ -215,11 +216,14 @@ class XiGuaLiveApi:
self.lottery = l
return True
def updRoomInfo(self):
def updRoomInfo(self, force=False):
"""
更新房间信息
:return:
"""
if not force and self._updRoomAt > (datetime.now() - timedelta(minutes=2)):
return self.isLive
self._updRoomAt = datetime.now()
if self.isLive:
return self._updateRoomOnly()
else:
@ -304,14 +308,13 @@ class XiGuaLiveApi:
elif i["common"]['method'] == "VideoLiveControlMessage":
print("消息:", "主播离开一小会")
# 这个消息代表主播下播了,直接更新房间信息
self.updRoomInfo()
self.updRoomInfo(True)
elif i["common"]['method'] == "VideoLiveDiggMessage":
self.onLike(User(i))
else:
pass
if self.lottery is None or self.lottery.ID == 0:
self.lottery = Lottery(i)
self._updRoomCount += 1
# 更新抽奖信息
if self.lottery is not None and self.lottery.ID != 0:
self.lottery.update()
@ -319,10 +322,7 @@ class XiGuaLiveApi:
self.onLottery(self.lottery)
self.lottery = None
# 2分钟自动更新下房间信息
if self._updRoomCount > 120 or len(d['data']) == 0:
self.updRoomInfo()
self._updRoomCount = 0
return
self.updRoomInfo(len(d['data']) == 0)
if __name__ == "__main__":
@ -347,8 +347,6 @@ if __name__ == "__main__":
time.sleep(5)
except Exception as e:
print(e)
time.sleep(1)
else:
print("主播未开播等待1分钟后重试")
time.sleep(60)
print("主播未开播等待2分钟后重试")
api.updRoomInfo()

View File

@ -113,7 +113,6 @@ def run():
if not Common.api.isValidRoom:
Common.appendError("[{}]房间未找到".format(Common.config["l_u"]))
return
_count = 0
while True:
if Common.api.isLive and not Common.forceNotBroadcasting:
if not Common.forceNotDownload:
@ -122,15 +121,12 @@ def run():
awakeUpload()
if not Common.forceNotEncode:
awakeEncode()
if _count % 15 == 14:
try:
Common.api.updRoomInfo()
_count = 0
_count_error = 0
except Exception as e:
Common.appendError(e.__str__())
time.sleep(5)
continue
try:
Common.api.updRoomInfo()
except Exception as e:
Common.appendError(e.__str__())
time.sleep(5)
continue
time.sleep(5)
else:
try:
@ -148,4 +144,4 @@ def run():
Common.forceStartUploadThread = False
if Common.doDelay():
Common.uploadQueue.put(True)
time.sleep(60)
time.sleep(15)