-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
75 lines (55 loc) · 2.28 KB
/
app.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
from flask import Flask , request, render_template
from threading import Thread
import youtube_dl
from flask_socketio import SocketIO, emit
import os
app = Flask(__name__)
app.config['SECRET_KEY'] = os.getenv("secret")
socketio = SocketIO(app)
user_sess = {}
@socketio.on('bg-job')
def handle_json(message):
if (vidurl := message.get('vidurl')) != None:
with youtube_dl.YoutubeDL({'outtmpl': '%(title)s.%(ext)s'}) as ydl:
filename = ydl.extract_info(vidurl, download=False)['title']
if filename in user_sess:
ext = '.' + user_sess[filename]['ext']
if float(user_sess[filename]['percent']) > 99:
resp = {"current": 99.99, "total": 100, "status":filename + ext, "result":42, "finished":"True", "size":user_sess[filename]['size']}
emit('response',resp)
else:
resp = {"current": user_sess[filename]['percent'], "total": 100, "status":filename + ext, "result":42}
emit('response',resp)
def hook(d):
global user_sess
filename = d['filename'].replace("static/","").strip().split('.')[0]
if filename not in user_sess:
user_sess[filename] = {'ext':'', 'size':0, 'percent':0}
user_sess[filename]['ext'] = d['filename'].replace("static/","").strip().split('.')[1]
if d['status'] == 'finished':
user_sess[filename]['size'] = d["_total_bytes_str"]
if d['status'] == 'downloading':
user_sess[filename]['percent'] = d['_percent_str'].replace('%','').strip()
#Long_wrok
def work(plink):
ydl_opts = {
'outtmpl': 'static/%(title)s.%(ext)s',
'format':'22/best',
'restrict-filenames':True,
'noplaylist': True,
'progress_hooks': [hook]
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([plink])
@app.route('/' , methods=['GET', 'POST'])
def stream():
if request.method == 'POST':
plink = request.form['vidurl']
thread = Thread(target=work,args=(plink,))
thread.daemon = True
thread.start()
return render_template('index.html')
if request.method == 'GET':
return render_template('index.html')
if __name__ == '__main__':
socketio.run(app,debug=True)