From f1b4a878a56d94ddd721883ca4c7a54577e971a9 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Mon, 29 Apr 2019 09:50:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BC=BA=E5=88=B6=E6=B8=85?= =?UTF-8?q?=E7=90=86=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=B7=BB=E5=8A=A0=E7=99=BE?= =?UTF-8?q?=E5=BA=A6=E4=BA=91=E4=B8=8A=E4=BC=A0=E5=8A=9F=E8=83=BD=EF=BC=8C?= =?UTF-8?q?=E9=83=A8=E5=88=86=E9=80=BB=E8=BE=91=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common.py | 63 ++++++++++++++++++++++++++++--------------- WebMain.py | 10 +++++-- liveDownloader.py | 4 --- static/device.js | 2 ++ templates/device.html | 5 ++++ 5 files changed, 56 insertions(+), 28 deletions(-) diff --git a/Common.py b/Common.py index 5e06ed4..4628cfc 100644 --- a/Common.py +++ b/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: diff --git a/WebMain.py b/WebMain.py index bd856b7..1516464 100644 --- a/WebMain.py +++ b/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"]): diff --git a/liveDownloader.py b/liveDownloader.py index 5eb5aad..a87f7d0 100644 --- a/liveDownloader.py +++ b/liveDownloader.py @@ -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")): diff --git a/static/device.js b/static/device.js index c3eed6b..2d311b8 100644 --- a/static/device.js +++ b/static/device.js @@ -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) } } ) diff --git a/templates/device.html b/templates/device.html index 0c8783e..6007c62 100644 --- a/templates/device.html +++ b/templates/device.html @@ -20,5 +20,10 @@ ↓ /s ↑ /s + + 文件清理时间 + @ + 清理超过天的文件 +