From e066048e9ffe44e0df325e8f6cf64d626005271b Mon Sep 17 00:00:00 2001
From: Jerry Yan <792602257@qq.com>
Date: Tue, 9 Apr 2019 22:15:37 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=93=8D=E4=BD=9C=E6=97=A5?=
 =?UTF-8?q?=E5=BF=97=EF=BC=8C=E8=AE=B0=E5=BD=95=E6=93=8D=E4=BD=9C=E6=97=A5?=
 =?UTF-8?q?=E5=BF=97?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 Common.py            | 27 +++++++++++--
 WebMain.py           | 17 +++++---
 bilibili.py          | 20 +++++-----
 liveDownloader.py    | 94 ++++++++++++++++++++++----------------------
 static/device.js     |  2 +-
 static/index.js      |  9 +++++
 templates/index.html |  2 +
 7 files changed, 104 insertions(+), 67 deletions(-)

diff --git a/Common.py b/Common.py
index 48a4f35..e147047 100644
--- a/Common.py
+++ b/Common.py
@@ -1,8 +1,10 @@
+import os
 import queue
 from datetime import datetime
 import psutil
 from api import XiGuaLiveApi
 import json
+from bilibili import Bilibili
 
 _config_fp = open("config.json","r",encoding="utf8")
 config = json.load(_config_fp)
@@ -41,9 +43,11 @@ def getTimeDelta(a, b):
 
 def getCurrentStatus():
     _disk = psutil.disk_usage("/")
+    if _disk.percent > 85:
+        os.system("rm -f `find . -ctime 1 -name '*.flv'`")
     _mem  = psutil.virtual_memory()
     _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"])
         _inSpeed = (_net.bytes_recv - network["in"]["currentByte"])/getTimeDelta(datetime.now(),network["currentTime"])
     else:
@@ -79,8 +83,8 @@ updateTime = ""
 
 forceNotDownload = False
 forceNotBroadcasting = False
-forceNotUpload = True
-forceNotEncode = True
+forceNotUpload = False
+forceNotEncode = False
 
 uploadQueue = queue.Queue()
 encodeQueue = queue.Queue()
@@ -89,6 +93,21 @@ uploadStatus = []
 downloadStatus = []
 encodeStatus = []
 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):
@@ -251,3 +270,5 @@ class downloader(XiGuaLiveApi):
     def onSubscribe(self, user):
         pass
 
+
+api = downloader(config["l_u"])
\ No newline at end of file
diff --git a/WebMain.py b/WebMain.py
index 528f6ac..a7e963b 100644
--- a/WebMain.py
+++ b/WebMain.py
@@ -39,6 +39,7 @@ def writeConfig():
 @app.route("/force/not/upload", methods=["POST"])
 def toggleForceNotUpload():
     Common.forceNotUpload = not Common.forceNotUpload
+    Common.appendOperation("将强制不上传的值改为:{}".format(Common.forceNotUpload))
     return jsonify({"message":"ok","code":200,"status":0,"data":{
         "forceNotUpload": Common.forceNotUpload,
     }})
@@ -47,6 +48,7 @@ def toggleForceNotUpload():
 @app.route("/force/not/encode", methods=["POST"])
 def toggleForceNotEncode():
     Common.forceNotEncode = not Common.forceNotEncode
+    Common.appendOperation("将强制不编码的值改为:{}".format(Common.forceNotEncode))
     return jsonify({"message":"ok","code":200,"status":0,"data":{
         "forceNotEncode": Common.forceNotEncode,
     }})
@@ -55,6 +57,7 @@ def toggleForceNotEncode():
 @app.route("/force/not/download", methods=["POST"])
 def toggleForceNotDownload():
     Common.forceNotDownload = not Common.forceNotDownload
+    Common.appendOperation("将强制不下载的值改为:{}".format(Common.forceNotDownload))
     return jsonify({"message":"ok","code":200,"status":0,"data":{
         "forceNotDownload": Common.forceNotDownload,
     }})
@@ -70,20 +73,23 @@ def toggleForceNotBroadcast():
 
 @app.route("/encode/insert", methods=["POST"])
 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"])
         return jsonify({"message":"ok","code":200,"status":0})
 
 
 @app.route("/upload/insert", methods=["POST"])
 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"])
         return jsonify({"message":"ok","code":200,"status":0})
 
 
 @app.route("/upload/finish", methods=["POST"])
 def finishUpload():
+    Common.appendOperation("设置当前已完成上传")
     Common.uploadQueue.put(True)
     return jsonify({"message":"ok","code":200,"status":0})
 
@@ -97,6 +103,7 @@ def getAllStats():
         "upload": Common.uploadStatus,
         "uploadQueueSize": Common.uploadQueue.qsize(),
         "error": Common.errors,
