-
Notifications
You must be signed in to change notification settings - Fork 1
/
run.py
55 lines (42 loc) · 1.41 KB
/
run.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
from __future__ import unicode_literals
import os
import youtube_dl
from flask import Flask, render_template, request
import config
app = Flask(__name__)
app.secret_key = config.flask_key
class MyLogger(object):
def debug(self, msg):
pass
def warning(self, msg):
pass
def error(self, msg):
print(msg)
def my_hook(d):
if d['status'] == 'finished':
print('Done downloading, now converting ...')
@app.route('/', methods=['GET'])
def index():
return render_template('index.html')
@app.route('/download', methods=['POST', 'GET'])
def download():
if request.method == 'POST':
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'outtmpl': os.path.join(config.download_folder, "%(title)s-%(id)s.%(ext)s"),
'logger': MyLogger(),
'progress_hooks': [my_hook],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([request.form["url"]])
else:
return render_template('index.html')
return render_template('index.html')
if __name__ == '__main__':
os.makedirs(os.path.join(os.path.dirname(os.path.abspath(__file__)), config.download_folder), exist_ok=True)
app.run(host=config.host, port=config.port, debug=config.debug)