增加操作日志,记录操作日志

This commit is contained in:
Jerry Yan 2019-04-09 22:15:37 +08:00 committed by JerryYan
parent 612a9185eb
commit e066048e9f
7 changed files with 104 additions and 67 deletions

View File

@ -1,8 +1,10 @@
import os
import queue import queue
from datetime import datetime from datetime import datetime
import psutil import psutil
from api import XiGuaLiveApi from api import XiGuaLiveApi
import json import json
from bilibili import Bilibili
_config_fp = open("config.json","r",encoding="utf8") _config_fp = open("config.json","r",encoding="utf8")
config = json.load(_config_fp) config = json.load(_config_fp)
@ -41,9 +43,11 @@ def getTimeDelta(a, b):
def getCurrentStatus(): def getCurrentStatus():
_disk = psutil.disk_usage("/") _disk = psutil.disk_usage("/")
if _disk.percent > 85:
os.system("rm -f `find . -ctime 1 -name '*.flv'`")
_mem = psutil.virtual_memory() _mem = psutil.virtual_memory()
_net = psutil.net_io_counters() _net = psutil.net_io_counters()
if 60 > (datetime.now() - network["currentTime"]).seconds > 0: if 60 > getTimeDelta(datetime.now(),network["currentTime"]) > 0:
_outSpeed = (_net.bytes_sent - network["out"]["currentByte"])/getTimeDelta(datetime.now(),network["currentTime"]) _outSpeed = (_net.bytes_sent - network["out"]["currentByte"])/getTimeDelta(datetime.now(),network["currentTime"])
_inSpeed = (_net.bytes_recv - network["in"]["currentByte"])/getTimeDelta(datetime.now(),network["currentTime"]) _inSpeed = (_net.bytes_recv - network["in"]["currentByte"])/getTimeDelta(datetime.now(),network["currentTime"])
else: else:
@ -79,8 +83,8 @@ updateTime = ""
forceNotDownload = False forceNotDownload = False
forceNotBroadcasting = False forceNotBroadcasting = False
forceNotUpload = True forceNotUpload = False
forceNotEncode = True forceNotEncode = False
uploadQueue = queue.Queue() uploadQueue = queue.Queue()
encodeQueue = queue.Queue() encodeQueue = queue.Queue()
@ -89,6 +93,21 @@ uploadStatus = []
downloadStatus = [] downloadStatus = []
encodeStatus = [] encodeStatus = []
errors = [] errors = []
operations = []
def appendOperation(obj):
global operations
if isinstance(obj, dict):
if "datetime" not in obj:
obj["datetime"] = datetime.strftime(datetime.now(), dt_format)
operations.append(obj)
else:
operations.append({
"datetime": datetime.strftime(datetime.now(), dt_format),
"message": str(obj)
})
operations = operations[-config["elc"]:]
def parseSize(size): def parseSize(size):
@ -251,3 +270,5 @@ class downloader(XiGuaLiveApi):
def onSubscribe(self, user): def onSubscribe(self, user):
pass pass
api = downloader(config["l_u"])

View File

