This repository has been archived on 2022-05-30. You can view files and clone it, but cannot push or open issues or pull requests.
XiguaLiveDanmakuHelper/liveDownloader.py
2019-04-16 15:14:18 +08:00

172 lines
6.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import shutil
import sys
import time
from datetime import datetime
import threading
from bilibili import *
import Common
import os
import requests
isEncode = False
isDownload = False
def download(url):
global isDownload
path = datetime.strftime(datetime.now(), "%Y%m%d_%H%M.flv")
p = requests.get(url, stream=True)
if p.status_code != 200:
Common.appendDownloadStatus("Download with Response 404, maybe broadcaster is not broadcasting")
return True
isDownload = True
Common.appendDownloadStatus("Download >{}< Start".format(path))
f = open(path, "wb")
try:
for t in p.iter_content(chunk_size=64 * 1024):
if t:
f.write(t)
_size = os.path.getsize(path)
Common.modifyLastDownloadStatus("Downloading >{}< @ {:.2f}%".format(path, 100.0 * _size/Common.config["p_s"]))
if _size > Common.config["p_s"] or Common.forceNotDownload:
Common.modifyLastDownloadStatus("Download >{}< Exceed MaxSize".format(path))
break
except Exception as e:
Common.appendError("Download >{}< With Exception {}".format(path, datetime.strftime(datetime.now(), "%y%m%d %H%M"),
e.__str__()))
f.close()
isDownload = False
Common.modifyLastDownloadStatus("Download >{}< Finished".format(path))
if os.path.getsize(path) < 1024 * 1024:
Common.modifyLastDownloadStatus("Downloaded File >{}< is too small, will ignore it".format(path))
os.remove(path)
return False
if Common.forceNotDownload:
Common.modifyLastDownloadStatus("设置了不下载,所以[{}]不会下载".format(path))
return
else:
Common.encodeQueue.put(path)
download(url)
def encode():
global isEncode
Common.appendEncodeStatus("Encode Daemon Starting")
while True:
i = Common.encodeQueue.get()
if Common.forceNotEncode:
Common.appendEncodeStatus("设置了不编码,所以[{}]不会编码".format(i))
Common.uploadQueue.put(i)
continue
if os.path.exists(i):
isEncode = True
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))
if Common.config["mv"]:
shutil.move(i, Common.config["mtd"])
elif Common.config["del"]:
os.remove(i)
isEncode = False
def upload(date=datetime.strftime(datetime.now(), "%Y_%m_%d")):
Common.appendUploadStatus("Upload Daemon Starting")
i = Common.uploadQueue.get()
while True:
if Common.forceNotUpload:
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
os.remove(i)
i = Common.uploadQueue.get()
Common.appendUploadStatus("Upload Daemon Quiting")
b = Bilibili()
b.login(Common.config["b_u"], Common.config["b_p"])
def run():
global isEncode, isDownload
et = threading.Thread(target=encode, args=())
et.setDaemon(True)
et.start()
if not Common.api.isValidRoom:
Common.appendError("[{}]房间不存在".format(Common.config["l_u"]))
return
d = None
t = threading.Thread(target=download)
ut = threading.Thread(target=upload, args=(d,))
_count = 0
_count_error = 0
while True:
if Common.api.isLive and not Common.forceNotBroadcasting:
if d is None:
d = datetime.strftime(datetime.now(), "%Y_%m_%d")
if not t.is_alive() and not Common.forceNotDownload:
try:
Common.api.updRoomInfo()
_count = 0
_count_error = 0
except Exception as e:
Common.appendError(e.__str__())
continue
_count_error += 1
_preT = Common.api.playlist
t = threading.Thread(target=download, args=(_preT,))
t.setDaemon(True)
t.start()
if not ut.is_alive():
ut = threading.Thread(target=upload, args=(d,))
ut.setDaemon(True)
ut.start()
if not et.is_alive():
et = threading.Thread(target=encode, args=())
et.setDaemon(True)
et.start()
if _count % 15 == 0:
try:
Common.api.updRoomInfo()
_count = 0
_count_error = 0
except Exception as e:
Common.appendError(e.__str__())
time.sleep(20)
_count_error += 1
continue
if _count_error > 15:
Common.api.isLive = False
_count += 1
time.sleep(20)
else:
if d is not None:
d = None
if not isEncode and not isDownload:
Common.uploadQueue.put(True)
isEncode = True
isDownload = True
# print("主播未开播等待1分钟后重试")
time.sleep(60)
try:
Common.api.updRoomInfo()
_count_error = 0
except Exception as e:
Common.appendError(e.__str__())