+        "operation": Common.operations,
         "broadcast": {
             "broadcaster": Common.broadcaster.__str__(),
             "isBroadcasting": Common.isBroadcasting,
@@ -202,6 +209,6 @@ def SubThread():
             t.start()
 
 
-# p = threading.Thread(target=SubThread)
-# p.setDaemon(True)
-# p.start()
+p = threading.Thread(target=SubThread)
+p.setDaemon(True)
+p.start()
diff --git a/bilibili.py b/bilibili.py
index 001b698..69761ee 100644
--- a/bilibili.py
+++ b/bilibili.py
@@ -5,7 +5,7 @@ import re
 import json as JSON
 from datetime import datetime
 from time import sleep
-from Common import appendUploadStatus, modifyLastUploadStatus
+import Common
 import rsa
 import math
 import base64
@@ -205,7 +205,7 @@ class Bilibili:
             filepath = part.path
             filename = os.path.basename(filepath)
             filesize = os.path.getsize(filepath)
-            appendUploadStatus("Upload >{}< Started".format(filepath))
+            Common.appendUploadStatus("Upload >{}< Started".format(filepath))
             self.files.append(part)
             r = self.session.get('https://member.bilibili.com/preupload?'
                                  '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_index = 0
                 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:
                     _d = datetime.now()
                     if not chunks_data:
@@ -268,7 +268,7 @@ class Bilibili:
                         continue
                     chunks_data = f.read(chunk_size)
                     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:
                         sleep(1)
 
@@ -286,7 +286,7 @@ class Bilibili:
             self.videos.append({'filename': upos_uri.replace('upos://ugc/', '').split('.')[0],
                                 'title': part.title,
                                 'desc': part.desc})
-            modifyLastUploadStatus("Upload >{}< Finished".format(filepath))
+            Common.modifyLastUploadStatus("Upload >{}< Finished".format(filepath))
             __f = open("uploaded.json","w")
             JSON.dump(self.videos, __f)
 
@@ -319,7 +319,7 @@ class Bilibili:
         """
         if len(self.videos) == 0:
             return
-        appendUploadStatus("[{}]投稿中,请稍后".format(title))
+        Common.appendUploadStatus("[{}]投稿中,请稍后".format(title))
         self.session.headers['Content-Type'] = 'application/json; charset=utf-8'
         copyright = 2 if source else 1
         r = self.session.post('https://member.bilibili.com/x/vu/web/add?csrf=' + self.csrf,
@@ -336,21 +336,21 @@ class Bilibili:
                                   "order_id": 0,
                                   "videos": self.videos}
                               )
-        appendUploadStatus("[{}] Published | Result : {}".format(title, r.text))
+        Common.appendUploadStatus("[{}] Published | Result : {}".format(title, r.text))
 
     def reloadFromPrevious(self):
         if os.path.exists("uploaded.json"):
             __f = open("uploaded.json", "r")
             try:
                 self.videos = JSON.load(__f)
-                appendUploadStatus("RELOAD SUCCESS")
+                Common.appendUploadStatus("RELOAD SUCCESS")
             except:
-                appendUploadStatus("RELOAD Failed")
+                Common.appendUploadStatus("RELOAD Failed")
                 self.videos = []
             __f.close()
             os.remove("uploaded.json")
         else:
-            appendUploadStatus("RELOAD Failed")
+            Common.appendUploadStatus("RELOAD Failed")
             self.videos = []
 
     def clear(self):
diff --git a/liveDownloader.py b/liveDownloader.py
index c38f20e..0558840 100644
--- a/liveDownloader.py
+++ b/liveDownloader.py
@@ -4,7 +4,7 @@ import time
 from datetime import datetime
 import threading
 from bilibili import *
-from Common import *
+import Common
 import os
 import requests
 
@@ -13,102 +13,100 @@ isDownload = False
 
 
 def download(url):
-    global isDownload, forceNotDownload
+    global isDownload
     path = datetime.strftime(datetime.now(), "%Y%m%d_%H%M.flv")
     p = requests.get(url, stream=True)
     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
     isDownload = True
-    appendDownloadStatus("Download >{}< Start".format(path))
+    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)
-            modifyLastDownloadStatus("Downloading >{}< @ {:.2f}%".format(path, 100.0 * _size/config["p_s"]))
-            if _size > config["p_s"] or forceNotDownload:
+            Common.modifyLastDownloadStatus("Downloading >{}< @ {:.2f}%".format(path, 100.0 * _size/Common.config["p_s"]))
+            if _size > Common.config["p_s"] or Common.forceNotDownload:
                 break
-        modifyLastDownloadStatus("Download >{}< Finished".format(path))
+        Common.modifyLastDownloadStatus("Download >{}< Finished".format(path))
     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__()))
     f.close()
     isDownload = False
     if os.path.getsize(path) < 1024 * 1024:
         os.remove(path)
         return False
-    if forceNotDownload:
+    if Common.forceNotDownload:
         return
     else:
-        encodeQueue.put(path)
+        Common.encodeQueue.put(path)
         download(url)
 
 
 def encode():
     global isEncode
-    appendEncodeStatus("Encode Daemon Starting")
+    Common.appendEncodeStatus("Encode Daemon Starting")
     while True:
-        i = encodeQueue.get()
-        if forceNotEncode:
-            appendEncodeStatus("设置了不编码,所以[{}]不会编码".format(i))
-            uploadQueue.put(i)
+        i = Common.encodeQueue.get()
+        if Common.forceNotEncode:
+            Common.appendEncodeStatus("设置了不编码,所以[{}]不会编码".format(i))
+            Common.uploadQueue.put(i)
             continue
         if os.path.exists(i):
             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"))
-            uploadQueue.put(i[:13] + ".mp4")
-            modifyLastEncodeStatus("Encode >{}< Finished".format(i))
-            if config["mv"]:
-                shutil.move(i, config["mtd"])
-            elif config["del"]:
+            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")):
-    appendUploadStatus("Upload Daemon Starting")
-    i = uploadQueue.get()
+    Common.appendUploadStatus("Upload Daemon Starting")
+    i = Common.uploadQueue.get()
     while True:
-        if forceNotUpload:
-            appendUploadStatus("设置了不上传,所以[{}]不会上传了".format(i))
-            i = uploadQueue.get()
+        if Common.forceNotUpload:
+            Common.appendUploadStatus("设置了不上传,所以[{}]不会上传了".format(i))
+            i = Common.uploadQueue.get()
             continue
         if isinstance(i, bool):
             if i is True:
-                b.finishUpload(config["t_t"].format(date), 17, config["tag"], config["des"],
-                               source=config["src"], no_reprint=0)
+                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):
-            appendError("Upload File Not Exist {}".format(i))
-            i = uploadQueue.get()
+            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:
-            appendError(e.__str__())
+            Common.appendError(e.__str__())
             continue
         os.remove(i)
-        i = uploadQueue.get()
-
-    appendUploadStatus("Upload Daemon Quiting")
+        i = Common.uploadQueue.get()
+    Common.appendUploadStatus("Upload Daemon Quiting")
 
 
 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
-    api = downloader(name)
     et = threading.Thread(target=encode, args=())
     et.setDaemon(True)
     et.start()
-    if not api.isValidRoom:
-        appendError("[{}]房间不存在".format(name))
+    if not Common.api.isValidRoom:
+        Common.appendError("[{}]房间不存在".format(Common.config["l_u"]))
         return
     d = None
     t = threading.Thread(target=download)
@@ -116,12 +114,12 @@ def run(name):
     _count = 0
     _count_error = 0
     while True:
-        if api.isLive and not forceNotBroadcasting:
+        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 forceNotDownload:
+            if not t.is_alive() and not Common.forceNotDownload:
                 _count_error += 1
-                _preT = api.playlist
+                _preT = Common.api.playlist
                 t = threading.Thread(target=download, args=(_preT,))
                 t.setDaemon(True)
                 t.start()
@@ -135,29 +133,29 @@ def run(name):
                 et.start()
             if _count % 15 == 0:
                 try:
-                    api.updRoomInfo()
+                    Common.api.updRoomInfo()
                     _count = 0
                     _count_error = 0
                 except Exception as e:
-                    appendError(e.__str__())
+                    Common.appendError(e.__str__())
                     time.sleep(20)
                     _count_error += 1
                     continue
             if _count_error > 15:
-                api.isLive = False
+                Common.api.isLive = False
             _count += 1
             time.sleep(20)
         else:
             if d is not None:
                 d = None
             if not isEncode and not isDownload:
-                uploadQueue.put(True)
+                Common.uploadQueue.put(True)
                 isEncode = True
                 isDownload = True
                 # print("主播未开播,等待1分钟后重试")
             time.sleep(60)
             try:
-                api.updRoomInfo()
+                Common.api.updRoomInfo()
                 _count_error = 0
             except Exception as e:
-                appendError(e.__str__())
+                Common.appendError(e.__str__())
diff --git a/static/device.js b/static/device.js
index ed98191..c3eed6b 100644
--- a/static/device.js
+++ b/static/device.js
@@ -21,4 +21,4 @@ function deviceUpdate(){
 }
 
 deviceUpdate()
-setInterval(deviceUpdate,2000)
+setInterval(deviceUpdate,4000)
diff --git a/static/index.js b/static/index.js
index 123bbaf..952cd80 100644
--- a/static/index.js
+++ b/static/index.js
@@ -11,6 +11,8 @@ function taskUpdate(){
                 $("#forceNotUpload").text(res.data.config.forceNotUpload)
                 $("#forceNotEncode").text(res.data.config.forceNotEncode)
                 $("#updateTime").text(res.data.broadcast.updateTime)
+                $("#encodeQueueSize").text(res.data.encodeQueueSize)
+                $("#uploadQueueSize").text(res.data.uploadQueueSize)
                 $("#download").html(function(){
                     var ret = ""
                     res.data.download.reverse().forEach(function(obj){
@@ -39,6 +41,13 @@ function taskUpdate(){
                     })
                     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>"
+                })
             }
         }
     )
diff --git a/templates/index.html b/templates/index.html
index 761765c..13f0ecd 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -55,10 +55,12 @@
         <tr>
             <td>转码日志</td>
             <td><span id="encode"></span></td>
+            <td>转码队列<span id="encodeQueueSize"></span></td>
         </tr>
         <tr>
             <td>上传日志</td>
             <td><span id="upload"></span></td>
+            <td>上传队列<span id="uploadQueueSize"></span></td>
         </tr>
         <tr>
             <td>错误日志</td>