From ce819185eb06debf5467f34ff8472ebf63682e63 Mon Sep 17 00:00:00 2001 From: Jerry Yan <792602257@qq.com> Date: Sun, 7 Apr 2019 22:26:21 +0800 Subject: [PATCH] support change while running --- Common.py | 10 ++++++++++ WebMain.py | 25 +++++++++++++++++++++++-- bilibili.py | 1 + liveDownloader.py | 9 +++++---- 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/Common.py b/Common.py index 5f7790c..164f060 100644 --- a/Common.py +++ b/Common.py @@ -6,6 +6,14 @@ import json _config_fp = open("config.json","r",encoding="utf8") config = json.load(_config_fp) +_config_fp.close() + + +def reloadConfig(): + global config, _config_fp + _config_fp = open("config.json", "r", encoding="utf8") + config = json.load(_config_fp) + _config_fp.close() dt_format="%Y/%m/%d %H:%M:%S" @@ -14,6 +22,8 @@ streamUrl = "" isBroadcasting = False updateTime = "" +forceStopDownload = False + uploadQueue = queue.Queue() encodeQueue = queue.Queue() diff --git a/WebMain.py b/WebMain.py index 283acc6..713f394 100644 --- a/WebMain.py +++ b/WebMain.py @@ -8,8 +8,8 @@ from liveDownloader import run as RUN app = Flask("liveStatus") app.config['JSON_AS_ASCII'] = False CORS(app, supports_credentials=True) -url_for('static', filename='index.html') -url_for('static', filename='index.js') +# url_for('static', filename='index.html') +# url_for('static', filename='index.js') @app.route("/") @@ -29,9 +29,30 @@ def readConfig(): @app.route("/config", methods=["POST"]) def writeConfig(): # TODO : 完善 + Common.reloadConfig() return jsonify({"message":"ok","code":200,"status":0,"data":request.form}) +@app.route("/encode/insert", methods=["POST"]) +def insertEncode(): + if "filename" in request.form: + 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: + Common.uploadQueue.put(request.form["filename"]) + return jsonify({"message":"ok","code":200,"status":0}) + + +@app.route("/upload/finish", methods=["POST"]) +def finishUpload(): + Common.uploadQueue.put(True) + return jsonify({"message":"ok","code":200,"status":0}) + + @app.route("/stats", methods=["GET"]) def getAllStats(): return jsonify({"message":"ok","code":200,"status":0,"data":{ diff --git a/bilibili.py b/bilibili.py index 6290f8a..4368f2e 100644 --- a/bilibili.py +++ b/bilibili.py @@ -313,6 +313,7 @@ class Bilibili: """ if len(self.videos) == 0: return + 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, diff --git a/liveDownloader.py b/liveDownloader.py index a27679b..8c7b883 100644 --- a/liveDownloader.py +++ b/liveDownloader.py @@ -13,7 +13,7 @@ isDownload = False def download(url): - global isDownload + global isDownload, forceStopDownload path = datetime.strftime(datetime.now(), "%Y%m%d_%H%M.flv") p = requests.get(url, stream=True) if p.status_code != 200: @@ -28,7 +28,7 @@ def download(url): f.write(t) _size = os.path.getsize(path) modifyLastDownloadStatus("Download >{}< @ {:.2f}%".format(path, 100.0 * _size/config["p_s"])) - if _size > config["p_s"]: + if _size > config["p_s"] or forceStopDownload: break modifyLastDownloadStatus("Finished Download >{}<".format(path)) except Exception as e: @@ -40,7 +40,9 @@ def download(url): os.remove(path) return False encodeQueue.put(path) - download(url) + if forceStopDownload: + download(url) + forceStopDownload = False def encode(): @@ -65,7 +67,6 @@ def upload(date=datetime.strftime(datetime.now(), "%Y_%m_%d")): while True: if isinstance(i, bool): if i is True: - appendUploadStatus("[{}]自动投稿中,请稍后".format(config["t_t"].format(date))) b.finishUpload(config["t_t"].format(date), 17, config["tag"], config["des"], source=config["src"], no_reprint=0) b.clear()