-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
71 lines (52 loc) · 1.99 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
from __future__ import unicode_literals
from flask import Flask, render_template, request, send_from_directory, current_app
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired
import requests
import youtube_dl
import os
class SearchBarForm(FlaskForm):
search_term = StringField('SearchTerm', validators=[DataRequired()])
submit = SubmitField('Search')
app = Flask(__name__)
app.config['SECRET_KEY'] = os.getenv("secret_key")
api_key = os.getenv("api_key")
@app.route('/', methods=['GET', 'POST'])
def index():
form = SearchBarForm()
if form.is_submitted():
search_term = form.search_term.data
search_result = requests.get(
f'https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=42&q={search_term}&key={api_key}')
items = search_result.json()['items']
return render_template('results.html', result=items)
return render_template('index.html', form=form)
@app.route('/result', methods=['GET', 'POST'])
def result():
return render_template('results.html')
@app.route('/download', methods=['GET', 'POST'])
def download():
url = request.form['url']
ydl_opts = {
'writethumbnail': True,
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192'},
{'key': 'EmbedThumbnail'}],
'call_home': False,
'progress_hooks': [pg_hook],
'outtmpl': '%(title)s.%(ext)s'
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=True)
filename = ydl.prepare_filename(info)
file = filename[:-4] + "mp3"
return send_from_directory(directory=current_app.root_path, filename=file, as_attachment=True)
def pg_hook(d):
if d['status'] == 'finished':
print('Done downloading, now converting ...')
if __name__ == '__main__':
app.run(host='0.0.0.0')