summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Honz <exislow+github@gmail.com>2024-03-13 06:48:11 +0100
committerRobert Honz <exislow+github@gmail.com>2024-03-13 06:48:11 +0100
commite3a8588324328ee27edd4ff7dcab65d9dda7d31a (patch)
tree2738fb2bb4e5b8c7c3c009d5527162bfa06202f6
parentf4365e879b869e508d1e8cc5e94157c30950d799 (diff)
parentbbb6051fdd34fabf7565df480df66c128ffa4ef5 (diff)
downloadtidal-dl-ng-e3a8588324328ee27edd4ff7dcab65d9dda7d31a.tar.gz
Merge branch '41-request-song-organization'
* 41-request-song-organization: ✨ Track media items will be re-created with album information. Path format refactored. ♻️ Refactored format_str method to one `format_str_media` method. # Conflicts: # tidal_dl_ng/download.py # tidal_dl_ng/gui.py
-rw-r--r--tidal_dl_ng/download.py2
-rw-r--r--tidal_dl_ng/gui.py2
-rw-r--r--tidal_dl_ng/helper/path.py141
-rw-r--r--tidal_dl_ng/helper/tidal.py27
4 files changed, 88 insertions, 84 deletions
diff --git a/tidal_dl_ng/download.py b/tidal_dl_ng/download.py
index e4904b6..4d752cb 100644
--- a/tidal_dl_ng/download.py
+++ b/tidal_dl_ng/download.py
@@ -166,6 +166,8 @@ class Download:
# If no media instance is provided, we need to create the media instance.
if media_id and media_type:
media = instantiate_media(self.session, media_type, media_id)
+ elif isinstance(media, Track):
+ media = self.session.track(media.id, with_album=True)
elif not media:
raise MediaMissing
diff --git a/tidal_dl_ng/gui.py b/tidal_dl_ng/gui.py
index 6cd1e2d..0c8e97a 100644
--- a/tidal_dl_ng/gui.py
+++ b/tidal_dl_ng/gui.py
@@ -318,7 +318,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
else:
self.s_tr_results_add_top_level_item.emit(child)
- def populate_tree_result_child(self, item: [Mix | Album | Playlist], index_count_digits: int):
+ def populate_tree_result_child(self, item: [Track | Video | Mix | Album | Playlist], index_count_digits: int):
# TODO: Duration needs to be calculated later to properly fill with zeros.
# Format seconds to mm:ss.
m, s = divmod(item.duration_sec, 60)
diff --git a/tidal_dl_ng/helper/path.py b/tidal_dl_ng/helper/path.py
index 373f78c..65f9163 100644
--- a/tidal_dl_ng/helper/path.py
+++ b/tidal_dl_ng/helper/path.py
@@ -1,7 +1,7 @@
import glob
+import math
import os
import re
-from collections.abc import Callable
from pathlib import Path
from pathvalidate import sanitize_filename, sanitize_filepath
@@ -66,85 +66,90 @@ def format_path_media(fmt_template: str, media: Track | Album | Playlist | UserP
# Search track format template for placeholder.
regex = r"\{(.+?)\}"
matches = re.finditer(regex, fmt_template, re.MULTILINE)
- fn_format = get_format_fn(media)
for _matchNum, match in enumerate(matches, start=1):
template_str = match.group()
- result_fmt = fn_format(match.group(1), media)
+ result_fmt = format_str_media(match.group(1), media)
- if result_fmt:
+ if result_fmt != match.group(1):
value = sanitize_filename(result_fmt)
result = result.replace(template_str, value)
return result
-def format_str_track(name: str, media: Track) -> str | bool:
- result: str | bool = False
+def format_str_media(name: str, media: Track | Album | Playlist | UserPlaylist | Video | Mix) -> str:
+ result: str = name
- if name == "track_num":
- result = str(media.track_num).rjust(2, "0")
- elif name == "artist_name":
- result = ", ".join(artist.name for artist in media.artists)
- elif name == "track_title":
- result = media.name
-
- return result
-
-
-def format_str_album(name: str, media: Album) -> str | bool:
- result: str | bool = False
-
- if name == "album_title":
- result = media.name
- elif name == "artist_name":
- result = media.artist.name
-
- return result
-
-
-def format_str_playlist(name: str, media: Playlist) -> str | bool:
- result: str | bool = False
-
- if name == "playlist_name":
- result = media.name
-
- return result
-
-
-def format_str_mix(name: str, media: Mix) -> str | bool:
- result: str | bool = False
-
- if name == "mix_name":
- result = media.title
-
- return result
-
-
-def format_str_video(name: str, media: Video) -> str | bool:
- result: str | bool = False
-
- if name == "artist_name":
- result = ", ".join(artist.name for artist in media.artists)
- elif name == "track_title":
- result = media.name
-
- return result
-
-
-def get_format_fn(media: Track | Album | Playlist | UserPlaylist | Video | Mix) -> Callable:
- result = None
+ try:
+ if name == "artist_name":
+ if hasattr(media, "artists"):
+ result = ", ".join(artist.name for artist in media.artists)
+ elif hasattr(media, "artist"):
+ result = media.artist.name
+ elif name == "track_title":
+ if isinstance(media, Track | Video):
+ result = media.name
+ elif name == "mix_name":
+ if isinstance(media, Mix):
+ result = media.title
+ elif name == "playlist_name":
+ if isinstance(media, Playlist | UserPlaylist):
+ result = media.name
+ elif name == "album_title":
+ if isinstance(media, Album):
+ result = media.name
+ elif isinstance(media, Track):
+ result = media.album.name
+ elif name == "album_track_num":
+ if isinstance(media, Track | Video):
+ num_tracks: int = media.album.num_tracks if hasattr(media, "album") else 1
+ count_digits: int = int(math.log10(num_tracks)) + 1
+ result = str(media.track_num).zfill(count_digits)
+ elif name == "album_num_tracks":
+ if isinstance(media, Track | Video):
+ result = str(media.album.num_tracks if hasattr(media, "album") else 1)
+ elif name == "track_id":
+ if isinstance(media, Track | Video):
+ result = media.id
+ elif name == "playlist_id":
+ if isinstance(media, Playlist):
+ result = media.id
+ elif name == "track_duration_seconds":
+ if isinstance(media, Track | Video):
+ result = str(media.duration)
+ elif name == "track_duration_minutes":
+ if isinstance(media, Track | Video):
+ m, s = divmod(media.duration, 60)
+ result = f"{m:01d}:{s:02d}"
+ elif name == "album_duration_seconds":
+ if isinstance(media, Album):
+ result = str(media.duration)
+ elif name == "album_duration_minutes":
+ if isinstance(media, Album):
+ m, s = divmod(media.duration, 60)
+ result = f"{m:01d}:{s:02d}"
+ elif name == "playlist_duration_seconds":
+ if isinstance(media, Album):
+ result = str(media.duration)
+ elif name == "playlist_duration_minutes":
+ if isinstance(media, Album):
+ m, s = divmod(media.duration, 60)
+ result = f"{m:01d}:{s:02d}"
+ elif name == "album_year":
+ if isinstance(media, Album):
+ result = str(media.release_date.year)
+ elif name == "video_quality":
+ if isinstance(media, Video):
+ result = media.video_quality
+ elif name == "track_quality":
+ if isinstance(media, Track):
+ result = ", ".join(tag for tag in media.media_metadata_tags)
+ except Exception as e:
+ # TODO: Implement better exception logging.
+ print(e)
- if isinstance(media, Track):
- result = format_str_track
- elif isinstance(media, Album):
- result = format_str_album
- elif isinstance(media, Playlist | UserPlaylist):
- result = format_str_playlist
- elif isinstance(media, Mix):
- result = format_str_mix
- elif isinstance(media, Video):
- result = format_str_video
+ pass
return result
diff --git a/tidal_dl_ng/helper/tidal.py b/tidal_dl_ng/helper/tidal.py
index e13c909..4ae57bd 100644
--- a/tidal_dl_ng/helper/tidal.py
+++ b/tidal_dl_ng/helper/tidal.py
@@ -111,21 +111,18 @@ def instantiate_media(
session: Session,
media_type: type[MediaType.TRACK, MediaType.VIDEO, MediaType.ALBUM, MediaType.PLAYLIST, MediaType.MIX],
id_media: str,
-) -> Track | Video:
- try:
- if media_type == MediaType.TRACK:
- media = Track(session, id_media)
- elif media_type == MediaType.VIDEO:
- media = Video(session, id_media)
- elif media_type == MediaType.ALBUM:
- media = Album(session, id_media)
- elif media_type == MediaType.PLAYLIST:
- media = Playlist(session, id_media)
- elif media_type == MediaType.MIX:
- media = Mix(session, id_media)
- else:
- raise MediaUnknown
- except:
+) -> Track | Video | Album | Playlist | Mix:
+ if media_type == MediaType.TRACK:
+ media = session.track(id_media, with_album=True)
+ elif media_type == MediaType.VIDEO:
+ media = session.video(id_media)
+ elif media_type == MediaType.ALBUM:
+ media = session.album(id_media)
+ elif media_type == MediaType.PLAYLIST:
+ media = session.playlist(id_media)
+ elif media_type == MediaType.MIX:
+ media = session.mix(id_media)
+ else:
raise MediaUnknown
return media