-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.py
109 lines (87 loc) · 4.87 KB
/
main.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
import logging, ydl_utils, params, process_utils
from urllib.parse import urlparse, unquote
from fastapi import BackgroundTasks, FastAPI, Response
import yt_dlp as youtube_dl
app = FastAPI()
@app.get(params.api_route)
async def download_request(response : Response, background_tasks : BackgroundTasks, url, token = None,
format = None, subtitles = None, location = None, filename = None, presets = None):
decoded_url = unquote(url)
decoded_presets = [] # from string to list
selected_presets_objects = [] # store presets objects required by the presets field
if presets is not None:
decoded_presets = presets.split(',')
selected_presets_objects = ydl_utils.existing_presets(decoded_presets) # transform string in object
user = None
if params.enable_users_management and token is not None:
user = ydl_utils.find_associated_user(unquote(token))
if params.enable_users_management and user is None:
logging.warning(f'An unauthorized user tried to download {decoded_url}')
response.status_code = 401 # unauthorized
return {'status_code' : response.status_code}
query_parameters = { # parameters object build form url query parameters
'format' : unquote(format) if format is not None else None,
'subtitles' : unquote(subtitles) if subtitles is not None else None,
'location' : unquote(location) if location is not None else None,
'filename' : unquote(filename) if filename is not None else None,
'presets' : unquote(presets) if presets is not None else None
}
# override location setting of the preset for the current user
if params.enable_users_management and user.get('force_location') is not None :
query_parameters['location'] = user.get('force_location')
# generate all options sets for all download
downloads_options_sets = ydl_utils.generate_ydl_options_sets(decoded_url, selected_presets_objects, query_parameters, user)
# count the number of check downloads and the number of errors
validity_check = ydl_utils.recap_all_downloads_validity(downloads_options_sets)
# if all downloads were checked and without errors, we can ensure the file will be correctly downloaded
if validity_check.get('checked') == validity_check.get('total') and validity_check.get('errors') == 0:
background_tasks.add_task(ydl_utils.launch_downloads, decoded_url, downloads_options_sets)
response.status_code = 200 # request ok
# if not all downloads were checked, we can't ensure all files will be correctly downloaded
elif validity_check.get('checked') != validity_check.get('total'):
background_tasks.add_task(ydl_utils.launch_downloads, decoded_url, downloads_options_sets)
response.status_code = 202 # request ok but result not granted
# if all downloads are in error, we can ensure no file will be downloaded
else:
logging.error(f'Impossible to download \'{decoded_url}\'')
response.status_code = 400 # bad request
return {
'status_code' : response.status_code,
'url' : decoded_url,
'presets_errors' : (len(decoded_presets) - len(selected_presets_objects)),
'list' : downloads_options_sets,
'youtube-dl_version' : youtube_dl.version.__version__
}
@app.get(params.api_route_info)
async def info_request():
return {
'state' : 'started',
'ydl_version' : youtube_dl.version.__version__
}
@app.get(params.api_route_active_downloads)
async def active_downloads_request(response : Response, token = None):
user = None
if params.enable_users_management and token is not None:
user = ydl_utils.find_associated_user(unquote(token))
if params.enable_users_management and user is None:
response.status_code = 401 # unauthorized
return {'status_code' : response.status_code}
return process_utils.get_active_downloads_list()
@app.get(f'{params.api_route_active_downloads}/terminate')
async def terminate_all_active_downloads(response : Response, token = None):
user = None
if params.enable_users_management and token is not None:
user = ydl_utils.find_associated_user(unquote(token))
if params.enable_users_management and user is None:
response.status_code = 401 # unauthorized
return {'status_code' : response.status_code}
return process_utils.terminate_all_active_downloads()
@app.get(f'{params.api_route_active_downloads}/terminate/{"{pid}"}')
async def terminate_active_download(response : Response, pid, token = None):
user = None
if params.enable_users_management and token is not None:
user = ydl_utils.find_associated_user(unquote(token))
if params.enable_users_management and user is None:
response.status_code = 401 # unauthorized
return {'status_code' : response.status_code}
return process_utils.terminate_active_download(unquote(pid))