Skip to content

Commit

Permalink
Fix generic downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
justin025 committed Jan 22, 2025
1 parent 1d27f15 commit 87e54c0
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 91 deletions.
6 changes: 5 additions & 1 deletion src/onthespot/api/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,11 @@ def spotify_login_user(account):
config = Session.Configuration.Builder().set_stored_credential_file(session_json_path).build()
# For some reason initialising session as None prevents premature application exit
session = None
session = Session.Builder(conf=config).stored_file(session_json_path).create()
try:
session = Session.Builder(conf=config).stored_file(session_json_path).create()
except ConnectionRefusedError:
time.sleep(3)
session = Session.Builder(conf=config).stored_file(session_json_path).create()
logger.debug("Session created")
logger.info(f"Login successful for user '{username[:4]}*******'")
account_type = session.get_user_attribute("type")
Expand Down
167 changes: 84 additions & 83 deletions src/onthespot/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ def run(self):
ydl_opts['quiet'] = True
ydl_opts['no_warnings'] = True
ydl_opts['noprogress'] = True
ydl_opts['outtmpl'] = config.get('generic_audio_download_path') + os.path.sep + '%(title)s.%(ext)s'
ydl_opts['outtmpl'] = config.get('video_download_path') + os.path.sep + '%(title)s.%(ext)s'
ydl_opts['ffmpeg_location'] = config.get('_ffmpeg_bin_path')
ydl_opts['postprocessors'] = [{
'key': 'FFmpegMetadata', # Enables embedding metadata
Expand All @@ -570,99 +570,100 @@ def run(self):
self.readd_item_to_download_queue(item)
continue

# Audio Formatting
if item_type in ('track', 'podcast_episode'):
# Lyrics
if item_service in ("apple_music", "spotify", "tidal"):
item['item_status'] = 'Getting Lyrics'
if self.gui:
self.progress.emit(item, self.tr("Getting Lyrics"), 99)
extra_metadata = globals()[f"{item_service}_get_lyrics"](token, item_id, item_type, item_metadata, file_path)
if isinstance(extra_metadata, dict):
item_metadata.update(extra_metadata)

if config.get('raw_media_download'):
file_path += default_format
elif item_type == "track":
file_path += "." + config.get("track_file_format")
elif item_type == "podcast_episode":
file_path += "." + config.get("podcast_file_format")

os.rename(temp_file_path, file_path)
item['file_path'] = file_path

# Convert file format and embed metadata
if not config.get('raw_media_download'):
item['item_status'] = 'Converting'
if self.gui:
self.progress.emit(item, self.tr("Converting"), 99)

if config.get('use_custom_file_bitrate'):
bitrate = config.get("file_bitrate")
convert_audio_format(file_path, bitrate, default_format)
if item_service != 'generic':
# Audio Formatting
if item_type in ('track', 'podcast_episode'):
# Lyrics
if item_service in ("apple_music", "spotify", "tidal"):
item['item_status'] = 'Getting Lyrics'
if self.gui:
self.progress.emit(item, self.tr("Getting Lyrics"), 99)
extra_metadata = globals()[f"{item_service}_get_lyrics"](token, item_id, item_type, item_metadata, file_path)
if isinstance(extra_metadata, dict):
item_metadata.update(extra_metadata)

if config.get('raw_media_download'):
file_path += default_format
elif item_type == "track":
file_path += "." + config.get("track_file_format")
elif item_type == "podcast_episode":
file_path += "." + config.get("podcast_file_format")

embed_metadata(item, item_metadata)
os.rename(temp_file_path, file_path)
item['file_path'] = file_path

# Thumbnail
if config.get('save_album_cover') or config.get('embed_cover'):
item['item_status'] = 'Setting Thumbnail'
# Convert file format and embed metadata
if not config.get('raw_media_download'):
item['item_status'] = 'Converting'
if self.gui:
self.progress.emit(item, self.tr("Setting Thumbnail"), 99)
set_music_thumbnail(file_path, item_metadata)
self.progress.emit(item, self.tr("Converting"), 99)

if os.path.splitext(file_path)[1] == '.mp3':
fix_mp3_metadata(file_path)
else:
if config.get('save_album_cover'):
item['item_status'] = 'Setting Thumbnail'
if self.gui:
self.progress.emit(item, self.tr("Setting Thumbnail"), 99)
set_music_thumbnail(file_path, item_metadata)
if config.get('use_custom_file_bitrate'):
bitrate = config.get("file_bitrate")
convert_audio_format(file_path, bitrate, default_format)

# M3U
if config.get('create_m3u_file') and item.get('parent_category') == 'playlist':
item['item_status'] = 'Adding To M3U'
if self.gui:
self.progress.emit(item, self.tr("Adding To M3U"), 1)
add_to_m3u_file(item, item_metadata)

# Video Formatting
elif item_type in ('movie', 'episode'):
subtitle_files = []
if config.get("download_subtitles"):
item['item_status'] = 'Getting Subtitles'
if self.gui:
self.progress.emit(item, self.tr("Getting Subtitles"), 99)
embed_metadata(item, item_metadata)

# Thumbnail
if config.get('save_album_cover') or config.get('embed_cover'):
item['item_status'] = 'Setting Thumbnail'
if self.gui:
self.progress.emit(item, self.tr("Setting Thumbnail"), 99)
set_music_thumbnail(file_path, item_metadata)

subtitle_dict = item_metadata.get("subtitle_urls")
if config.get("download_all_available_subtitles"):
for key in subtitle_dict:
subtitle_data = requests.get(subtitle_dict[key].get("url")).text
subtitle_file = file_path + f".{key}." + subtitle_dict[key].get("ext")
if os.path.splitext(file_path)[1] == '.mp3':
fix_mp3_metadata(file_path)
else:
if config.get('save_album_cover'):
item['item_status'] = 'Setting Thumbnail'
if self.gui:
self.progress.emit(item, self.tr("Setting Thumbnail"), 99)
set_music_thumbnail(file_path, item_metadata)

# M3U
if config.get('create_m3u_file') and item.get('parent_category') == 'playlist':
item['item_status'] = 'Adding To M3U'
if self.gui:
self.progress.emit(item, self.tr("Adding To M3U"), 1)
add_to_m3u_file(item, item_metadata)

# Video Formatting
elif item_type in ('movie', 'episode'):
subtitle_files = []
if config.get("download_subtitles"):
item['item_status'] = 'Getting Subtitles'
if self.gui:
self.progress.emit(item, self.tr("Getting Subtitles"), 99)


subtitle_dict = item_metadata.get("subtitle_urls")
if config.get("download_all_available_subtitles"):
for key in subtitle_dict:
subtitle_data = requests.get(subtitle_dict[key].get("url")).text
subtitle_file = file_path + f".{key}." + subtitle_dict[key].get("ext")
with open(subtitle_file, "w") as file:
file.write(subtitle_data)
subtitle_files.append(subtitle_file)
else:
lang = config.get("preferred_subtitle_language")
subtitle_data = requests.get(subtitle_dict[lang].get("url")).text
subtitle_file = file_path + f".{lang}." + subtitle_dict[lang].get("ext")
with open(subtitle_file, "w") as file:
file.write(subtitle_data)
subtitle_files.append(subtitle_file)

if not config.get("raw_media_format"):
item['item_status'] = 'Converting'
if self.gui:
self.progress.emit(item, self.tr("Converting"), 99)
if item_type == "episode":
output_format = config.get("show_file_format")
elif item_type == "movie":
output_format = config.get("movie_file_format")
convert_video_format(file_path, output_format, video_file_parts, subtitle_files)
item['file_path'] = file_path + '.' + output_format
else:
lang = config.get("preferred_subtitle_language")
subtitle_data = requests.get(subtitle_dict[lang].get("url")).text
subtitle_file = file_path + f".{lang}." + subtitle_dict[lang].get("ext")
with open(subtitle_file, "w") as file:
file.write(subtitle_data)
subtitle_files.append(subtitle_file)

if not config.get("raw_media_format"):
item['item_status'] = 'Converting'
if self.gui:
self.progress.emit(item, self.tr("Converting"), 99)
if item_type == "episode":
output_format = config.get("show_file_format")
elif item_type == "movie":
output_format = config.get("movie_file_format")
convert_video_format(file_path, output_format, video_file_parts, subtitle_files)
item['file_path'] = file_path + '.' + output_format
else:
item['file_path'] = file_path + '.mp4'
item['file_path'] = file_path + '.mp4'

item['item_status'] = 'Downloaded'
logger.info("Item Successfully Downloaded")
Expand Down
4 changes: 2 additions & 2 deletions src/onthespot/gui/mainui.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ def bind_button_inputs(self):
self.btn_progress_retry_all.clicked.connect(self.retry_all_failed_downloads)
self.btn_progress_cancel_all.clicked.connect(self.cancel_all_downloads)
self.btn_audio_download_path_browse.clicked.connect(lambda: self.select_dir(self.audio_download_path))
self.btn_generic_audio_download_path_browse.clicked.connect(lambda: self.select_dir(self.generic_audio_download_path))
self.btn_video_download_path.clicked.connect(lambda: self.select_dir(self.video_download_path))

self.btn_download_tmp_browse.clicked.connect(lambda: self.select_dir(self.tmp_dl_root))
self.tmp_dl_root.textChanged.connect(self.update_tmp_dir)
Expand Down Expand Up @@ -217,7 +217,7 @@ def bind_button_inputs(self):
self.settings_bookmark_accounts.clicked.connect(lambda: self.settings_scroll_area.verticalScrollBar().setValue(0))
self.settings_bookmark_general.clicked.connect(lambda: self.settings_scroll_area.verticalScrollBar().setValue(328))
self.settings_bookmark_audio_downloads.clicked.connect(lambda: self.settings_scroll_area.verticalScrollBar().setValue(1160))
self.settings_bookmark_audio_metadata.clicked.connect(lambda: self.settings_scroll_area.verticalScrollBar().setValue(1930))
self.settings_bookmark_audio_metadata.clicked.connect(lambda: self.settings_scroll_area.verticalScrollBar().setValue(2000))
self.settings_bookmark_video_downloads.clicked.connect(lambda: self.settings_scroll_area.verticalScrollBar().setValue(9999))

self.export_logs.clicked.connect(lambda: shutil.copy(
Expand Down
8 changes: 4 additions & 4 deletions src/onthespot/gui/qtui/main.ui
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@
<property name="geometry">
<rect>
<x>0</x>
<y>-1888</y>
<y>-3485</y>
<width>627</width>
<height>4132</height>
</rect>
Expand Down Expand Up @@ -4171,7 +4171,7 @@
</widget>
</item>
<item>
<widget class="QPushButton" name="btn_generic_audio_download_path_browse">
<widget class="QPushButton" name="btn_video_download_path">
<property name="text">
<string/>
</property>
Expand Down Expand Up @@ -4707,8 +4707,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>641</width>
<height>405</height>
<width>140</width>
<height>1309</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
Expand Down
2 changes: 1 addition & 1 deletion src/onthespot/gui/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def load_config(self):
#self.btn_reconfig.setIcon(self.get_icon('trash'))
self.btn_save_config.setIcon(self.get_icon('save'))
self.btn_audio_download_path_browse.setIcon(self.get_icon('folder'))
self.btn_generic_audio_download_path_browse.setIcon(self.get_icon('folder'))
self.btn_video_download_path.setIcon(self.get_icon('folder'))
self.btn_download_tmp_browse.setIcon(self.get_icon('folder'))
self.btn_search.setIcon(self.get_icon('search'))
self.btn_search_filter_toggle.setIcon(self.get_icon('collapse_down'))
Expand Down

0 comments on commit 87e54c0

Please sign in to comment.