Skip to content
42 changes: 32 additions & 10 deletions tidal_dl_ng/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,9 +570,12 @@ def item(
)

# Step 5: Post-processing
self._perform_post_processing(

final_path = path_media_dst # Path before potential move

new_path_if_moved = self._perform_post_processing(
media,
path_media_dst,
final_path,
quality_audio,
quality_video,
quality_audio_old,
Expand All @@ -581,7 +584,11 @@ def item(
skip_file,
)

return download_success, path_media_dst
# If post-processing moved the file, update the final_path to the new location
if new_path_if_moved:
final_path = new_path_if_moved

return download_success, final_path

def _validate_and_prepare_media(
self,
Expand Down Expand Up @@ -927,31 +934,36 @@ def _handle_metadata_and_extras(
def _perform_post_processing(
self,
media: Track | Video,
path_media_dst: pathlib.Path,
path_media_src: pathlib.Path,
quality_audio: Quality | None,
quality_video: QualityVideo | None,
quality_audio_old: Quality | None,
quality_video_old: QualityVideo | None,
download_delay: bool,
skip_file: bool,
) -> None:
) -> pathlib.Path | None:
"""Perform post-processing tasks.

Args:
media (Track | Video): Media item.
path_media_dst (pathlib.Path): Destination file path.
path_media_src (pathlib.Path): The source file path (before potential move).
quality_audio (Quality | None): Audio quality setting.
quality_video (QualityVideo | None): Video quality setting.
quality_audio_old (Quality | None): Previous audio quality.
quality_video_old (QualityVideo | None): Previous video quality.
download_delay (bool): Whether to apply download delay.
skip_file (bool): Whether file was skipped.

Returns:
pathlib.Path | None: The new path if the file was moved, otherwise None.
"""
new_path: pathlib.Path | None = None

# If files needs to be symlinked, do postprocessing here.
if self.settings.data.symlink_to_track and not isinstance(media, Video):
# Determine file extension for symlink
file_extension = path_media_dst.suffix
self.media_move_and_symlink(media, path_media_dst, file_extension)
file_extension = path_media_src.suffix
new_path = self.media_move_and_symlink(media, path_media_src, file_extension)

# Reset quality settings
if quality_audio_old is not None:
Expand All @@ -972,6 +984,8 @@ def _perform_post_processing(
self.fn_logger.debug(f"Next download will start in {time_sleep} seconds.")
time.sleep(time_sleep)

return new_path

def media_move_and_symlink(
self, media: Track | Video, path_media_src: pathlib.Path, file_extension: str
) -> pathlib.Path:
Expand Down Expand Up @@ -1301,7 +1315,7 @@ def items(
download_delay: bool = True,
quality_audio: Quality | None = None,
quality_video: QualityVideo | None = None,
) -> None:
) -> pathlib.Path | None:
"""Download all items in an album, playlist, or mix.

Args:
Expand All @@ -1313,11 +1327,14 @@ def items(
download_delay (bool, optional): Whether to delay between downloads. Defaults to True.
quality_audio (Quality | None, optional): Audio quality. Defaults to None.
quality_video (QualityVideo | None, optional): Video quality. Defaults to None.

Returns:
pathlib.Path | None: The path to the downloaded album/playlist directory, or None on failure.
"""
# Validate and prepare media collection
validated_media = self._validate_and_prepare_media(media, media_id, media_type, video_download)
if validated_media is None or not isinstance(validated_media, Album | Playlist | UserPlaylist | Mix):
return
return None

media = validated_media

Expand Down Expand Up @@ -1356,6 +1373,11 @@ def items(

self.fn_logger.info(f"Finished list '{list_media_name}'.")

# Return the path to the album/playlist directory
if result_dirs:
return result_dirs[0] # All paths should share the same parent
return None

def _setup_collection_download_context(
self,
media: Album | Playlist | UserPlaylist | Mix,
Expand Down
Loading
Loading