From 223bd4e6ca2ebb690f7167f74cec4352f2023b10 Mon Sep 17 00:00:00 2001 From: kiss2u Date: Sat, 23 Nov 2024 23:05:12 +0800 Subject: [PATCH] Update app.py --- app.py | 121 +++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 101 insertions(+), 20 deletions(-) diff --git a/app.py b/app.py index ad614fc..95a9328 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,4 @@ -from flask import Flask, request, jsonify -from flask_socketio import SocketIO, emit +from flask import Flask, request, jsonify, render_template_string from yt_dlp import YoutubeDL import os import re @@ -7,16 +6,11 @@ import sys app = Flask(__name__) -app.config['SECRET_KEY'] = 'secret!' -socketio = SocketIO(app) # 配置日志 logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') class CustomLogger: - def __init__(self, sid): - self.sid = sid - def debug(self, msg): logging.debug(msg) @@ -26,16 +20,12 @@ def warning(self, msg): def error(self, msg): logging.error(msg) - def info(self, msg): - socketio.emit('download_progress', {'message': msg}, room=self.sid) - -def progress_hook(d, sid): +def progress_hook(d): if d['status'] == 'downloading': - message = f"\rDownloading: {d['_percent_str']} at {d['_speed_str']}" - socketio.emit('download_progress', {'message': message}, room=sid) + print(f"\rDownloading: {d['_percent_str']}", end="") sys.stdout.flush() -def download_audio(url, output_dir, number, check=False, max_retries=3, sid=None): +def download_audio(url, output_dir, number, check=False, max_retries=3): base_dir = "/mnt/shares/audiobooks/" new_output_dir = os.path.join(base_dir, output_dir) os.makedirs(new_output_dir, exist_ok=True) @@ -67,8 +57,8 @@ def format_title(title): 'noplaylist': True, 'ignoreerrors': True, 'download_archive': os.path.join(new_output_dir, '.downloaded_videos.txt'), - 'logger': CustomLogger(sid), - 'progress_hooks': [lambda d: progress_hook(d, sid)], + 'logger': CustomLogger(), + 'progress_hooks': [progress_hook], } retries = 0 @@ -94,12 +84,11 @@ def download(): output_dir = data.get('output_dir') number = int(data.get('number', 1)) check = data.get('check', False) - sid = request.sid results = [] for i in range(1, number + 1): url_with_param = f"{url}?p={i}" - result = download_audio(url_with_param, output_dir, i, check, sid=sid) + result = download_audio(url_with_param, output_dir, i, check) results.append(result) if "No such playlist" in result: break @@ -108,7 +97,99 @@ def download(): @app.route('/') def home(): - return app.send_static_file('index.html') + return render_template_string(""" + + + + + BiliBili Audio Downloader + + + +

BiliBili Audio Downloader

+
+
+
+
+
+
+
+
+
+ + +
+
+ + + + + + """) if __name__ == '__main__': - socketio.run(app, host='0.0.0.0', port=8080, debug=True) + app.run(host='0.0.0.0', debug=True)