|
1 | | -from flask import Flask,request,redirect,render_template,send_file |
| 1 | +from flask import Flask, request, redirect, render_template, send_file |
2 | 2 | import zipfile |
3 | 3 | import numpy as np |
4 | 4 | import smtplib |
5 | | -import concurrent.futures # for multithreading compilation |
6 | | -#import sys |
7 | | -#import re |
8 | | -from pytube import YouTube #downloading the mp4 file |
9 | | -from pydub import AudioSegment # creating small snippets |
10 | | -from pydub.utils import make_chunks # making small chunks out of it |
| 5 | +import concurrent.futures # for multithreading compilation |
| 6 | + |
| 7 | +# import sys |
| 8 | +# import re |
| 9 | +from pytube import YouTube # downloading the mp4 file |
| 10 | +from pydub import AudioSegment # creating small snippets |
| 11 | +from pydub.utils import make_chunks # making small chunks out of it |
11 | 12 | import os |
12 | | -from email.mime.multipart import MIMEMultipart #Emailing the output to the user |
| 13 | +from email.mime.multipart import MIMEMultipart # Emailing the output to the user |
13 | 14 | from email.mime.base import MIMEBase |
14 | 15 | from email.utils import COMMASPACE, formatdate |
15 | | -from email import encoders # Encoding the attachment |
| 16 | +from email import encoders # Encoding the attachment |
16 | 17 | from zipfile import ZipFile |
17 | | -from youtube_search import YoutubeSearch #Bypassing the Youtube API with the another API |
| 18 | +from youtube_search import ( |
| 19 | + YoutubeSearch, |
| 20 | +) # Bypassing the Youtube API with the another API |
18 | 21 | import pandas as pd |
19 | | -app=Flask(__name__) |
20 | | -@app.route('/',methods=['GET','POST']) |
| 22 | + |
| 23 | +app = Flask(__name__) |
| 24 | + |
| 25 | + |
| 26 | +@app.route("/", methods=["GET", "POST"]) |
21 | 27 | def index(): |
22 | | - if(request.method=='POST'): |
23 | | - singer=request.form['singer'] |
24 | | - Number_vid=int(request.form['Number_vid']) |
25 | | - duration=int(request.form['duration']) |
26 | | - Email=request.form['email'] |
| 28 | + if request.method == "POST": |
| 29 | + singer = request.form["singer"] |
| 30 | + Number_vid = int(request.form["Number_vid"]) |
| 31 | + duration = int(request.form["duration"]) |
| 32 | + Email = request.form["email"] |
27 | 33 | with concurrent.futures.ThreadPoolExecutor() as executor: |
28 | | - executor.submit(process_audio, singer, Number_vid, duration,Email) |
| 34 | + executor.submit(process_audio, singer, Number_vid, duration, Email) |
29 | 35 | # process_audio(singer,Number_vid,duration) |
30 | | - #email=request.form['email'] |
| 36 | + # email=request.form['email'] |
31 | 37 | return redirect("/success") |
32 | | - return render_template('index.html') |
33 | | -def process_audio(singer,Number_vid,duration,Email): |
34 | | - singer1=singer+"songs" |
| 38 | + return render_template("index.html") |
| 39 | + |
| 40 | + |
| 41 | +def process_audio(singer, Number_vid, duration, Email): |
| 42 | + singer1 = singer + "songs" |
35 | 43 | results1 = YoutubeSearch(singer1, max_results=Number_vid).to_dict() |
36 | | - data=pd.DataFrame(results1) |
37 | | - for i in range(1,(data['url_suffix'].count())): |
38 | | - data['url_suffix'][i]="https://www.youtube.com"+data['url_suffix'][i] |
39 | | - links=data['url_suffix'] |
| 44 | + data = pd.DataFrame(results1) |
| 45 | + for i in range(1, (data["url_suffix"].count())): |
| 46 | + data["url_suffix"][i] = "https://www.youtube.com" + data["url_suffix"][i] |
| 47 | + links = data["url_suffix"] |
40 | 48 | for i in links: |
41 | | - #yt = YouTube(str(i)) |
42 | | - yt=YouTube(i,use_oauth=True,allow_oauth_cache=True) |
43 | | - #print(yt.title) |
44 | | - #if(yt.length<100): |
| 49 | + # yt = YouTube(str(i)) |
| 50 | + yt = YouTube(i, use_oauth=True, allow_oauth_cache=True) |
| 51 | + # print(yt.title) |
| 52 | + # if(yt.length<100): |
45 | 53 | # extract only audio |
46 | | - video = yt.streams.filter(file_extension='mp4',only_audio=True).first() |
47 | | - # download the file |
| 54 | + video = yt.streams.filter(file_extension="mp4", only_audio=True).first() |
| 55 | + # download the file |
48 | 56 | out_file = video.download() |
49 | | - # save the file |
| 57 | + # save the file |
50 | 58 | base, ext = os.path.splitext(out_file) |
51 | | - new_file = base + '.mp4' |
| 59 | + new_file = base + ".mp4" |
52 | 60 | os.rename(out_file, new_file) |
53 | 61 | audio_files = [] |
54 | 62 | for file in os.listdir(): |
55 | | - if file.endswith(".mp4"): |
56 | | - audio = AudioSegment.from_file(file, "mp4") |
57 | | - audio_file = file.replace(".mp4", ".wav") |
58 | | - audio.export(audio_file, format="wav") |
59 | | - audio_files.append(audio_file) |
| 63 | + if file.endswith(".mp4"): |
| 64 | + audio = AudioSegment.from_file(file, "mp4") |
| 65 | + audio_file = file.replace(".mp4", ".wav") |
| 66 | + audio.export(audio_file, format="wav") |
| 67 | + audio_files.append(audio_file) |
60 | 68 | chunks = [] |
61 | | - #print(audio_files) |
| 69 | + # print(audio_files) |
62 | 70 | for audio_file in audio_files: |
63 | | - chunk = make_chunks(AudioSegment.from_file(audio_file,format="wav"), chunk_length=duration * 1000) |
64 | | - id=np.random.randint(0,len(chunk)) |
65 | | - chunk=chunk[id] |
66 | | - #print(chunk) |
67 | | - chunks.append(chunk) |
68 | | - merged=AudioSegment.empty() |
69 | | - for chunk in chunks: |
70 | | - merged+=chunk |
71 | | - merged.export('output.mp3', format="mp3") |
72 | | - dir=os.getcwd() |
| 71 | + chunk = make_chunks( |
| 72 | + AudioSegment.from_file(audio_file, format="wav"), |
| 73 | + chunk_length=duration * 1000, |
| 74 | + ) |
| 75 | + id = np.random.randint(0, len(chunk)) |
| 76 | + chunk = chunk[id] |
| 77 | + # print(chunk) |
| 78 | + chunks.append(chunk) |
| 79 | + merged = AudioSegment.empty() |
| 80 | + for chunk in chunks: |
| 81 | + merged += chunk |
| 82 | + merged.export("output.mp3", format="mp3") |
| 83 | + dir = os.getcwd() |
73 | 84 | test = os.listdir(dir) |
74 | 85 | for item in test: |
75 | 86 | if item.endswith(".mp4"): |
76 | 87 | os.remove(os.path.join(dir, item)) |
77 | 88 | if item.endswith(".wav"): |
78 | 89 | os.remove(os.path.join(dir, item)) |
79 | | - with zipfile.ZipFile('output.zip', 'w') as zipf: |
80 | | - zipf.write('output.mp3') |
| 90 | + with zipfile.ZipFile("output.zip", "w") as zipf: |
| 91 | + zipf.write("output.mp3") |
81 | 92 | msg = MIMEMultipart() |
82 | | - msg['From'] = "noobbobby241@gmail.com" |
83 | | - msg['To'] = COMMASPACE.join([Email]) |
84 | | - msg['Date'] = formatdate(localtime=True) |
85 | | - msg['Subject'] = "Downloaded and Converted Audio" |
| 93 | + msg["From"] = "noobbobby241@gmail.com" |
| 94 | + msg["To"] = COMMASPACE.join([Email]) |
| 95 | + msg["Date"] = formatdate(localtime=True) |
| 96 | + msg["Subject"] = "Downloaded and Converted Audio" |
86 | 97 |
|
87 | 98 | with open("output.zip", "rb") as f: |
88 | | - part = MIMEBase('application', "octet-stream") |
| 99 | + part = MIMEBase("application", "octet-stream") |
89 | 100 | part.set_payload(f.read()) |
90 | 101 |
|
91 | 102 | encoders.encode_base64(part) |
92 | | - part.add_header('Content-Disposition', 'attachment', filename="output.zip") |
| 103 | + part.add_header("Content-Disposition", "attachment", filename="output.zip") |
93 | 104 | msg.attach(part) |
94 | | - smtp = smtplib.SMTP('smtp.gmail.com', 587) |
| 105 | + smtp = smtplib.SMTP("smtp.gmail.com", 587) |
95 | 106 | smtp.ehlo() |
96 | 107 | smtp.starttls() |
97 | 108 | smtp.ehlo() |
98 | 109 | smtp.login("noobbobby241@gmail.com", "cliwqftyqujpisht") |
99 | 110 | smtp.sendmail("noobbobby241@gmail.com", [Email], msg.as_string()) |
100 | 111 | print("Email Sent") |
101 | | -@app.route('/success') |
| 112 | + |
| 113 | + |
| 114 | +@app.route("/success") |
102 | 115 | def success(): |
103 | | - return render_template('success.html') |
104 | | -#@app.route('/download') |
| 116 | + return render_template("success.html") |
| 117 | + |
| 118 | + |
| 119 | +# @app.route('/download') |
105 | 120 | # def download(): |
106 | 121 | # #@after_this_request |
107 | 122 | # #def cleanup(response): |
108 | 123 | # #cleanup_directory() |
109 | 124 | # #return response |
110 | 125 | # return send_file('output.zip', as_attachment=True) |
111 | | -#def cleanup_directory(): |
112 | | - #test = os.listdir() |
113 | | - #for item in test: |
114 | | - #if item.endswith(".mp3"): |
115 | | - #os.remove(os.path.join(os.getcwd(), item)) |
116 | | - #if item.endswith(".zip"): |
117 | | - #os.remove(os.path.join(os.getcwd(), item)) |
118 | | -if __name__=='__main__': |
119 | | - app.debug=True |
120 | | - app.run(host="0.0.0.0",port=5000) |
| 126 | +# def cleanup_directory(): |
| 127 | +# test = os.listdir() |
| 128 | +# for item in test: |
| 129 | +# if item.endswith(".mp3"): |
| 130 | +# os.remove(os.path.join(os.getcwd(), item)) |
| 131 | +# if item.endswith(".zip"): |
| 132 | +# os.remove(os.path.join(os.getcwd(), item)) |
| 133 | +if __name__ == "__main__": |
| 134 | + app.debug = True |
| 135 | + app.run(host="0.0.0.0", port=5000) |
0 commit comments