forked from nbr23/youtube-dl-server
-
Notifications
You must be signed in to change notification settings - Fork 4
/
youtube-dlc-server.py
137 lines (105 loc) · 4.15 KB
/
youtube-dlc-server.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
from __future__ import unicode_literals
import json
import os
from collections import ChainMap
from operator import itemgetter
from pathlib import Path
from bottle import Bottle, request, static_file, template
from ydl_server import jobshandler, ydlhandler
from ydl_server.config import app_defaults
from ydl_server.logdb import JobsDB, Job, Actions, JobType
app = Bottle()
@app.route(['/', '/index'])
def front_index():
return template('./ydl_server/templates/index.html',
ydl_version=ydlhandler.get_ydl_version())
@app.route('/logs')
def front_logs():
return template('./ydl_server/templates/logs.html',
ydl_version=ydlhandler.get_ydl_version())
@app.route('/finished')
def front_finished():
return template('./ydl_server/templates/finished.html',
ydl_version=ydlhandler.get_ydl_version())
@app.route('/api/finished')
def api_list_finished():
root_dir = Path(app_vars['YDL_OUTPUT_TEMPLATE']).parent
matches = root_dir.glob('*')
files = [{'name': f1.name,
'modified': f1.stat().st_mtime * 1000,
'children': sorted([{
'name': f2.name,
'modified': f2.stat().st_mtime * 1000
} for f2 in f1.iterdir() if not f2.name.startswith('.')] if f1.is_dir() else [], key=itemgetter('modified'), reverse=True)
} for f1 in matches if not f1.name.startswith('.')]
files = sorted(files, key=itemgetter('modified'), reverse=True)
return {
"success": True,
"files": files
}
@app.route('/api/finished/:filename#.*#')
def api_serve_finished_file(filename):
root_dir = Path(app_vars['YDL_OUTPUT_TEMPLATE']).parent
return static_file(filename, root=root_dir, mimetype='application/octet-stream')
@app.route('/static/:filename#.*#')
def server_static(filename):
return static_file(filename, root='./ydl_server/static')
@app.route('/api/downloads/stats', method='GET')
def api_queue_size():
db = JobsDB(readonly=True)
jobs = db.get_all()
return {
"success": True,
"stats": {
"queue": ydlhandler.queue.qsize(),
"pending": len([job for job in jobs if job['status'] == "Pending"]),
"running": len([job for job in jobs if job['status'] == "Running"]),
"completed": len([job for job in jobs if job['status'] == "Completed"]),
"failed": len([job for job in jobs if job['status'] == "Failed"])
}
}
@app.route('/api/downloads', method='GET')
def api_logs():
db = JobsDB(readonly=True)
return json.dumps(db.get_all())
@app.route('/api/downloads', method='DELETE')
def api_logs_purge():
jobshandler.put((Actions.PURGE_LOGS, None))
return {"success": True}
@app.route('/api/downloads', method='POST')
def api_queue_download():
url = request.forms.get("url")
options = {'format': request.forms.get("format")}
if not url:
return {"success": False, "error": "'url' query parameter omitted"}
job = Job(url, Job.PENDING, "", JobType.YDL_DOWNLOAD, request.forms.get("format"), url)
jobshandler.put((Actions.INSERT, job))
print("Added url " + url + " to the download queue")
return {"success": True, "url": url, "options": options}
@app.route('/api/metadata', method='POST')
def api_metadata_fetch():
url = request.forms.get("url")
return ydlhandler.fetch_metadata(url)
@app.route("/api/youtube-dlc/update", method="GET")
def ydl_update():
job = Job("Youtube-dlc Update", Job.PENDING, "", JobType.YDL_UPDATE, None, None)
jobshandler.put((Actions.INSERT, job))
return {"success": True}
JobsDB.check_db_latest()
JobsDB.init_db()
ydlhandler.start()
print("Started download thread")
jobshandler.start(ydlhandler.queue)
print("Started jobs manager thread")
print("Updating youtube-dlc to the newest version")
job = Job("Youtube-dlc Update", Job.PENDING, "", JobType.YDL_UPDATE, None, None)
jobshandler.put((Actions.INSERT, job))
ydlhandler.resume_pending()
app_vars = ChainMap(os.environ, app_defaults)
app.run(host=app_vars['YDL_SERVER_HOST'],
port=app_vars['YDL_SERVER_PORT'],
debug=app_vars['YDL_DEBUG'])
ydlhandler.finish()
jobshandler.finish()
ydlhandler.join()
jobshandler.join()