diff --git a/app/api/artist.py b/app/api/artist.py index 3c8b3f75..45d1d65d 100644 --- a/app/api/artist.py +++ b/app/api/artist.py @@ -199,10 +199,9 @@ def get_artist(artisthash: str): if acount == 0 and tcount < 10: limit = tcount - artist.trackcount = tcount - artist.albumcount = acount - - artist.duration = sum(t.duration for t in tracks) + artist.set_trackcount(tcount) + artist.set_albumcount(acount) + artist.set_duration(sum(t.duration for t in tracks)) artist.is_favorite = favdb.check_is_favorite(artisthash, FavType.artist) diff --git a/app/api/playlist.py b/app/api/playlist.py index 69531fc5..c1c7abf2 100644 --- a/app/api/playlist.py +++ b/app/api/playlist.py @@ -80,8 +80,7 @@ def send_all_playlists(): playlist.images = get_first_4_images(playlist.trackhashes) playlist.images = [img['image'] for img in playlist.images] - playlist.trackhashes = [] - playlist.artisthashes = [] + playlist.clear_lists() playlists.sort( key=lambda p: datetime.strptime(p.last_updated, "%Y-%m-%d %H:%M:%S"), @@ -163,7 +162,7 @@ def get_playlist(playlistid: str): duration = sum(t.duration for t in tracks) playlist.last_updated = date_string_to_time_passed(playlist.last_updated) - playlist.duration = duration + playlist.set_duration(duration) if not playlist.has_image: playlist.images = get_first_4_images(playlist.trackhashes) @@ -172,8 +171,7 @@ def get_playlist(playlistid: str): # swap 3rd image with first (3rd image is the visible image in UI) playlist.images[2], playlist.images[0] = playlist.images[0], playlist.images[2] - playlist.trackhashes = [] - playlist.artisthashes = [] + playlist.clear_lists() return {"info": playlist, "tracks": tracks} diff --git a/app/lib/taglib.py b/app/lib/taglib.py index b0141a32..9102b99b 100644 --- a/app/lib/taglib.py +++ b/app/lib/taglib.py @@ -64,16 +64,11 @@ def extract_thumb(filepath: str, webp_path: str) -> bool: return False -def extract_date(date_str: str | None) -> int: - current_year = datetime.date.today().today().year - - if date_str is None: - return current_year - +def extract_date(date_str: str | None, filepath: str) -> int: try: return int(date_str.split("-")[0]) except: # pylint: disable=bare-except - return current_year + return datetime.date.today().today().year def get_tags(filepath: str): @@ -145,7 +140,7 @@ def get_tags(filepath: str): tags.image = f"{tags.albumhash}.webp" tags.folder = win_replace_slash(os.path.dirname(filepath)) - tags.date = extract_date(tags.year) + tags.date = extract_date(tags.year, filepath) tags.filepath = win_replace_slash(filepath) tags.filetype = filetype diff --git a/app/models/artist.py b/app/models/artist.py index 2a525f43..0c826064 100644 --- a/app/models/artist.py +++ b/app/models/artist.py @@ -1,5 +1,5 @@ -import dataclasses import json +import dataclasses from dataclasses import dataclass from app.utils.hashing import create_hash @@ -33,11 +33,18 @@ class Artist(ArtistMinimal): colors: list[str] = dataclasses.field(default_factory=list) is_favorite: bool = False - def __post_init__(self): - super(Artist, self).__init__(self.name) + def __init__(self, name: str): + super(Artist, self).__init__(name) self.colors = json.loads(str(self.colors)) + def set_trackcount(self, count: int): + self.trackcount = count + + def set_albumcount(self, count: int): + self.albumcount = count + + def set_duration(self, duration: int): + self.duration = duration + def set_colors(self, colors: list[str]): self.colors = colors - -# TODO: Use inheritance to create the classes in this file. diff --git a/app/models/playlist.py b/app/models/playlist.py index dcabfdb3..8e4fefd0 100644 --- a/app/models/playlist.py +++ b/app/models/playlist.py @@ -40,3 +40,14 @@ class Playlist: else: self.image = "None" self.thumb = "None" + + def set_duration(self, duration: int): + self.duration = duration + + def clear_lists(self): + """ + Removes data from lists to make it lighter for sending + over the API. + """ + self.trackhashes = [] + self.artisthashes = [] diff --git a/app/store/artists.py b/app/store/artists.py index b8177700..b6ad3dcf 100644 --- a/app/store/artists.py +++ b/app/store/artists.py @@ -36,7 +36,7 @@ class ArtistStore: for artist in cls.artists: if artist.artisthash == artisthash: - artist.colors = color + artist.set_colors(color) break @classmethod