Nov-07-2020, 01:31 PM
Hi guys,
I've been researching and coding til my fingers have fallen off on this one!
I'm currently building a youtube video downloader simply to embed python into my brain by doing small projects.
So my current setup (just a brief overview).
1. index page to enter youtube URL - hit the submit button which then executes a function to download youtube video to folder on server, follwed by a return redirect to goto 'conversion complete' page.
2. Arrive at the 'conversion complete' page where you are shown a download button. Click this and it downloads the file but stays on that page with the download button still there.
button for step 2 is...
<a href='download' type='button'><button class='btn btn-default'>Download!</button></a>
My 'download' page doesn't actually show, it simply executes a send_file function to grab the file from step 1 off the server and give to user to download in browser.
So, ultimately, everythings works as I want it to, except that I want to redirect the page after clicking the download button (just to show a page saying download complete, or goto the index page, whatever, location isn't important, it's the fact it won't actually redirect anywhere), however I have seen many people have issues with a redirect following a send file and hope you guys can help!
Popped my python code below incase needed. Feel free to tell me if there's a better way to do something i'm doing. (I'm aware there maybe some imports at the top being used that aren't needed, I'll clean those up later along with removing get/post requests on routes that aren't needed - i put them there for now just to ensure that doesn't trip me up)
Really appreciate any help here! I've come this far without asking for help, but just need a little advice to get over the finish line!
I've been researching and coding til my fingers have fallen off on this one!
I'm currently building a youtube video downloader simply to embed python into my brain by doing small projects.
So my current setup (just a brief overview).
1. index page to enter youtube URL - hit the submit button which then executes a function to download youtube video to folder on server, follwed by a return redirect to goto 'conversion complete' page.
2. Arrive at the 'conversion complete' page where you are shown a download button. Click this and it downloads the file but stays on that page with the download button still there.
button for step 2 is...
<a href='download' type='button'><button class='btn btn-default'>Download!</button></a>
My 'download' page doesn't actually show, it simply executes a send_file function to grab the file from step 1 off the server and give to user to download in browser.
So, ultimately, everythings works as I want it to, except that I want to redirect the page after clicking the download button (just to show a page saying download complete, or goto the index page, whatever, location isn't important, it's the fact it won't actually redirect anywhere), however I have seen many people have issues with a redirect following a send file and hope you guys can help!
Popped my python code below incase needed. Feel free to tell me if there's a better way to do something i'm doing. (I'm aware there maybe some imports at the top being used that aren't needed, I'll clean those up later along with removing get/post requests on routes that aren't needed - i put them there for now just to ensure that doesn't trip me up)
Really appreciate any help here! I've come this far without asking for help, but just need a little advice to get over the finish line!
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 |
################################### ####### Youtube Downloader ######## ################################### from flask import Flask, render_template, redirect, url_for, session, request, flash, send_file from flask_wtf import FlaskForm from wtforms import StringField, SubmitField, BooleanField from pytube import YouTube import os import random, string import re app = Flask(__name__) app.config[ 'SECRET_KEY' ] = 'mysecretkey' session_id = ''.join(random.choices(string.ascii_letters + string.digits, k = 6 )) ###################################### ############## Forms ################# ###################################### class url_form(FlaskForm): link = StringField( 'Enter link of YouTube video you would like to convert...' ) audio = BooleanField( 'Audio Only' ) submit = SubmitField( 'Convert' ) ###################################### ############## Functions############## ###################################### def download_video(link, just_audio): yt = YouTube(link) download_path = 'conversions/' #global myvar if just_audio = = True : stream = yt.streams. filter (only_audio = True ).first() tag = 'video' else : stream = yt.streams. filter (adaptive = True ).first() tag = 'audio only' download_video.name = yt.title download_video.cleanString = re.sub( '[^a-zA-Z0-9 \n\.]' , '', download_video.name) download_video.thumbnail = yt.thumbnail_url download_video.path = stream.download(filename = download_video.cleanString, output_path = download_path) return ###################################### ############## MAIN ################## ###################################### @app .route( '/' , methods = [ 'GET' , 'POST' ]) def index(): result = False form = url_form() if form.validate_on_submit(): session[ 'link' ] = form.link.data if form.audio.data: just_audio = True else : just_audio = False session[ 'just_audio' ] = just_audio link = session.get( 'link' ) just_audio = session.get( 'just_audio' ) download_video(link, just_audio) #download_video(link, just_audio) #return send_file(download_video.path, attachment_filename=download_video.cleanString + '.mp4', as_attachment=True) # CURRENTLY GETTING FROM ROOT FOLDER! return redirect(url_for( 'conversion_complete' )) return render_template( 'index.html' , form = form, result = result) # URL Variable here relates to decorator variable @app .route( '/conversion_complete' , methods = [ 'GET' , 'POST' ]) def conversion_complete(): return render_template( 'conversion_complete.html' ) # URL Variable here relates to decorator variable @app .route( '/download/' , methods = [ 'GET' ]) def download(): return send_file(download_video.path, attachment_filename = download_video.cleanString + '.mp4' , as_attachment = True ) # CURRENTLY GETTING FROM ROOT FOLDER! return render_template( 'result.html' ) if __name__ = = '__main__' : app.run(debug = True ) |