-
Notifications
You must be signed in to change notification settings - Fork 9
/
webui_interface.py
248 lines (217 loc) · 6.75 KB
/
webui_interface.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
from flask import (
Flask,
request,
jsonify,
send_from_directory,
render_template,
url_for,
redirect,
)
import os
import requests
import m3u8dl
import sys
import json
import os
import cProfile
import time
import utils
from queue import Queue, Empty
import uuid
import threading
import ctypes
import multiprocessing
import webbrowser
app = Flask(__name__, static_folder="webui")
"""
{
"url":
"output":
"name":
"cur":
"tot":
"uuid":
"canceled":
"merge_status":
"download_type":
"download_audio": bool
"audio_url":
}
"""
all_task_status = []
"""
{
"uuid"
}
"""
task_queue = Queue()
def find_all_task_by_uuid(uuid):
for id, task in enumerate(all_task_status):
if task["uuid"] == uuid:
return task, id
return None
g_father_queue = None
current_task_uuid = ""
def executor_progress_callback(cur, tot, merge_status):
global g_father_queue, current_task_uuid
g_father_queue.put(
{
"uuid": current_task_uuid,
"cur": cur,
"tot": tot,
"merge_status": merge_status,
}
)
# print({
# "uuid": current_task_uuid,
# "cur": cur,
# "tot": tot,
# "merge_status": merge_status
# })
return False
def execute_one_download_task_worker(task_dict, father_queue):
global current_task_uuid, g_father_queue
print(f"downloading task {task_dict}")
current_task_uuid = task_dict["uuid"]
uuid = task_dict["uuid"]
url = task_dict["url"]
output = task_dict["output"]
name = task_dict["name"]
g_father_queue = father_queue
m3u8dl.M3u8Download(url, output, name, progress_callback=executor_progress_callback)
if task_dict["download_audio"]:
audio_url = task_dict["audio_url"]
if audio_url:
print("Downloading audio...")
utils.download_audio(audio_url, output, name)
print("Download audio successfully.")
return
def execute_tasks():
global all_task_status
queue = multiprocessing.Queue()
while True:
try:
task = task_queue.get(timeout=1)
task_uuid = task["uuid"]
task_obj, task_id = find_all_task_by_uuid(task_uuid)
if task_obj["canceled"] == True:
all_task_status.pop(task_id)
continue
process = multiprocessing.Process(
target=execute_one_download_task_worker, args=(task_obj, queue)
)
process.start()
while True:
if all_task_status[task_id]["canceled"]:
print("task canceled, terminate subprocess...")
process.terminate()
all_task_status.pop(task_id)
break
try:
msg = queue.get_nowait()
update_obj, update_id = find_all_task_by_uuid(msg["uuid"])
all_task_status[update_id]["cur"] = msg["cur"]
all_task_status[update_id]["tot"] = msg["tot"]
all_task_status[update_id]["merge_status"] = msg["merge_status"]
except Empty:
if process.is_alive() == False:
break
time.sleep(0.1)
continue
except TypeError:
continue
except Empty:
continue
except TypeError:
continue
@app.route("/")
def index():
auth = utils.read_auth()
return render_template(
"index.html",
auth=auth,
auth_prompt="" if auth else "。".join(utils.auth_prompt()),
)
@app.route("/get_course")
def get_course():
course_id = request.args.get("course_id")
auth = request.args.get("auth")
if auth:
utils.write_auth(auth)
if not utils.test_auth(courseID=course_id):
utils.remove_auth()
return jsonify({"code": 403, "msg": "。".join(utils.auth_prompt(False))})
try:
videoList, courseName, professor = utils.get_course_info(courseID=course_id)
except:
return jsonify({"videoList": [], "courseName": "", "professor": ""})
return jsonify(
{"videoList": videoList, "courseName": courseName, "professor": professor}
)
@app.route("/new_task", methods=["POST"])
def new_task():
global task_queue, all_task_status
data = request.json
course_id = data["course_id"]
course_number = data["course_number"]
download_version = data["download_version"]
download_audio = data["download_audio"]
videoList, courseName, professor = utils.get_course_info(courseID=course_id)
course_number_arr = course_number.split(",")
ret_id = []
for courseNum in course_number_arr:
courseNumT = int(courseNum)
c = videoList[courseNumT]
name = courseName + "-" + professor + "-" + c["title"]
print(name)
cur_uuid = str(uuid.uuid4())
ret_id.append(cur_uuid)
task_status = {
"url": "",
"output": "",
"name": name,
"cur": 0,
"tot": 0,
"uuid": cur_uuid,
"canceled": False,
"merge_status": 0,
"download_type": download_version,
"download_audio": download_audio == "1",
"audio_url": "",
}
task_status["audio_url"] = utils.get_audio_url(c["video_ids"][0])
if download_version == "2":
print("Downloading screen...")
task_status["url"] = c["videos"][0]["vga"]
task_status["output"] = "output/" + courseName + "-screen"
else:
print("Downloading video...")
task_status["url"] = c["videos"][0]["main"]
task_status["output"] = "output/" + courseName + "-video"
all_task_status.append(task_status)
task_queue.put({"uuid": cur_uuid})
return jsonify({"status": "success", "task_id": ret_id})
@app.route("/get_status")
def get_status():
global all_task_status
return jsonify(all_task_status)
@app.route("/kill_task")
def kill_task():
global all_task_status
uuid = request.args.get("uuid")
task, id = find_all_task_by_uuid(uuid)
if task["merge_status"] == 2:
# if already finished
all_task_status.pop(id)
return jsonify({"status": "ok"})
all_task_status[id]["canceled"] = True
return jsonify({"status": "ok"})
@app.route("/<path:path>")
def static_files(path):
return send_from_directory(app.static_folder, path)
if __name__ == "__main__":
multiprocessing.freeze_support()
t = threading.Thread(target=execute_tasks)
t.start()
webbrowser.open("http://127.0.0.1:5001/")
app.run(debug=False, host="0.0.0.0", use_reloader=False, port=5001)