剔除一些无效代码,优化部分逻辑
This commit is contained in:
parent
64fbe010ef
commit
adc6e10df1
43
Common.py
43
Common.py
@ -34,9 +34,11 @@ config = {
|
|||||||
# 仅下载
|
# 仅下载
|
||||||
"dlO": True,
|
"dlO": True,
|
||||||
# 下播延迟投稿
|
# 下播延迟投稿
|
||||||
"dly": 30
|
"dly": 30,
|
||||||
|
"enc": "ffmpeg -i {f} -c:v copy -c:a copy -f mp4 {t} -y"
|
||||||
}
|
}
|
||||||
doCleanTime = datetime.now()
|
doCleanTime = datetime.now()
|
||||||
|
loginTime = datetime.now()
|
||||||
_clean_flag = None
|
_clean_flag = None
|
||||||
delay = 30
|
delay = 30
|
||||||
b = Bilibili()
|
b = Bilibili()
|
||||||
@ -75,10 +77,12 @@ def resetDelay():
|
|||||||
|
|
||||||
|
|
||||||
def doDelay():
|
def doDelay():
|
||||||
global delay
|
global delay, isBroadcasting, isEncode, isUpload
|
||||||
|
if isBroadcasting or isEncode or isUpload:
|
||||||
|
resetDelay()
|
||||||
|
return False
|
||||||
if delay < 0:
|
if delay < 0:
|
||||||
resetDelay()
|
resetDelay()
|
||||||
sleep(60)
|
|
||||||
delay -= 1
|
delay -= 1
|
||||||
return delay < 0
|
return delay < 0
|
||||||
|
|
||||||
@ -167,6 +171,8 @@ if config["dlO"] is True:
|
|||||||
forceNotEncode = True
|
forceNotEncode = True
|
||||||
forceStartEncodeThread = False
|
forceStartEncodeThread = False
|
||||||
forceStartUploadThread = False
|
forceStartUploadThread = False
|
||||||
|
isEncode = False
|
||||||
|
isUpload = False
|
||||||
|
|
||||||
uploadQueue = queue.Queue()
|
uploadQueue = queue.Queue()
|
||||||
encodeQueue = queue.Queue()
|
encodeQueue = queue.Queue()
|
||||||
@ -295,7 +301,11 @@ def appendError(obj):
|
|||||||
|
|
||||||
def loginBilibili():
|
def loginBilibili():
|
||||||
if "dlO" not in config or config["dlO"] is False or forceNotUpload is False:
|
if "dlO" not in config or config["dlO"] is False or forceNotUpload is False:
|
||||||
|
global loginTime
|
||||||
|
if getTimeDelta(datetime.now(), loginTime) < 86400 * 3:
|
||||||
|
return True
|
||||||
res = b.login(config["b_u"], config["b_p"])
|
res = b.login(config["b_u"], config["b_p"])
|
||||||
|
loginTime = datetime.now()
|
||||||
appendOperation("登陆账号,结果为:[{}]".format(res))
|
appendOperation("登陆账号,结果为:[{}]".format(res))
|
||||||
|
|
||||||
|
|
||||||
@ -368,15 +378,19 @@ def refreshDownloader():
|
|||||||
|
|
||||||
|
|
||||||
def uploadVideo(name):
|
def uploadVideo(name):
|
||||||
|
global isUpload
|
||||||
if not os.path.exists(name):
|
if not os.path.exists(name):
|
||||||
Common.appendError("Upload File Not Exist {}".format(name))
|
Common.appendError("Upload File Not Exist {}".format(name))
|
||||||
if forceNotUpload is False:
|
if forceNotUpload is False:
|
||||||
|
isUpload = True
|
||||||
b.preUpload(VideoPart(name, os.path.basename(name)))
|
b.preUpload(VideoPart(name, os.path.basename(name)))
|
||||||
|
isUpload = False
|
||||||
else:
|
else:
|
||||||
appendUploadStatus("设置了不上传,所以[{}]不会上传了".format(name))
|
appendUploadStatus("设置了不上传,所以[{}]不会上传了".format(name))
|
||||||
if not Common.forceNotEncode:
|
if not Common.forceNotEncode:
|
||||||
os.remove(name)
|
os.remove(name)
|
||||||
|
|
||||||
|
|
||||||
def publishVideo(date):
|
def publishVideo(date):
|
||||||
if forceNotUpload is False:
|
if forceNotUpload is False:
|
||||||
b.finishUpload(config["t_t"].format(date), 17, config["tag"], config["des"],
|
b.finishUpload(config["t_t"].format(date), 17, config["tag"], config["des"],
|
||||||
@ -384,3 +398,26 @@ def publishVideo(date):
|
|||||||
b.clear()
|
b.clear()
|
||||||
else:
|
else:
|
||||||
appendUploadStatus("设置了不上传,所以[{}]的录播不会上传了".format(date))
|
appendUploadStatus("设置了不上传,所以[{}]的录播不会上传了".format(date))
|
||||||
|
|
||||||
|
|
||||||
|
def encodeVideo(name):
|
||||||
|
if forceNotEncode:
|
||||||
|
appendEncodeStatus("设置了不编码,所以[{}]不会编码".format(name))
|
||||||
|
return False
|
||||||
|
if not os.path.exists(name):
|
||||||
|
appendEncodeStatus("文件[{}]不存在".format(name))
|
||||||
|
return False
|
||||||
|
if os.path.getsize(name) < 8 * 1024 * 1024:
|
||||||
|
appendEncodeStatus("Encoded File >{}< is too small, will ignore it".format(name))
|
||||||
|
return False
|
||||||
|
appendEncodeStatus("Encoding >{}< Start".format(name))
|
||||||
|
global isEncode
|
||||||
|
isEncode=True
|
||||||
|
_new_name = os.path.splitext(name)[0]+".mp4"
|
||||||
|
_code = os.system(config["enc"].format(f=name, t=_new_name))
|
||||||
|
isEncode=False
|
||||||
|
if _code != 0:
|
||||||
|
Common.appendError("Encode {} with Non-Zero Return.".format(name))
|
||||||
|
return False
|
||||||
|
Common.modifyLastEncodeStatus("Encode >{}< Finished".format(name))
|
||||||
|
uploadQueue.put(_new_name)
|
||||||
|
@ -7,12 +7,9 @@ import Common
|
|||||||
import os
|
import os
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
isEncode = False
|
|
||||||
isDownload = False
|
|
||||||
|
|
||||||
|
|
||||||
def download():
|
def download():
|
||||||
global isDownload
|
|
||||||
session = requests.session()
|
session = requests.session()
|
||||||
while Common.api.isLive and not Common.forceNotDownload:
|
while Common.api.isLive and not Common.forceNotDownload:
|
||||||
if not Common.streamUrl:
|
if not Common.streamUrl:
|
||||||
@ -23,7 +20,6 @@ def download():
|
|||||||
if p.status_code != 200:
|
if p.status_code != 200:
|
||||||
Common.appendDownloadStatus("Download with Response {}".format(p.status_code))
|
Common.appendDownloadStatus("Download with Response {}".format(p.status_code))
|
||||||
break
|
break
|
||||||
isDownload = True
|
|
||||||
Common.appendDownloadStatus("Download >{}< Start".format(path))
|
Common.appendDownloadStatus("Download >{}< Start".format(path))
|
||||||
f = open(path, "wb")
|
f = open(path, "wb")
|
||||||
_size = 0
|
_size = 0
|
||||||
@ -43,7 +39,6 @@ def download():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
Common.appendError("Download >{}< With Exception {}".format(path, e.__str__()))
|
Common.appendError("Download >{}< With Exception {}".format(path, e.__str__()))
|
||||||
f.close()
|
f.close()
|
||||||
isDownload = False
|
|
||||||
if os.path.getsize(path) < 1024 * 1024:
|
if os.path.getsize(path) < 1024 * 1024:
|
||||||
Common.modifyLastDownloadStatus("Downloaded File >{}< is too small, will ignore it".format(path))
|
Common.modifyLastDownloadStatus("Downloaded File >{}< is too small, will ignore it".format(path))
|
||||||
os.remove(path)
|
os.remove(path)
|
||||||
@ -53,28 +48,10 @@ def download():
|
|||||||
|
|
||||||
|
|
||||||
def encode():
|
def encode():
|
||||||
global isEncode
|
|
||||||
Common.appendEncodeStatus("Encode Daemon Starting")
|
Common.appendEncodeStatus("Encode Daemon Starting")
|
||||||
while True:
|
while True:
|
||||||
isEncode = False
|
|
||||||
i = Common.encodeQueue.get()
|
i = Common.encodeQueue.get()
|
||||||
if Common.forceNotEncode:
|
Common.encodeVideo(i)
|
||||||
Common.appendEncodeStatus("设置了不编码,所以[{}]不会编码".format(i))
|
|
||||||
elif os.path.exists(i):
|
|
||||||
if os.path.getsize(i) < 8 * 1024 * 1024:
|
|
||||||
Common.appendEncodeStatus("Encoded File >{}< is too small, will ignore it".format(i))
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
Common.appendEncodeStatus("Encoding >{}< Start".format(i))
|
|
||||||
isEncode = True
|
|
||||||
_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():
|
def upload():
|
||||||
@ -84,14 +61,14 @@ def upload():
|
|||||||
i = Common.uploadQueue.get()
|
i = Common.uploadQueue.get()
|
||||||
while True:
|
while True:
|
||||||
Common.doClean()
|
Common.doClean()
|
||||||
if isinstance(i, bool):
|
if i is True:
|
||||||
if i is True:
|
Common.publishVideo(date)
|
||||||
Common.publishVideo(date)
|
|
||||||
break
|
break
|
||||||
try:
|
try:
|
||||||
Common.uploadVideo(i)
|
Common.uploadVideo(i)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
Common.appendError(e.__str__())
|
Common.appendError(e.__str__())
|
||||||
|
time.sleep(120)
|
||||||
continue
|
continue
|
||||||
i = Common.uploadQueue.get()
|
i = Common.uploadQueue.get()
|
||||||
Common.appendUploadStatus("Upload Daemon Quiting")
|
Common.appendUploadStatus("Upload Daemon Quiting")
|
||||||
@ -134,7 +111,6 @@ def awakeUpload():
|
|||||||
|
|
||||||
|
|
||||||
def run():
|
def run():
|
||||||
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"]))
|
||||||
@ -172,8 +148,6 @@ def run():
|
|||||||
if Common.forceStartUploadThread:
|
if Common.forceStartUploadThread:
|
||||||
awakeUpload()
|
awakeUpload()
|
||||||
Common.forceStartUploadThread = False
|
Common.forceStartUploadThread = False
|
||||||
if not isEncode and not isDownload and Common.doDelay():
|
if Common.doDelay():
|
||||||
Common.uploadQueue.put(True)
|
Common.uploadQueue.put(True)
|
||||||
isEncode = True
|
|
||||||
isDownload = True
|
|
||||||
time.sleep(60)
|
time.sleep(60)
|
||||||
|
Reference in New Issue
Block a user