-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.py
136 lines (97 loc) · 3.68 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
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
from flask import Flask, render_template, request, redirect, url_for, send_file
from pytube import YouTube
import time as tim
import os
app = Flask(__name__)
# Homepage
@app.route("/", methods = ['POST', 'GET'])
def homepage():
return render_template("index.html")
# URL input
@app.route("/youtube/api", methods = ['POST', 'GET'])
def youtube():
try:
global file_type
global link_input
global title
global video1
global video2
if request.method == "POST":
file_type = request.form.get("file_type")
link_input = request.form.get("link_input")
video = YouTube(link_input)
thumbnail_url = YouTube(link_input).thumbnail_url
title = YouTube(link_input).title
description = YouTube(link_input).description
author = YouTube(link_input).author
length = tim.strftime("%H:%M:%S", tim.gmtime(YouTube(link_input).length))
time = str(YouTube(link_input).publish_date)
videos = video.streams.filter(progressive=True)
video1 = videos[1]
video2 = videos[2]
video1_filesize = int(video1.filesize/1048576)
video2_filesize = int(video2.filesize/1048576)
if file_type == "mp3":
return render_template("index2.html",
thumbnail_url = thumbnail_url,
title = title,
description = description[:300],
author = author,
time = time[:10],
length = length,)
return render_template("index1.html",
thumbnail_url = thumbnail_url,
title = title,
description = description[:300],
author = author,
time = time[:10],
length = length,
video1 = video1,
video2 = video2,
video1_filesize = video1_filesize,
video2_filesize = video2_filesize)
except:
return render_template("error.html")
# Download just sound
@app.route("/downloadmp3", methods = ['POST', 'GET'])
def downloadmp3():
try:
global file_type
global link_input
global title
video = YouTube(link_input).streams.filter(only_audio=True).first()
video = video.download(filename=title+".mp3")
return send_file(video, as_attachment=True)
except:
return render_template("error.html")
finally:
os.remove(video)
# 360P Video Download
@app.route("/download360p", methods = ['POST', 'GET'])
def download360p():
try:
global file_type
global link_input
global title
video = video1.download(filename=title+"_360p"+".mp4")
return send_file(video, as_attachment=True, download_name=title+"_360p", mimetype="video/mp4")
except:
return render_template("error.html")
finally:
os.remove(video)
# 720P Video Download
@app.route("/download720p", methods = ['POST', 'GET'])
def download720p():
try:
global file_type
global link_input
global title
global video2
video = video2.download(filename=title+"_720p"+".mp4")
return send_file(video, as_attachment=True, download_name=title+"_720p", mimetype="video/mp4")
except:
return render_template("error.html")
finally:
os.remove(video)
if __name__ == "__main__":
app.run(debug=True)