-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
64 lines (50 loc) · 1.5 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
from flask import Flask, render_template, request, redirect
import youtube_dl
from pytubefix import YouTube
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html')
@app.route('/how-to')
def how_to():
return render_template('how-to.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/terms-conditions')
def terms():
return render_template('terms-conditions.html')
@app.route('/download', methods=["POST", "GET"])
def download():
url = request.form["url"]
if "youtube.com" in url or "youtu.be" in url:
download_link = download_youtube_video(url)
else:
try:
url = request.form["url"]
with youtube_dl.YoutubeDL() as ydl:
url = ydl.extract_info(url, download=False)
try:
download_link = url["entries"][-1]["formats"][-1]["url"]
except:
download_link = url["formats"][-1]["url"]
if ".mp4" in download_link:
return redirect(download_link)
else:
return redirect(download_link + "&dl=1")
except:
return render_template('error.html')
if download_link:
return redirect(download_link)
else:
return render_template('error.html')
def download_youtube_video(url):
yt = YouTube(url)
highest_resolution_stream = yt.streams.get_highest_resolution()
highest_resolution_stream.download()
download_link = highest_resolution_stream.url
return download_link
if __name__ == '__main__':
# Use this for local testing
# app.run(port=80, debug=False)
app.run(debug=False, host='0.0.0.0')