代码优化(可能是负优化,待测试)
This commit is contained in:
parent
a3d9e17a71
commit
708218d34a
61
api.py
61
api.py
@ -29,28 +29,34 @@ class XiGuaLiveApi:
|
||||
lottery = None
|
||||
|
||||
def __init__(self, name: str = "永恒de草薙"):
|
||||
"""
|
||||
Api类
|
||||
Init Function
|
||||
:param name: 主播名
|
||||
"""
|
||||
self.name = name
|
||||
self.updRoomInfo()
|
||||
|
||||
def _updateRoomPopularity(self, json):
|
||||
def _updateRoomPopularity(self, _data):
|
||||
"""
|
||||
更新房间人气的方法
|
||||
Update Room Popularity
|
||||
:param json: Received Message
|
||||
:param _data: Received Message
|
||||
"""
|
||||
if "extra" in json:
|
||||
if "member_count" in json["extra"] and json["extra"]["member_count"] > 0:
|
||||
self.roomPopularity = json["extra"]["member_count"]
|
||||
elif "data" in json:
|
||||
if "popularity" in json["data"]:
|
||||
self.roomPopularity = json["data"]["popularity"]
|
||||
if "extra" in _data:
|
||||
if "member_count" in _data["extra"] and _data["extra"]["member_count"] > 0:
|
||||
self.roomPopularity = _data["extra"]["member_count"]
|
||||
elif "data" in _data:
|
||||
if "popularity" in _data["data"]:
|
||||
self.roomPopularity = _data["data"]["popularity"]
|
||||
|
||||
def apiChangedError(self, msg: str, *args):
|
||||
@staticmethod
|
||||
def apiChangedError(msg: str, *args):
|
||||
"""
|
||||
API发生更改时的提示
|
||||
Warning while Detected Api has Changed
|
||||
:param msg:
|
||||
:param args:
|
||||
:param msg: 提示信息
|
||||
:param args: DEBUG模式下,显示更多信息
|
||||
"""
|
||||
print(msg)
|
||||
if DEBUG:
|
||||
@ -134,12 +140,13 @@ class XiGuaLiveApi:
|
||||
def _checkUsernameIsMatched(self):
|
||||
"""
|
||||
验证主播名字是自己想要的那个
|
||||
Check name matched
|
||||
:return: bool: 是否匹配
|
||||
"""
|
||||
if self.name is None or self.roomLiver is None:
|
||||
return False
|
||||
#验证主播名字是自己想要的那个
|
||||
return True
|
||||
if self.name in self.roomLiver:
|
||||
return True
|
||||
|
||||
def _forceSearchUser(self):
|
||||
"""
|
||||
@ -170,7 +177,7 @@ class XiGuaLiveApi:
|
||||
self.isLive = False
|
||||
self.roomLiver = User(i["cells"][0])
|
||||
if self.isLive:
|
||||
return self.updRoomInfo()
|
||||
return self._updateRoomOnly()
|
||||
else:
|
||||
return False
|
||||
|
||||
@ -211,7 +218,7 @@ class XiGuaLiveApi:
|
||||
|
||||
def updRoomInfo(self):
|
||||
"""
|
||||
更新房间信息(可能写的很垃圾)
|
||||
更新房间信息
|
||||
:return:
|
||||
"""
|
||||
if self.isLive:
|
||||
@ -231,7 +238,7 @@ class XiGuaLiveApi:
|
||||
print(p.text)
|
||||
d = p.json()
|
||||
if "data" not in d or "title" not in d["data"] or "id" not in d["data"]:
|
||||
XiGuaLiveApi.apiChangedError("无法获取RoomID,请与我联系")
|
||||
XiGuaLiveApi.apiChangedError("网页接口已更改,除非你是开发者,请不要用这个方法", d)
|
||||
return XiGuaLiveApi()
|
||||
return XiGuaLiveApi(d["data"]["id"])
|
||||
|
||||
@ -240,7 +247,7 @@ class XiGuaLiveApi:
|
||||
"""
|
||||
通过关键词搜索主播
|
||||
:param keyword: 关键词
|
||||
:return:
|
||||
:return: array: 搜索结果
|
||||
"""
|
||||
ret = []
|
||||
p = s.get("https://security.snssdk.com/video/app/search/live/?version_code=730&device_platform=android"
|
||||
@ -248,10 +255,9 @@ class XiGuaLiveApi:
|
||||
d = p.json()
|
||||
if "data" in d:
|
||||
for i in d["data"]:
|
||||
if i["block_type"] != 0:
|
||||
continue
|
||||
for _i in i["cells"]:
|
||||
ret.append(_i["room"])
|
||||
if i["block_type"] == 0:
|
||||
for _i in i["cells"]:
|
||||
ret.append(_i["room"])
|
||||
return ret
|
||||
|
||||
def getDanmaku(self):
|
||||
@ -268,18 +274,14 @@ class XiGuaLiveApi:
|
||||
))
|
||||
d = p.json()
|
||||
if "data" not in d or "extra" not in d or "cursor" not in d["extra"]:
|
||||
if DEBUG:
|
||||
print(d)
|
||||
if "base_resp" in d:
|
||||
if d["base_resp"]["status_code"] != 10038:
|
||||
print(d["base_resp"]["status_message"])
|
||||
else:
|
||||
self.apiChangedError("数据结构改变,请与我联系")
|
||||
if "base_resp" in d and d["base_resp"]["status_code"] != 10038:
|
||||
print(d["base_resp"]["status_message"])
|
||||
self.apiChangedError("接口数据返回错误", d)
|
||||
return
|
||||
else:
|
||||
self._cursor = d["extra"]["cursor"]
|
||||
if DEBUG:
|
||||
print("Cursor", self._cursor)
|
||||
print("Cursor:\t", self._cursor)
|
||||
for i in d['data']:
|
||||
if DEBUG:
|
||||
print(i)
|
||||
@ -333,7 +335,6 @@ if __name__ == "__main__":
|
||||
print("西瓜直播弹幕助手 by JerryYan")
|
||||
api = XiGuaLiveApi(name)
|
||||
if not api.isValidRoom:
|
||||
print(api.roomID)
|
||||
input("房间不存在")
|
||||
sys.exit()
|
||||
print("进入", api.roomLiver, "的直播间")
|
||||
|
@ -34,7 +34,8 @@ def download():
|
||||
return
|
||||
f.write(t)
|
||||
_size += len(t)
|
||||
Common.modifyLastDownloadStatus("Downloading >{}< @ {:.2f}%".format(path, 100.0 * _size/Common.config["p_s"]))
|
||||
Common.modifyLastDownloadStatus(
|
||||
"Downloading >{}< @ {:.2f}%".format(path, 100.0 * _size / Common.config["p_s"]))
|
||||
if _size > Common.config["p_s"]:
|
||||
Common.modifyLastDownloadStatus("Download >{}< Exceed MaxSize".format(path))
|
||||
break
|
||||
@ -59,17 +60,20 @@ def encode():
|
||||
i = Common.encodeQueue.get()
|
||||
if Common.forceNotEncode:
|
||||
Common.appendEncodeStatus("设置了不编码,所以[{}]不会编码".format(i))
|
||||
Common.uploadQueue.put(i)
|
||||
continue
|
||||
if os.path.exists(i):
|
||||
elif os.path.exists(i):
|
||||
isEncode = True
|
||||
if os.path.getsize(i) < 8 * 1024 * 1024:
|
||||
Common.appendEncodeStatus("Encoded File >{}< is too small, will ignore it".format(i))
|
||||
continue
|
||||
Common.appendEncodeStatus("Encoding >{}< Start".format(i))
|
||||
os.system("ffmpeg -i {} -c:v copy -c:a copy -f mp4 {} -y".format(i, i[:13] + ".mp4"))
|
||||
Common.uploadQueue.put(i[:13] + ".mp4")
|
||||
Common.modifyLastEncodeStatus("Encode >{}< Finished".format(i))
|
||||
else:
|
||||
Common.appendEncodeStatus("Encoding >{}< Start".format(i))
|
||||
_code = os.system("ffmpeg -i {} -c:v copy -c:a copy -f mp4 {} -y".format(i, i[:13] + ".mp4"))
|
||||
if _code != 0:
|
||||
Common.appendError("Encode {} with Non-Zero Return.".format(i))
|
||||
continue
|
||||
else:
|
||||
Common.modifyLastEncodeStatus("Encode >{}< Finished".format(i))
|
||||
i = i[:13] + ".mp4"
|
||||
Common.uploadQueue.put(i)
|
||||
|
||||
|
||||
def upload(date=datetime.strftime(datetime.now(), "%Y_%m_%d")):
|
||||
@ -80,27 +84,26 @@ def upload(date=datetime.strftime(datetime.now(), "%Y_%m_%d")):
|
||||
if Common.forceNotUpload:
|
||||
if isinstance(i, bool):
|
||||
Common.appendUploadStatus("设置了不上传,不会发布了")
|
||||
return
|
||||
Common.appendUploadStatus("设置了不上传,所以[{}]不会上传了".format(i))
|
||||
i = Common.uploadQueue.get()
|
||||
continue
|
||||
if isinstance(i, bool):
|
||||
if i is True:
|
||||
b.finishUpload(Common.config["t_t"].format(date), 17, Common.config["tag"], Common.config["des"],
|
||||
source=Common.config["src"], no_reprint=0)
|
||||
b.clear()
|
||||
break
|
||||
if not os.path.exists(i):
|
||||
Common.appendError("Upload File Not Exist {}".format(i))
|
||||
i = Common.uploadQueue.get()
|
||||
continue
|
||||
try:
|
||||
b.preUpload(VideoPart(i, os.path.basename(i)))
|
||||
except Exception as e:
|
||||
Common.appendError(e.__str__())
|
||||
continue
|
||||
if not Common.forceNotEncode:
|
||||
os.remove(i)
|
||||
break
|
||||
else:
|
||||
Common.appendUploadStatus("设置了不上传,所以[{}]不会上传了".format(i))
|
||||
else:
|
||||
if isinstance(i, bool):
|
||||
if i is True:
|
||||
b.finishUpload(Common.config["t_t"].format(date), 17, Common.config["tag"], Common.config["des"],
|
||||
source=Common.config["src"], no_reprint=0)
|
||||
b.clear()
|
||||
break
|
||||
if not os.path.exists(i):
|
||||
Common.appendError("Upload File Not Exist {}".format(i))
|
||||
else:
|
||||
try:
|
||||
b.preUpload(VideoPart(i, os.path.basename(i)))
|
||||
except Exception as e:
|
||||
Common.appendError(e.__str__())
|
||||
continue
|
||||
if not Common.forceNotEncode:
|
||||
os.remove(i)
|
||||
i = Common.uploadQueue.get()
|
||||
Common.appendUploadStatus("Upload Daemon Quiting")
|
||||
|
||||
@ -143,7 +146,7 @@ def awakeUpload():
|
||||
|
||||
|
||||
def run():
|
||||
global isEncode, isDownload, et, t, ut
|
||||
global isEncode, isDownload
|
||||
Common.refreshDownloader()
|
||||
if not Common.api.isValidRoom:
|
||||
Common.appendError("[{}]房间未找到".format(Common.config["l_u"]))
|
||||
@ -152,12 +155,10 @@ def run():
|
||||
_count = 0
|
||||
while True:
|
||||
if Common.api.isLive and not Common.forceNotBroadcasting:
|
||||
if not t.is_alive() and not Common.forceNotDownload:
|
||||
if not Common.forceNotDownload:
|
||||
awakeDownload()
|
||||
if not ut.is_alive():
|
||||
awakeUpload()
|
||||
if not et.is_alive():
|
||||
awakeEncode()
|
||||
awakeUpload()
|
||||
awakeEncode()
|
||||
if _count % 15 == 14:
|
||||
try:
|
||||
Common.api.updRoomInfo()
|
||||
@ -182,15 +183,8 @@ def run():
|
||||
if not Common.api.roomLiver:
|
||||
Common.refreshDownloader()
|
||||
if Common.forceStartEncodeThread:
|
||||
if not et.is_alive():
|
||||
et = threading.Thread(target=encode, args=())
|
||||
et.setDaemon(True)
|
||||
et.start()
|
||||
awakeEncode()
|
||||
Common.forceStartEncodeThread = False
|
||||
if Common.forceStartUploadThread:
|
||||
if not ut.is_alive():
|
||||
d = datetime.strftime(datetime.now(), "%Y_%m_%d")
|
||||
ut = threading.Thread(target=upload, args=(d,))
|
||||
ut.setDaemon(True)
|
||||
ut.start()
|
||||
awakeUpload()
|
||||
Common.forceStartUploadThread = False
|
||||
|
Reference in New Issue
Block a user