You've already forked XiguaLiveDanmakuHelper
							
							添加强制清理功能,添加百度云上传功能,部分逻辑修改
This commit is contained in:
		
							
								
								
									
										63
									
								
								Common.py
									
									
									
									
									
								
							
							
						
						
									
										63
									
								
								Common.py
									
									
									
									
									
								
							| @@ -1,24 +1,27 @@ | ||||
| import os | ||||
| import queue | ||||
| from datetime import datetime | ||||
| from glob import glob | ||||
|  | ||||
| import psutil | ||||
| from api import XiGuaLiveApi | ||||
| import json | ||||
| from bilibili import Bilibili | ||||
| import threading | ||||
| from bypy import ByPy | ||||
|  | ||||
| _config_fp = open("config.json","r",encoding="utf8") | ||||
| _config_fp = open("config.json", "r", encoding="utf8") | ||||
| config = json.load(_config_fp) | ||||
| _config_fp.close() | ||||
|  | ||||
| _do_move_time = datetime.now() | ||||
| bypy = ByPy() | ||||
| doCleanTime = datetime.now() | ||||
|  | ||||
| network = { | ||||
|     "currentTime": datetime.now(), | ||||
|     "out":{ | ||||
|         "currentByte":psutil.net_io_counters().bytes_sent, | ||||
|     "out": { | ||||
|         "currentByte": psutil.net_io_counters().bytes_sent, | ||||
|     }, | ||||
|     "in":{ | ||||
|     "in": { | ||||
|         "currentByte": psutil.net_io_counters().bytes_recv, | ||||
|     } | ||||
| } | ||||
| @@ -28,10 +31,10 @@ def updateNetwork(): | ||||
|     global network | ||||
|     network = { | ||||
|         "currentTime": datetime.now(), | ||||
|         "out":{ | ||||
|             "currentByte":psutil.net_io_counters().bytes_sent, | ||||
|         "out": { | ||||
|             "currentByte": psutil.net_io_counters().bytes_sent, | ||||
|         }, | ||||
|         "in":{ | ||||
|         "in": { | ||||
|             "currentByte": psutil.net_io_counters().bytes_recv, | ||||
|         } | ||||
|     } | ||||
| @@ -43,16 +46,30 @@ def getTimeDelta(a, b): | ||||
|     return sec+(ms/100000.0) | ||||
|  | ||||
|  | ||||
| def _doClean(): | ||||
|     global _do_move_time | ||||
| def _doClean(_force=False): | ||||
|     global doCleanTime | ||||
|     _disk = psutil.disk_usage(".") | ||||
|     if _disk.percent > config["max"] and getTimeDelta(datetime.now(), _do_move_time) > 7200: | ||||
|         _do_move_time = datetime.now() | ||||
|         os.system(config["dow"]) | ||||
|     if (_disk.percent > config["max"] and getTimeDelta(datetime.now(), doCleanTime) > 7200) or _force: | ||||
|         doCleanTime = datetime.now() | ||||
|         _list = sorted(glob("*.flv"), key=lambda x: datetime.utcfromtimestamp(os.path.getmtime(x))) | ||||
|         for _i in _list: | ||||
|             if not os.path.exists(_i): | ||||
|                 break | ||||
|             doCleanTime = datetime.now() | ||||
|             if (datetime.now() - datetime.utcfromtimestamp(os.path.getmtime(_i))).days > config["exp"]: | ||||
|                 if config["dow"] == "bypy": | ||||
|                     _res = bypy.upload(_i) | ||||
|                     if _res == 0: | ||||
|                         os.remove(_i) | ||||
|                 else: | ||||
|                     os.system(config["dow"]) | ||||
|             else: | ||||
|                 break | ||||
|             doCleanTime = datetime.now() | ||||
|  | ||||
|  | ||||
| def doClean(): | ||||
|     p = threading.Thread(target=_doClean) | ||||
| def doClean(_force=False): | ||||
|     p = threading.Thread(target=_doClean, args=(_force,)) | ||||
|     p.setDaemon(True) | ||||
|     p.start() | ||||
|  | ||||
| @@ -63,13 +80,13 @@ def getCurrentStatus(): | ||||
|     _delta= getTimeDelta(datetime.now(),network["currentTime"]) | ||||
|     _net  = psutil.net_io_counters() | ||||
|     if 60 > _delta > 0: | ||||
|         _inSpeed = (_net.bytes_recv - network["in"]["currentByte"])/_delta | ||||
|         _outSpeed = (_net.bytes_sent - network["out"]["currentByte"])/_delta | ||||
|         _inSpeed = (_net.bytes_recv - network["in"]["currentByte"]) / _delta | ||||
|         _outSpeed = (_net.bytes_sent - network["out"]["currentByte"]) / _delta | ||||
|     else: | ||||
|         _outSpeed = 0 | ||||
|         _inSpeed = 0 | ||||
|     updateNetwork() | ||||
|     if getTimeDelta(datetime.now(), _do_move_time) > 3600: | ||||
|     if getTimeDelta(datetime.now(), doCleanTime) > 3600: | ||||
|         doClean() | ||||
|     return { | ||||
|         "memTotal": parseSize(_mem.total), | ||||
| @@ -81,6 +98,8 @@ def getCurrentStatus(): | ||||
|         "cpu": psutil.cpu_percent(), | ||||
|         "outSpeed": parseSize(_outSpeed), | ||||
|         "inSpeed": parseSize(_inSpeed), | ||||
|         "doCleanTime": doCleanTime, | ||||
|         "fileExpire": config["exp"], | ||||
|     } | ||||
|  | ||||
|  | ||||
| @@ -91,7 +110,7 @@ def reloadConfig(): | ||||
|     _config_fp.close() | ||||
|  | ||||
|  | ||||
| dt_format="%Y/%m/%d %H:%M:%S" | ||||
| dt_format = "%Y/%m/%d %H:%M:%S" | ||||
|  | ||||
| broadcaster = "" | ||||
| streamUrl = "" | ||||
| @@ -130,9 +149,9 @@ def appendOperation(obj): | ||||
|  | ||||
|  | ||||
| def parseSize(size): | ||||
|     K = size/1024.0 | ||||
|     K = size / 1024.0 | ||||
|     if K > 1000: | ||||
|         M = K/1024.0 | ||||
|         M = K / 1024.0 | ||||
|         if M > 1000: | ||||
|             return "{:.2f}GB".format(M / 1024.0) | ||||
|         else: | ||||
|   | ||||
							
								
								
									
										10
									
								
								WebMain.py
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								WebMain.py
									
									
									
									
									
								
							| @@ -23,8 +23,6 @@ def index(): | ||||
| def readConfig(): | ||||
|     config = Common.config.copy() | ||||
|     config.pop("b_p") | ||||
|     config.pop("mtd") | ||||
|     config.pop("del") | ||||
|     config.pop("mv") | ||||
|     return jsonify(config) | ||||
|  | ||||
| @@ -88,6 +86,14 @@ def toggleForceStartUploadThread(): | ||||
|     }}) | ||||
|  | ||||
|  | ||||
| @app.route("/force/start/clean", methods=["POST"]) | ||||
| def toggleForceStartUploadThread(): | ||||
|     Common.doClean(True) | ||||
|     Common.appendOperation("强制执行清理程序") | ||||
|     return jsonify({"message":"ok","code":200,"status":0,"data":{ | ||||
|     }}) | ||||
|  | ||||
|  | ||||
| @app.route("/encode/insert", methods=["POST"]) | ||||
| def insertEncode(): | ||||
|     if "filename" in request.form and os.path.exists(request.form["filename"]): | ||||
|   | ||||
| @@ -67,10 +67,6 @@ def encode(): | ||||
|             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) | ||||
|  | ||||
|  | ||||
| def upload(date=datetime.strftime(datetime.now(), "%Y_%m_%d")): | ||||
|   | ||||
| @@ -15,6 +15,8 @@ function deviceUpdate(){ | ||||
|                 $("#cpuP").val(res.data.status.cpu) | ||||
|                 $("#inSpeed").text(res.data.status.inSpeed) | ||||
|                 $("#outSpeed").text(res.data.status.outSpeed) | ||||
|                 $("#doCleanTime").text(res.data.status.doCleanTime) | ||||
|                 $("#fileExpire").text(res.data.status.fileExpire) | ||||
|             } | ||||
|         } | ||||
|     ) | ||||
|   | ||||
| @@ -20,5 +20,10 @@ | ||||
|             <td>↓ <span id="inSpeed"></span>/s</td> | ||||
|             <td>↑ <span id="outSpeed"></span>/s</td> | ||||
|         </tr> | ||||
|         <tr> | ||||
|             <td class='title'>文件清理时间</td> | ||||
|             <td>@ <span id="doCleanTime"></span></td> | ||||
|             <td>清理超过<span id="fileExpire"></span>天的文件</td> | ||||
|         </tr> | ||||
|     </table> | ||||
|     <script src="/static/device.js"></script> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user