def send_json_response(progress_file_path, video_id): clean_downloads_foider() correct_file = [ filename for filename in os.listdir(download_dir) if video_id in filename ][0] filesize = round( (os.path.getsize(os.path.join(download_dir, correct_file)) / 1_000_000), 2) log.info(f'{filesize} MB') user_ip = get_ip() # Query the database by IP. user = User.query.filter_by(ip=user_ip).first() if user: user.mb_downloaded += filesize db.session.commit() new_filename = correct_file.replace('_', ' ').replace('#', '').replace( f'-{video_id}', '') log.info(new_filename) os.replace(os.path.join(download_dir, correct_file), os.path.join(download_dir, new_filename)) with open("logs/downloads.txt", "a") as f: f.write(f'\n{new_filename}') return jsonify(download_path=os.path.join('downloads', new_filename), log_file=progress_file_path)
def send_json_response(progress_file_path, video_id): for file in os.listdir(download_dir): # Delete the unnecessary files. if '.temp' in file or '.webp' in file: os.remove(os.path.join(download_dir, file)) elif video_id in file: filesize = round((os.path.getsize(os.path.join(download_dir, file)) / 1_000_000), 2) log.info(f'{filesize} MB') user_ip = get_ip() # Query the database by IP. user = User.query.filter_by(ip=user_ip).first() if user: user.mb_downloaded += filesize db.session.commit() new_filename = file.replace('_', ' ').replace('#', '').replace(f'-{video_id}', '') os.replace(os.path.join(download_dir, file), os.path.join(download_dir, new_filename)) with open("logs/downloads.txt", "a") as f: f.write(f'\n{new_filename}') return jsonify(download_path=os.path.join('downloads', new_filename), log_file=progress_file_path)
def send_json_response(download_type): global filename filename = [ file for file in os.listdir(download_dir) if '.part' not in file and os.path.splitext(file)[0] == filename ][0] filesize = round( (os.path.getsize(os.path.join(download_dir, filename)) / 1_000_000), 2) log.info(f'{filename} | {filesize} MB') # Query the database by IP. user = User.query.filter_by(ip=get_ip()).first() # If the user has used the downloader before, update the database. if user: user.mb_downloaded += filesize db.session.commit() # Remove any hashtags or pecentage symbols as they cause an issue and make the filename more aesthetically pleasing. new_filename = filename.replace('#', '').replace(download_type, '.').replace( '%', '').replace('_', ' ') log.info(new_filename) os.replace(os.path.join(download_dir, filename), os.path.join(download_dir, new_filename)) # Update the list of videos downloaded. with open("logs/downloads.txt", "a") as f: f.write(f'\n{new_filename}') return jsonify(download_path=os.path.join('downloads', new_filename), log_file=session['progress_file_path'])
def return_download_link(progress_file_path, video_id, download_type): for file in os.listdir(download_dir): if file.split( '.' )[-1] in relevant_extensions and video_id in file and download_type in file: log.info(f'DOWNLOADED "{file}"') filesize = round( (os.path.getsize(f'{download_dir}/{file}') / 1_000_000), 2) log.info(f'{filesize} MB') user_ip = get_ip() user = User.query.filter_by(ip=user_ip).first() if user: user.mb_downloaded += filesize db.session.commit() with open("logs/downloaded-files.txt", "a") as f: f.write(f'\n{file}') new_filename = file.replace('#', '').replace(f'-{video_id}', '') os.replace(f'{download_dir}/{file}', f'{download_dir}/{new_filename}') return { 'download_path': os.path.join('downloads', new_filename), 'log_file': progress_file_path }
def update_database(mb_downloaded): # Use the get_ip function imported from loggers.py user_ip = get_ip() # Query the database by IP. user = User.query.filter_by(ip=user_ip).first() if user: user.times_used_yt_downloader += 1 user.mb_downloaded += mb_downloaded db.session.commit() else: new_user = User(ip=user_ip, times_used_yt_downloader=1, mb_downloaded=0) db.session.add(new_user) db.session.commit()
def update_database(): # Use the get_ip function imported from loggers.py user_ip = get_ip() # Query the database by IP. user = User.query.filter_by(ip=user_ip).first() if user: x = 'times' if user.times_used_yt_downloader == 1 else 'times' log.info(f'This user has used the downloader {user.times_used_yt_downloader} {x} before.') user.times_used_yt_downloader += 1 db.session.commit() else: new_user = User(ip=user_ip, times_used_yt_downloader=1, mb_downloaded=0) db.session.add(new_user) db.session.commit()
def send_json_response(progress_file_path, video_id, download_type): clean_downloads_foider() downloads = os.listdir(download_dir) correct_file = [filename for filename in downloads if video_id in filename and download_type in filename][0] log.info(correct_file) filesize = round((os.path.getsize(os.path.join(download_dir, correct_file)) / 1_000_000), 2) # Query the database by IP. user = User.query.filter_by(ip=get_ip()).first() # If the user has used the downloader before, update the database. if user: user.mb_downloaded += filesize db.session.commit() # Remove any hashtags or pecentage symbols as they cause an issue and make the filename more aesthetically pleasing. new_filename = correct_file.replace('#', '').replace(f'-{video_id}{download_type}', '').replace('%', '').replace('_', ' ') log.info(new_filename) os.replace(os.path.join(download_dir, correct_file), os.path.join(download_dir, new_filename)) # Update the list of videos downloaded. with open("logs/downloads.txt", "a") as f: f.write(f'\n{new_filename}') return jsonify(download_path=os.path.join('downloads', new_filename), log_file=progress_file_path)
def yt_downloader(): # First POST request when the user clicks on a download button. if request.form['button_clicked'] == 'yes': log_this('clicked a download button.') downloads_folder_size = 0 for file in os.listdir(download_dir): # Downloads folder size in MB. downloads_folder_size += os.path.getsize( os.path.join(download_dir, file)) / 1_000_000 if downloads_folder_size > 3000: log.info( 'Downloads folder larger than 3 GB. Emptying the folder...') for file in os.listdir(download_dir): os.remove(os.path.join(download_dir, file)) log.info('Downloads folder emptied.') # Use the get_ip function imported from loggers.py user_ip = get_ip() # Query the database by IP. user = User.query.filter_by(ip=user_ip).first() if user: x = 'time' if user.times_used_yt_downloader == 1 else 'times' log.info( f'This user has used the downloader {user.times_used_yt_downloader} {x} before.' ) user.times_used_yt_downloader += 1 db.session.commit() else: new_user = User(ip=user_ip, times_used_yt_downloader=1, mb_downloaded=0) db.session.add(new_user) db.session.commit() # I want to save the download progress to a file and read from that file to show the download progress # to the user. Set the name of the file to the time since the epoch. progress_file_name = f'{str(time())[:-8]}.txt' session['progress_file_path'] = os.path.join('yt-progress', progress_file_name) log.info(f'Progress will be saved to: {session["progress_file_path"]}') return session['progress_file_path'] # Second POST request: video_link = request.form['link'] # Use the get_video_id function to get the video ID from the link. video_id = str(get_video_id(video_link)) log.info(f'{video_link} | {video_id}') download_template = f'{download_dir}/%(title)s-%(id)s.%(ext)s' # Video [best] if request.form['button_clicked'] == 'Video [best]': args = [ youtube_dl_path, '--newline', '--restrict-filenames', '--cookies', 'cookies.txt', '-o', download_template, '--', video_id ] run_youtube_dl(request.form['button_clicked'], args) log_downloads_per_day() return send_json_response(session['progress_file_path'], video_id) # Video [MP4] elif request.form['button_clicked'] == 'Video [MP4]': args = [ youtube_dl_path, '--newline', '--restrict-filenames', '--cookies', 'cookies.txt', '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best', '-o', download_template, '--', video_id ] run_youtube_dl(request.form['button_clicked'], args) log_downloads_per_day() return send_json_response(session['progress_file_path'], video_id) # Audio [best] elif request.form['button_clicked'] == 'Audio [best]': args = [ youtube_dl_path, '--newline', '--restrict-filenames', '--cookies', 'cookies.txt', '-x', '-o', download_template, '--', video_id ] run_youtube_dl(request.form['button_clicked'], args) log_downloads_per_day() return send_json_response(session['progress_file_path'], video_id) # MP3 elif request.form['button_clicked'] == 'MP3': args = [ youtube_dl_path, '--newline', '--restrict-filenames', '--cookies', 'cookies.txt', '-x', '--audio-format', 'mp3', '--audio-quality', '0', '-o', download_template, '--', video_id ] run_youtube_dl(request.form['button_clicked'], args) log_downloads_per_day() return send_json_response(session['progress_file_path'], video_id)
def yt_downloader(): # First POST request when the user clicks on a download button. if request.form['button_clicked'] == 'yes': log_this('clicked a download button.') # Use the get_ip function imported from loggers.py user_ip = get_ip() # Query the database by IP. user = User.query.filter_by(ip=user_ip).first() if user: x = 'time' if user.times_used_yt_downloader == 1 else 'times' log.info(f'This user has used the downloader {user.times_used_yt_downloader} {x} before.') user.times_used_yt_downloader += 1 db.session.commit() else: new_user = User(ip=user_ip, times_used_yt_downloader=1, mb_downloaded=0) db.session.add(new_user) db.session.commit() # I want to save the download progress to a file and read from that file to show the download progress # to the user. Set the name of the file to the time since the epoch. progress_file_name = f'{str(time.time())[:-8]}.txt' session['progress_file_path'] = os.path.join('yt-progress', progress_file_name) log.info(f'Progress will be saved to: {session["progress_file_path"]}') return session['progress_file_path'] # Second POST request: video_link = request.form['link'] # Use the get_video_id function to get the video ID from the link. video_id = str(get_video_id(video_link)) log.info(f'{video_link} | {video_id}') download_template = f'{download_dir}/%(title)s-%(id)s.%(ext)s' # Video [best] if request.form['button_clicked'] == 'Video [best]': args = [youtube_dl_path, '--newline', '--restrict-filenames', '--cookies', 'cookies.txt', '-o', download_template, '--', video_id] download_start_time = time.time() with open(session['progress_file_path'], 'w') as f: subprocess.run(args, stdout=f) download_complete_time = time.time() log.info(f'Video [best] was chosen. Download took: {round((download_complete_time - download_start_time), 1)}s') return send_json_response(session['progress_file_path'], video_id) # Video [MP4] elif request.form['button_clicked'] == 'Video [MP4]': args = [youtube_dl_path, '--newline', '--restrict-filenames', '--cookies', 'cookies.txt', '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best', '-o', download_template, '--', video_id] download_start_time = time.time() with open(session['progress_file_path'], 'w') as f: subprocess.run(args, stdout=f) download_complete_time = time.time() log.info(f'MP4 was chosen. Download took: {round((download_complete_time - download_start_time), 1)}s') return send_json_response(session['progress_file_path'], video_id, applicable_extensions) # Audio [best] elif request.form['button_clicked'] == 'Audio [best]': args = [youtube_dl_path, '--newline','--restrict-filenames', '--cookies', 'cookies.txt', '-x', '-o', download_template, '--', video_id] download_start_time = time.time() with open(session['progress_file_path'], 'w') as f: subprocess.run(args, stdout=f) download_complete_time = time.time() log.info(f'Audio [best] was chosen. Download took: {round((download_complete_time - download_start_time), 1)}s') return send_json_response(session['progress_file_path'], video_id) # MP3 elif request.form['button_clicked'] == 'MP3': args = [youtube_dl_path, '--newline', '--restrict-filenames', '--cookies', 'cookies.txt', '-x', '--embed-thumbnail', '--audio-format', 'mp3', '--audio-quality', '0', '-o', download_template, '--', video_id] download_start_time = time.time() with open(session['progress_file_path'], 'w') as f: subprocess.run(args, stdout=f) download_complete_time = time.time() log.info(f'MP3 was chosen. Download took: {round((download_complete_time - download_start_time), 1)}s') return send_json_response(session['progress_file_path'], video_id)
def yt_downloader(): if request.form['button_clicked'] == 'yes': log_this('Clicked a download button.') #db.create_all() # Intialise the size_of_media_files variable size_of_media_files = 0 # Iterate over each file in the folder and add its size to the above variable. for file in os.listdir(download_dir): size_of_file = os.path.getsize( f'{download_dir}/{file}') / 1_000_000 size_of_media_files += size_of_file # If there's more than 10 GB of files in the downloads folder, empty it. if size_of_media_files > 10_000: log.info( f'More than 10 GB worth of downloads found. Emptying downloads folder...' ) for file in os.listdir(download_dir): if file.split('.')[-1] in relevant_extensions: os.remove(f'{download_dir}/{file}') log.info('Downloads folder emptied.') # I want to save the download progress to a file and read from that file to show the download progress # to the user. Set the name of the file to the time since the epoch. progress_file_name = str(time.time())[:-8] + '.txt' session['progress_file_path'] = os.path.join('yt-progress', progress_file_name) log.info( f'Progress will be saved to: {session["progress_file_path"] }') user_ip = get_ip() user = User.query.filter_by(ip=user_ip).first() if user: x = 'time' if user.times_used_yt_downloader == 1 else 'times' log.info( f'This user has used the downloader {user.times_used_yt_downloader} {x} before.' ) user.times_used_yt_downloader += 1 db.session.commit() else: new_user = User(ip=user_ip, times_used_yt_downloader=1, mb_downloaded=0) db.session.add(new_user) db.session.commit() return session['progress_file_path'] # The following runs after the 2nd POST request: link = request.form['link'] video_id = str(get_video_id(link)) log.info(f'LINK: {link} | ID: {video_id}') if request.form['button_clicked'] == 'Video [best]': log.info(f'Video [best] was chosen.') download_template = f'{download_dir}/%(title)s-%(id)s [Video].%(ext)s' args = [ youtube_dl_path, '--newline', '--restrict-filenames', '--cookies', 'cookies.txt', '-o', download_template, '--', video_id ] download_start_time = time.time() with open(session['progress_file_path'], 'w') as f: subprocess.run(args, stdout=f) download_complete_time = time.time() log.info( f'Download took: {round((download_complete_time - download_start_time), 1)}s' ) download_link = return_download_link(session['progress_file_path'], video_id, '[Video]') return download_link elif request.form['button_clicked'] == 'Video [MP4]': log.info(f'MP4 was chosen.') download_template = f'{download_dir}/%(title)s-%(id)s [MP4].%(ext)s' args = [ youtube_dl_path, '--newline', '--restrict-filenames', '--cookies', 'cookies.txt', '-f', 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best', '-o', download_template, '--', video_id ] download_start_time = time.time() with open(session['progress_file_path'], 'w') as f: subprocess.run(args, stdout=f) download_complete_time = time.time() log.info( f'Download took: {round((download_complete_time - download_start_time), 1)}s' ) download_link = return_download_link(session['progress_file_path'], video_id, '[MP4]') return download_link elif request.form['button_clicked'] == 'Audio [best]': log.info(f'Audio [best] was chosen.') download_template = f'{download_dir}/%(title)s-%(id)s [Audio].%(ext)s' args = [ youtube_dl_path, '--newline', '--restrict-filenames', '--cookies', 'cookies.txt', '-x', '-o', download_template, '--', video_id ] download_start_time = time.time() with open(session['progress_file_path'], 'w') as f: subprocess.run(args, stdout=f) download_complete_time = time.time() log.info( f'Download took: {round((download_complete_time - download_start_time), 1)}s' ) download_link = return_download_link(session['progress_file_path'], video_id, '[Audio]') return download_link elif request.form['button_clicked'] == 'MP3': log.info(f'MP3 was chosen.') download_template = f'{download_dir}/%(title)s-%(id)s [MP3].%(ext)s' args = [ youtube_dl_path, '--newline', '--restrict-filenames', '--cookies', 'cookies.txt', '-x', '--embed-thumbnail', '--audio-format', 'mp3', '--audio-quality', '0', '-o', download_template, '--', video_id ] download_start_time = time.time() with open(session['progress_file_path'], 'w') as f: subprocess.run(args, stdout=f) download_complete_time = time.time() log.info( f'Download took: {round((download_complete_time - download_start_time), 1)}s' ) download_link = return_download_link(session['progress_file_path'], video_id, '[MP3]') return download_link
def yt_downloader(): # First POST request: if request.form['button_clicked'] == 'yes': # Empty the downloads folder if there is less than 2 GB free storage space. free_space = shutil.disk_usage('/')[2] free_space_gb = free_space / 1_000_000_000 if free_space_gb < 2: empty_folder('downloads') progress_file_name = f'{str(time())[:-8]}.txt' session['progress_file_path'] = f'yt-progress/{progress_file_name}' return session['progress_file_path'], 200 # Second POST request: log_this(f'Clicked on {request.form["button_clicked"]}') user_ip = get_ip() # Query the database by IP. user = User.query.filter_by(ip=user_ip).first() if user: string = f'{user.times_used_yt_downloader} times' if user.times_used_yt_downloader > 1 else 'once' log.info(f'This user has used the downloader {string} before.') video_link = request.form['link'] log.info(video_link) # Video (best quality) if request.form['button_clicked'] == 'video_best': options = { 'format': 'bestvideo+bestaudio/best', 'outtmpl': f'{download_dir}/%(title)s.%(ext)s', 'restrictfilenames': True, 'logger': Logger() } run_youtube_dl(video_link, options) return return_download_path() # MP4 elif request.form['button_clicked'] == 'mp4': options = { 'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best', 'outtmpl': f'{download_dir}/%(title)s.%(ext)s', 'restrictfilenames': True, 'logger': Logger() } run_youtube_dl(video_link, options) return return_download_path() # Audio (best quality) elif request.form['button_clicked'] == 'audio_best': options = { 'format': 'bestaudio/best', 'outtmpl': f'{download_dir}/%(title)s.%(ext)s', 'postprocessors': [{ 'key': 'FFmpegExtractAudio' }], 'restrictfilenames': True, 'logger': Logger() } run_youtube_dl(video_link, options) return return_download_path() # MP3 elif request.form['button_clicked'] == 'audio_mp3': options = { 'format': 'bestaudio/best', 'outtmpl': f'{download_dir}/%(title)s.%(ext)s', 'writethumbnail': True, 'postprocessors': [ { 'key': 'FFmpegExtractAudio', 'preferredcodec': 'mp3', 'preferredquality': '0' }, { 'key': 'EmbedThumbnail' } ], 'restrictfilenames': True, 'logger': Logger() } run_youtube_dl(video_link, options) return return_download_path()