forked from QiuChenlyOpenSource/MusicDownload
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.py
More file actions
165 lines (129 loc) · 3.63 KB
/
Copy pathApp.py
File metadata and controls
165 lines (129 loc) · 3.63 KB
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
# Copyright (c) 2023. 秋城落叶, Inc. All Rights Reserved
# @作者 : 秋城落叶(QiuChenly)
# @邮件 : qiuchenly@outlook.com
# @文件 : 项目 [qqmusic] - App.py
# @修改时间 : 2023-07-28 10:52:00
# @上次修改 : 2023/7/28 下午10:52
import json
import os.path
import time
from concurrent.futures import Future
from flask import Flask, request
from flask_cors import CORS
from flask_socketio import SocketIO, emit
from flaskSystem.src.Common.Concurrency import Downloader
app: Flask = Flask(__name__, static_url_path="")
CORS(app, resources=r'/*')
@app.get("/")
def index():
"""
返回主页面
Returns:
"""
return app.redirect("/index.html")
@app.get("/status")
def appState():
return {
'code': 200
}
@app.post("/config")
def configSave():
jsn = request.get_json()
num = int(jsn['num'])
location = jsn['folder']
savelyric = jsn['lyric']
save_config(location,num,savelyric)
return {
'code': 200
}
def save_config(location,num,savelyric):
c.set_folder(location)
c.initPool(num)
c.set_lyric(savelyric)
with open("config.cfg", "w+") as cfg:
cfg.write(json.dumps({
"thread_num": num,
"download_locate": location,
"save_lyric": savelyric
}))
cfg.flush()
@app.get("/getConfig")
def getConfig():
return {
'num': c.getCurrentResize(),
'folder': c.get_folder(),
'lyric': c.get_lyric()
}
@app.post("/download")
def add():
from flaskSystem.src.Common.Tools import downSingle
print("准备开始下载任务")
jsn = request.get_json()
music = jsn['music']
config = jsn['config']
# downSingle(jsn)
c.addTask(done, downSingle, music, c.get_folder(), config)
return {
'code': 200
}
c = Downloader()
def done(ret: Future):
"""
得到下载返回结果
Args:
ret:
Returns:
"""
excepts = ret.exception()
ret = ret.result()
if ret['code'] != 200 or excepts:
print(f"下载失败," + ret['msg'], excepts)
else:
print(f"下载成功。")
def executeFn(a1: str, a2: bool):
"""
测试线程池函数 实际上没有被调用
Args:
a1:
a2:
Returns:
"""
time.sleep(4)
return a1 + "a2 True" if a2 else "a2 False"
# SocketIO
socketio = SocketIO()
socketio.init_app(app, cors_allowed_origins='*')
namespace = "qiuchenly"
@socketio.on('connect',namespace=namespace)
def connected_msg(socket):
print(f"Client connected",socket)
@socketio.on('disconnect', namespace=namespace)
def disconnect_msg():
print('client disconnected.')
@socketio.on('my_event', namespace=namespace)
def mtest_message(message):
print(message)
emit('my_response', {'data': message['data'], 'count': 1})
@app.after_request
def add_header(response):
if 'text/plain' in response.headers['Content-Type']:
response.headers['Content-Type'] = 'application/javascript; charset=utf-8'
return response
def Start(port):
c.initPool(16)
if os.path.exists("config.cfg"):
with open("config.cfg", "r") as cfg:
cfg = cfg.read()
cfg = json.loads(cfg)
c.initPool(int(cfg['thread_num']))
if os.path.exists(cfg['download_locate']):
c.set_folder(cfg['download_locate'])
else:
print("路径不存在,使用默认下载目录:", c.get_folder())
save_config(c.get_folder(),c.getCurrentResize())
app.run(
'0.0.0.0',
port,
debug=False
)
socketio.run(app, host='0.0.0.0', port=port)