diff options
| author | Robert Honz <exislow+github@gmail.com> | 2025-04-02 07:00:28 +0200 |
|---|---|---|
| committer | Robert Honz <exislow+github@gmail.com> | 2025-04-02 07:00:28 +0200 |
| commit | 819655d8b7dc108fd1d9bc7e7deded6baa9bb624 (patch) | |
| tree | a10a0b33847d47da604d66b1e34f5d7affba1ba8 /tidal_dl_ng/download.py | |
| parent | d7a9227c0668dd49c966387d12ecd3c6a216bcff (diff) | |
| download | tidal-dl-ng-819655d8b7dc108fd1d9bc7e7deded6baa9bb624.tar.gz | |
✨ Implemented playlist track position naming (`{list_pos}`) for downloads. Fixes #165
Diffstat (limited to 'tidal_dl_ng/download.py')
| -rw-r--r-- | tidal_dl_ng/download.py | 30 |
1 files changed, 23 insertions, 7 deletions
diff --git a/tidal_dl_ng/download.py b/tidal_dl_ng/download.py index fe5f0c6..3866ca9 100644 --- a/tidal_dl_ng/download.py +++ b/tidal_dl_ng/download.py @@ -329,6 +329,8 @@ class Download: quality_audio: Quality | None = None, quality_video: QualityVideo | None = None, is_parent_album: bool = False, + list_position: int = 0, + list_total: int = 0, ) -> (bool, pathlib.Path): try: if media_id and media_type: @@ -363,7 +365,9 @@ class Download: metadata_tags=[] if isinstance(media, Video) else media.media_metadata_tags, is_video=isinstance(media, Video), ) - file_name_relative: str = format_path_media(file_template, media, self.settings.data.album_track_num_pad_min) + file_name_relative: str = format_path_media( + file_template, media, self.settings.data.album_track_num_pad_min, list_position, list_total + ) path_media_dst: pathlib.Path = ( pathlib.Path(self.path_base).expanduser() / (file_name_relative + file_extension_dummy) ).absolute() @@ -732,7 +736,7 @@ class Download: return # Create file name and path - file_name_relative: str = format_path_media(file_template, media, self.settings.data.album_track_num_pad_min) + file_name_relative: str = format_path_media(file_template, media) # Get the name of the list and check, if videos should be included. list_media_name: str = name_builder_title(media) @@ -756,7 +760,12 @@ class Download: ) is_album: bool = isinstance(media, Album) + # TODO: Refactor strings to constants (also in cfg.py) + sort_by_track_num: bool = ( + True if "album_track_num" in file_name_relative or "list_pos" in file_name_relative else False + ) result_dirs: [pathlib.Path] = [] + list_total: int = len(items) # Iterate through list items while not progress.finished: @@ -771,8 +780,10 @@ class Download: quality_video=quality_video, download_delay=download_delay, is_parent_album=is_album, + list_position=count + 1, + list_total=list_total, ) - for item_media in items + for count, item_media in enumerate(items) ] # Report results as they become available @@ -800,11 +811,13 @@ class Download: # Create playlist file if self.settings.data.playlist_create: - self.playlist_populate(set(result_dirs), list_media_name, is_album) + self.playlist_populate(set(result_dirs), list_media_name, is_album, sort_by_track_num) self.fn_logger.info(f"Finished list '{list_media_name}'.") - def playlist_populate(self, dirs_scoped: [pathlib.Path], name_list: str, is_album: bool) -> [pathlib.Path]: + def playlist_populate( + self, dirs_scoped: [pathlib.Path], name_list: str, is_album: bool, sort_alphabetically + ) -> [pathlib.Path]: result: [pathlib.Path] = [] # For each dir, which contains tracks @@ -821,8 +834,11 @@ class Download: for extension_audio in AudioExtensions: path_tracks = path_tracks + list(dir_scoped.glob(f"*{extension_audio!s}")) - # If it is not an album sort by modification time - if not is_album: + # Sort alphabetically, e.g. if items are prefixed with numbers + if sort_alphabetically: + path_tracks.sort() + elif not is_album: + # If it is not an album sort by modification time path_tracks.sort(key=lambda x: os.path.getmtime(x)) # Write data to m3u file |
