-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapplication.py
More file actions
57 lines (46 loc) · 1.5 KB
/
Copy pathapplication.py
File metadata and controls
57 lines (46 loc) · 1.5 KB
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
from __future__ import unicode_literals
from flask import Flask, request, render_template, send_file
from werkzeug.utils import secure_filename
import helper
import logging
from pytube import YouTube
application = app = Flask(__name__)
@app.route("/", methods=["GET", "POST"])
def converter():
# Needs to be global for the /download route
global youtube_url, video_details
user_url = ""
youtube_url = ""
video_details = {}
helper.delete()
if request.method == "POST" and "youtube_link" in request.form:
user_url = str(request.form.get("youtube_link")).strip()
video_details = helper.webscrapper(user_url)
youtube_url = video_details.get("video_url")
return render_template(
"index.html",
video_details=video_details
)
@app.route("/download", methods=["GET", "POST"])
def download():
try:
try:
download_path = (
YouTube(youtube_url)
.streams.get_audio_only()
.download(filename=f"{video_details.get('video_title')}.mp3")
)
except:
download_path = (
YouTube(youtube_url)
.streams.get_audio_only()
.download(filename=f"{video_details.get('video_id')}.mp3")
)
fname = download_path.split("//")[-1]
return send_file(
fname,
as_attachment=True,
)
except:
logging.exception("Failed download")
return "Video download failed!"