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