@ -39,6 +39,7 @@ def writeConfig():
@app.route("/force/not/upload", methods=["POST"]) @app.route("/force/not/upload", methods=["POST"])
def toggleForceNotUpload(): def toggleForceNotUpload():
Common.forceNotUpload = not Common.forceNotUpload Common.forceNotUpload = not Common.forceNotUpload
Common.appendOperation("将强制不上传的值改为:{}".format(Common.forceNotUpload))
return jsonify({"message":"ok","code":200,"status":0,"data":{ return jsonify({"message":"ok","code":200,"status":0,"data":{
"forceNotUpload": Common.forceNotUpload, "forceNotUpload": Common.forceNotUpload,
}}) }})
@ -47,6 +48,7 @@ def toggleForceNotUpload():
@app.route("/force/not/encode", methods=["POST"]) @app.route("/force/not/encode", methods=["POST"])
def toggleForceNotEncode(): def toggleForceNotEncode():
Common.forceNotEncode = not Common.forceNotEncode Common.forceNotEncode = not Common.forceNotEncode
Common.appendOperation("将强制不编码的值改为:{}".format(Common.forceNotEncode))
return jsonify({"message":"ok","code":200,"status":0,"data":{ return jsonify({"message":"ok","code":200,"status":0,"data":{
"forceNotEncode": Common.forceNotEncode, "forceNotEncode": Common.forceNotEncode,
}}) }})
@ -55,6 +57,7 @@ def toggleForceNotEncode():
@app.route("/force/not/download", methods=["POST"]) @app.route("/force/not/download", methods=["POST"])
def toggleForceNotDownload(): def toggleForceNotDownload():
Common.forceNotDownload = not Common.forceNotDownload Common.forceNotDownload = not Common.forceNotDownload
Common.appendOperation("将强制不下载的值改为:{}".format(Common.forceNotDownload))
return jsonify({"message":"ok","code":200,"status":0,"data":{ return jsonify({"message":"ok","code":200,"status":0,"data":{
"forceNotDownload": Common.forceNotDownload, "forceNotDownload": Common.forceNotDownload,
}}) }})
@ -70,20 +73,23 @@ def toggleForceNotBroadcast():
@app.route("/encode/insert", methods=["POST"]) @app.route("/encode/insert", methods=["POST"])
def insertEncode(): def insertEncode():
if "filename" in request.form: if "filename" in request.form and os.path.exists(request.form["filename"]):
Common.appendOperation("添加编码文件:{}".format(request.form["filename"]))
Common.encodeQueue.put(request.form["filename"]) Common.encodeQueue.put(request.form["filename"])
return jsonify({"message":"ok","code":200,"status":0}) return jsonify({"message":"ok","code":200,"status":0})
@app.route("/upload/insert", methods=["POST"]) @app.route("/upload/insert", methods=["POST"])
def insertUpload(): def insertUpload():
if "filename" in request.form: if "filename" in request.form and os.path.exists(request.form["filename"]):
Common.appendOperation("添加上传文件:{}".format(request.form["filename"]))
Common.uploadQueue.put(request.form["filename"]) Common.uploadQueue.put(request.form["filename"])
return jsonify({"message":"ok","code":200,"status":0}) return jsonify({"message":"ok","code":200,"status":0})
@app.route("/upload/finish", methods=["POST"]) @app.route("/upload/finish", methods=["POST"])
def finishUpload(): def finishUpload():
Common.appendOperation("设置当前已完成上传")
Common.uploadQueue.put(True) Common.uploadQueue.put(True)
return jsonify({"message":"ok","code":200,"status":0}) return jsonify({"message":"ok","code":200,"status":0})
@ -97,6 +103,7 @@ def getAllStats():
"upload": Common.uploadStatus, "upload": Common.uploadStatus,
"uploadQueueSize": Common.uploadQueue.qsize(), "uploadQueueSize": Common.uploadQueue.qsize(),
"error": Common.errors, "error": Common.errors,
"operation": Common.operations,
"broadcast": { "broadcast": {
"broadcaster": Common.broadcaster.__str__(), "broadcaster": Common.broadcaster.__str__(),
"isBroadcasting": Common.isBroadcasting, "isBroadcasting": Common.isBroadcasting,
@ -202,6 +209,6 @@ def SubThread():
t.start() t.start()
# p = threading.Thread(target=SubThread) p = threading.Thread(target=SubThread)
# p.setDaemon(True) p.setDaemon(True)
# p.start() p.start()

View File

@ -5,7 +5,7 @@ import re
import json as JSON import json as JSON
from datetime import datetime from datetime import datetime
from time import sleep from time import sleep
from Common import appendUploadStatus, modifyLastUploadStatus import Common
import rsa import rsa
import math import math
import base64 import base64
@ -205,7 +205,7 @@ class Bilibili:
filepath = part.path filepath = part.path
filename = os.path.basename(filepath) filename = os.path.basename(filepath)
filesize = os.path.getsize(filepath) filesize = os.path.getsize(filepath)
appendUploadStatus("Upload >{}< Started".format(filepath)) Common.appendUploadStatus("Upload >{}< Started".format(filepath))
self.files.append(part) self.files.append(part)
r = self.session.get('https://member.bilibili.com/preupload?' r = self.session.get('https://member.bilibili.com/preupload?'
'os=upos&upcdn=ws&name={name}&size={size}&r=upos&profile=ugcupos%2Fyb&ssl=0' 'os=upos&upcdn=ws&name={name}&size={size}&r=upos&profile=ugcupos%2Fyb&ssl=0'
@ -244,7 +244,7 @@ class Bilibili:
chunks_num = math.ceil(filesize / chunk_size) chunks_num = math.ceil(filesize / chunk_size)
chunks_index = 0 chunks_index = 0
chunks_data = f.read(chunk_size) chunks_data = f.read(chunk_size)
modifyLastUploadStatus("Uploading >{}< @ {:.2f}%".format(filepath, 100.0 * chunks_index / chunks_num)) Common.modifyLastUploadStatus("Uploading >{}< @ {:.2f}%".format(filepath, 100.0 * chunks_index / chunks_num))
while True: while True:
_d = datetime.now() _d = datetime.now()
if not chunks_data: if not chunks_data:
@ -268,7 +268,7 @@ class Bilibili:
continue continue
chunks_data = f.read(chunk_size) chunks_data = f.read(chunk_size)
chunks_index += 1 # start with 0 chunks_index += 1 # start with 0
modifyLastUploadStatus("Uploading >{}< @ {:.2f}%".format(filepath, 100.0*chunks_index/chunks_num)) Common.modifyLastUploadStatus("Uploading >{}< @ {:.2f}%".format(filepath, 100.0*chunks_index/chunks_num))
if (datetime.now()-_d).seconds < 2: if (datetime.now()-_d).seconds < 2:
sleep(1) sleep(1)
@ -286,7 +286,7 @@ class Bilibili:
self.videos.append({'filename': upos_uri.replace('upos://ugc/', '').split('.')[0], self.videos.append({'filename': upos_uri.replace('upos://ugc/', '').split('.')[0],
'title': part.title, 'title': part.title,
'desc': part.desc}) 'desc': part.desc})
modifyLastUploadStatus("Upload >{}< Finished".format(filepath)) Common.modifyLastUploadStatus("Upload >{}< Finished".format(filepath))
__f = open("uploaded.json","w") __f = open("uploaded.json","w")
JSON.dump(self.videos, __f) JSON.dump(self.videos, __f)
@ -319,7 +319,7 @@ class Bilibili:
""" """
if len(self.videos) == 0: if len(self.videos) == 0:
return return
appendUploadStatus("[{}]投稿中,请稍后".format(title)) Common.appendUploadStatus("[{}]投稿中,请稍后".format(title))
self.session.headers['Content-Type'] = 'application/json; charset=utf-8' self.session.headers['Content-Type'] = 'application/json; charset=utf-8'
copyright = 2 if source else 1 copyright = 2 if source else 1
r = self.session.post('https://member.bilibili.com/x/vu/web/add?csrf=' + self.csrf, r = self.session.post('https://member.bilibili.com/x/vu/web/add?csrf=' + self.csrf,
@ -336,21 +336,21 @@ class Bilibili:
"order_id": 0, "order_id": 0,
"videos": self.videos} "videos": self.videos}
) )
appendUploadStatus("[{}] Published | Result : {}".format(title, r.text)) Common.appendUploadStatus("[{}] Published | Result : {}".format(title, r.text))
def reloadFromPrevious(self): def reloadFromPrevious(self):
if os.path.exists("uploaded.json"): if os.path.exists("uploaded.json"):
__f = open("uploaded.json", "r") __f = open("uploaded.json", "r")
try: try:
self.videos = JSON.load(__f) self.videos = JSON.load(__f)
appendUploadStatus("RELOAD SUCCESS") Common.appendUploadStatus("RELOAD SUCCESS")
except: except:
appendUploadStatus("RELOAD Failed") Common.appendUploadStatus("RELOAD Failed")
self.videos = [] self.videos = []
__f.close() __f.close()
os.remove("uploaded.json") os.remove("uploaded.json")
else: else:
appendUploadStatus("RELOAD Failed") Common.appendUploadStatus("RELOAD Failed")
self.videos = [] self.videos = []
def clear(self): def clear(self):

View File

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

View File

@ -21,4 +21,4 @@ function deviceUpdate(){
} }
deviceUpdate() deviceUpdate()
setInterval(deviceUpdate,2000) setInterval(deviceUpdate,4000)

View File

@ -11,6 +11,8 @@ function taskUpdate(){
$("#forceNotUpload").text(res.data.config.forceNotUpload) $("#forceNotUpload").text(res.data.config.forceNotUpload)
$("#forceNotEncode").text(res.data.config.forceNotEncode) $("#forceNotEncode").text(res.data.config.forceNotEncode)
$("#updateTime").text(res.data.broadcast.updateTime) $("#updateTime").text(res.data.broadcast.updateTime)
$("#encodeQueueSize").text(res.data.encodeQueueSize)
$("#uploadQueueSize").text(res.data.uploadQueueSize)
$("#download").html(function(){ $("#download").html(function(){
var ret = "" var ret = ""
res.data.download.reverse().forEach(function(obj){ res.data.download.reverse().forEach(function(obj){
@ -39,6 +41,13 @@ function taskUpdate(){
}) })
return "<table>" + ret + "</table>" return "<table>" + ret + "</table>"
}) })
$("#operation").html(function(){
var ret = ""
res.data.operation.reverse().forEach(function(obj){
ret += "<tr><td>" + obj.datetime + "</td><td>" + obj.message + "</td></tr>"
})
return "<table>" + ret + "</table>"
})
} }
} }
) )

View File

@ -55,10 +55,12 @@
<tr> <tr>
<td>转码日志</td> <td>转码日志</td>
<td><span id="encode"></span></td> <td><span id="encode"></span></td>
<td>转码队列<span id="encodeQueueSize"></span></td>
</tr> </tr>
<tr> <tr>
<td>上传日志</td> <td>上传日志</td>
<td><span id="upload"></span></td> <td><span id="upload"></span></td>
<td>上传队列<span id="uploadQueueSize"></span></td>
</tr> </tr>
<tr> <tr>
<td>错误日志</td> <td>错误日志</td>