Skip to content

Commit

Permalink
Added support for pre-populate save file name with video title
Browse files Browse the repository at this point in the history
  • Loading branch information
DhimanGhosh committed Nov 6, 2022
1 parent e7b960f commit 01fa803
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion window.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import subprocess
import os
import shutil
import urllib.request
import json
import urllib


class Window(QWidget):
Expand Down Expand Up @@ -96,7 +99,13 @@ def __start_download_mp3(self):
self.__display_status('Error in Audio Download!')

def __save_to_dialog(self):
downloads_directory = f"C:\\Users\\{os.environ.get('USERNAME')}\\Downloads"
url = self.url.text()
if url:
title = self.__get_video_title(url)
title = title.replace('|', ', ')
else:
title = 'Sample'
downloads_directory = f"C:\\Users\\{os.environ.get('USERNAME')}\\Downloads\\{title}"
downloadable_formats = "Audio (*.mp3)\nVideo (*.mp4)"
path, _ = QFileDialog.getSaveFileName(parent=self, caption='Save File', directory=downloads_directory, filter=downloadable_formats, initialFilter='File')
self.save_to_path.setText(path.replace('/', os.sep))
Expand All @@ -113,3 +122,14 @@ def __move_to(self, downloaded_file, media_extension='.mp4'):
move_to_path = os.sep.join(move_to_path.split(os.sep)[:-1]) + os.sep + move_to_path.split(os.sep)[-1].split('.')[0] + media_extension
shutil.copy(downloaded_file, move_to_path)
os.remove(downloaded_file)

def __get_video_title(self, url):
params = {"format": "json", "url": url}
video_url = "https://www.youtube.com/oembed"
query_string = urllib.parse.urlencode(params)
video_url += "?" + query_string

with urllib.request.urlopen(video_url) as response:
response_text = response.read()
data = json.loads(response_text.decode())
return data['title']

0 comments on commit 01fa803

Please sign in to comment.