-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplugin.py
346 lines (320 loc) · 13.6 KB
/
plugin.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import os
import traceback
import json
from flask import Blueprint, request, jsonify, abort
from framework import app, path_data
from framework.logger import get_logger
from framework.util import Util
from framework.common.plugin import (
get_model_setting,
Logic,
default_route_single_module,
)
class Plugin:
package_name = __name__.split(".", maxsplit=1)[0]
logger = get_logger(package_name)
blueprint = Blueprint(
package_name,
package_name,
url_prefix=f"/{package_name}",
template_folder=os.path.join(os.path.dirname(__file__), "templates"),
static_folder=os.path.join(os.path.dirname(__file__), "static"),
)
# 메뉴 정의
menu = {
"main": [package_name, "youtube-dl"],
"sub": [
["setting", "설정"],
["download", "다운로드"],
["thumbnail", "썸네일 다운로드"],
["sub", "자막 다운로드"],
["list", "목록"],
["log", "로그"],
],
"category": "vod",
}
plugin_info = {
"version": "4.0.1",
"name": package_name,
"category_name": "vod",
"developer": "joyfuI",
"description": "유튜브, 네이버TV 등 동영상 사이트에서 동영상 다운로드",
"home": f"https://github.com/joyfuI/{package_name}",
"more": "",
}
ModelSetting = get_model_setting(package_name, logger)
logic = None
module_list = None
home_module = "list" # 기본모듈
youtube_dl_packages = ["youtube-dl", "yt-dlp"]
def initialize():
try:
app.config["SQLALCHEMY_BINDS"][
Plugin.package_name
] = f"sqlite:///{os.path.join(path_data, 'db', f'{Plugin.package_name}.db')}"
Util.save_from_dict_to_json(
Plugin.plugin_info, os.path.join(os.path.dirname(__file__), "info.json")
)
# 로드할 모듈 정의
from .main import LogicMain
Plugin.module_list = [LogicMain(Plugin)]
Plugin.logic = Logic(Plugin)
default_route_single_module(Plugin)
except Exception as error:
Plugin.logger.error("Exception:%s", error)
Plugin.logger.error(traceback.format_exc())
# API 명세는 https://github.com/joyfuI/youtube-dl#api
@Plugin.blueprint.route("/api/<sub>", methods=["GET", "POST"])
def api(sub):
from .main import LogicMain
from .abort import LogicAbort
try:
Plugin.logger.debug("API: %s, %s", sub, request.values)
plugin = request.values.get("plugin")
if not plugin: # 요청한 플러그인명이 빈문자열이거나 None면
abort(403) # 403 에러(거부)
# 동영상 정보를 반환하는 API
if sub == "info_dict":
url = request.values.get("url")
ret = {"errorCode": 0, "info_dict": None}
if None in (url,):
return LogicAbort.abort(ret, 1) # 필수 요청 변수가 없음
if not url.startswith("http"):
return LogicAbort.abort(ret, 2) # 잘못된 동영상 주소
info_dict = LogicMain.get_info_dict(url, Plugin.ModelSetting.get("proxy"))
if info_dict is None:
return LogicAbort.abort(ret, 10) # 실패
ret["info_dict"] = info_dict
# 비디오 다운로드 준비를 요청하는 API
elif sub == "download":
key = request.values.get("key")
url = request.values.get("url")
filename = request.values.get(
"filename", Plugin.ModelSetting.get("default_filename")
)
save_path = request.values.get(
"save_path", Plugin.ModelSetting.get("save_path")
)
format_code = request.values.get("format", None)
preferedformat = request.values.get("preferedformat", None)
preferredcodec = request.values.get("preferredcodec", None)
preferredquality = request.values.get("preferredquality", 192)
dateafter = request.values.get("dateafter", None)
playlist = request.values.get("playlist", None)
archive = request.values.get("archive", None)
start = request.values.get("start", False)
cookiefile = request.values.get("cookiefile", None)
headers = request.values.get("headers", "null")
ret = {"errorCode": 0, "index": None}
if None in (key, url):
return LogicAbort.abort(ret, 1) # 필수 요청 변수가 없음
if not url.startswith("http"):
return LogicAbort.abort(ret, 2) # 잘못된 동영상 주소
if preferredcodec not in (
None,
"best",
"mp3",
"aac",
"flac",
"m4a",
"opus",
"vorbis",
"wav",
):
return LogicAbort.abort(ret, 5) # 허용되지 않은 값이 있음
if not filename:
filename = LogicMain.get_default_filename()
youtube_dl = LogicMain.download(
plugin=plugin,
url=url,
filename=filename,
temp_path=Plugin.ModelSetting.get("temp_path"),
save_path=save_path,
format=format_code,
preferedformat=preferedformat,
preferredcodec=preferredcodec,
preferredquality=preferredquality,
dateafter=dateafter,
playlist=playlist,
archive=archive,
proxy=Plugin.ModelSetting.get("proxy"),
ffmpeg_path=Plugin.ModelSetting.get("ffmpeg_path"),
key=key,
cookiefile=cookiefile,
headers=json.loads(headers),
)
if youtube_dl is None:
return LogicAbort.abort(ret, 10) # 실패
ret["index"] = youtube_dl.index
if start:
youtube_dl.start()
LogicMain.socketio_emit("add", youtube_dl)
# 썸네일 다운로드 준비를 요청하는 API
elif sub == "thumbnail":
key = request.values.get("key")
url = request.values.get("url")
filename = request.values.get(
"filename", Plugin.ModelSetting.get("default_filename")
)
save_path = request.values.get(
"save_path", Plugin.ModelSetting.get("save_path")
)
all_thumbnails = request.values.get("all_thumbnails", False)
dateafter = request.values.get("dateafter", None)
playlist = request.values.get("playlist", None)
archive = request.values.get("archive", None)
start = request.values.get("start", False)
cookiefile = request.values.get("cookiefile", None)
headers = request.values.get("headers", "null")
ret = {"errorCode": 0, "index": None}
if None in (key, url):
return LogicAbort.abort(ret, 1) # 필수 요청 변수가 없음
if not url.startswith("http"):
return LogicAbort.abort(ret, 2) # 잘못된 동영상 주소
if not filename:
filename = LogicMain.get_default_filename()
youtube_dl = LogicMain.thumbnail(
plugin=plugin,
url=url,
filename=filename,
temp_path=Plugin.ModelSetting.get("temp_path"),
save_path=save_path,
all_thumbnails=all_thumbnails,
dateafter=dateafter,
playlist=playlist,
archive=archive,
proxy=Plugin.ModelSetting.get("proxy"),
ffmpeg_path=Plugin.ModelSetting.get("ffmpeg_path"),
key=key,
cookiefile=cookiefile,
headers=json.loads(headers),
)
if youtube_dl is None:
return LogicAbort.abort(ret, 10) # 실패
ret["index"] = youtube_dl.index
if start:
youtube_dl.start()
LogicMain.socketio_emit("add", youtube_dl)
# 자막 다운로드 준비를 요청하는 API
elif sub == "sub":
key = request.values.get("key")
url = request.values.get("url")
filename = request.values.get(
"filename", Plugin.ModelSetting.get("default_filename")
)
save_path = request.values.get(
"save_path", Plugin.ModelSetting.get("save_path")
)
all_subs = request.values.get("all_subs", False)
sub_lang = request.values.get("sub_lang", "ko")
auto_sub = request.values.get("all_subs", False)
dateafter = request.values.get("dateafter", None)
playlist = request.values.get("playlist", None)
archive = request.values.get("archive", None)
start = request.values.get("start", False)
cookiefile = request.values.get("cookiefile", None)
headers = request.values.get("headers", "null")
ret = {"errorCode": 0, "index": None}
if None in (key, url):
return LogicAbort.abort(ret, 1) # 필수 요청 변수가 없음
if not url.startswith("http"):
return LogicAbort.abort(ret, 2) # 잘못된 동영상 주소
if not filename:
filename = LogicMain.get_default_filename()
youtube_dl = LogicMain.sub(
plugin=plugin,
url=url,
filename=filename,
temp_path=Plugin.ModelSetting.get("temp_path"),
save_path=save_path,
all_subs=all_subs,
sub_lang=sub_lang,
auto_sub=auto_sub,
dateafter=dateafter,
playlist=playlist,
archive=archive,
proxy=Plugin.ModelSetting.get("proxy"),
ffmpeg_path=Plugin.ModelSetting.get("ffmpeg_path"),
key=key,
cookiefile=cookiefile,
headers=json.loads(headers),
)
if youtube_dl is None:
return LogicAbort.abort(ret, 10) # 실패
ret["index"] = youtube_dl.index
if start:
youtube_dl.start()
LogicMain.socketio_emit("add", youtube_dl)
# 다운로드 시작을 요청하는 API
elif sub == "start":
index = request.values.get("index")
key = request.values.get("key")
ret = {"errorCode": 0, "status": None}
if None in (index, key):
return LogicAbort.abort(ret, 1) # 필수 요청 변수가 없음
index = int(index)
if not 0 <= index < len(LogicMain.youtube_dl_list):
return LogicAbort.abort(ret, 3) # 인덱스 범위를 벗어남
youtube_dl = LogicMain.youtube_dl_list[index]
if youtube_dl.key != key:
return LogicAbort.abort(ret, 4) # 키가 일치하지 않음
ret["status"] = youtube_dl.status.name
if not youtube_dl.start():
return LogicAbort.abort(ret, 10) # 실패
# 다운로드 중지를 요청하는 API
elif sub == "stop":
index = request.values.get("index")
key = request.values.get("key")
ret = {"errorCode": 0, "status": None}
if None in (index, key):
return LogicAbort.abort(ret, 1) # 필수 요청 변수가 없음
index = int(index)
if not 0 <= index < len(LogicMain.youtube_dl_list):
return LogicAbort.abort(ret, 3) # 인덱스 범위를 벗어남
youtube_dl = LogicMain.youtube_dl_list[index]
if youtube_dl.key != key:
return LogicAbort.abort(ret, 4) # 키가 일치하지 않음
ret["status"] = youtube_dl.status.name
if not youtube_dl.stop():
return LogicAbort.abort(ret, 10) # 실패
# 현재 상태를 반환하는 API
elif sub == "status":
index = request.values.get("index")
key = request.values.get("key")
ret = {
"errorCode": 0,
"status": None,
"type": None,
"start_time": None,
"end_time": None,
"temp_path": None,
"save_path": None,
}
if None in (index, key):
return LogicAbort.abort(ret, 1) # 필수 요청 변수가 없음
index = int(index)
if not 0 <= index < len(LogicMain.youtube_dl_list):
return LogicAbort.abort(ret, 3) # 인덱스 범위를 벗어남
youtube_dl = LogicMain.youtube_dl_list[index]
if youtube_dl.key != key:
return LogicAbort.abort(ret, 4) # 키가 일치하지 않음
ret["status"] = youtube_dl.status.name
ret["type"] = youtube_dl.type
ret["start_time"] = (
youtube_dl.start_time.strftime("%Y-%m-%dT%H:%M:%S")
if youtube_dl.start_time is not None
else None
)
ret["end_time"] = (
youtube_dl.end_time.strftime("%Y-%m-%dT%H:%M:%S")
if youtube_dl.end_time is not None
else None
)
ret["temp_path"] = youtube_dl.temp_path
ret["save_path"] = youtube_dl.save_path
return jsonify(ret)
except Exception as error:
Plugin.logger.error("Exception:%s", error)
Plugin.logger.error(traceback.format_exc())
logger = Plugin.logger
initialize()