♻️ Refactored format_str method to one format_str_media method.
This commit is contained in:
+1
-1
@@ -310,7 +310,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):
|
||||
# Format seconds to mm:ss.
|
||||
m, s = divmod(item.duration_sec, 60)
|
||||
duration: str = f"{m:02d}:{s:02d}"
|
||||
|
||||
+73
-69
@@ -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,89 @@ 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
|
||||
|
||||
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
|
||||
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 == "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(len(str(num_tracks)))) + 1
|
||||
result = str(media.track_num).zfill(count_digits)
|
||||
elif name == "track_id":
|
||||
if isinstance(media, Track | Video):
|
||||
result = media.id
|
||||
elif name == "track_id":
|
||||
if isinstance(media, Track):
|
||||
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)
|
||||
pass
|
||||
|
||||
return result
|
||||
|
||||
|
||||
Reference in New Issue
Block a user