From d21a834a00a12ea9f47e5fcd6f074ff0471afd11 Mon Sep 17 00:00:00 2001 From: geoffrey45 Date: Wed, 15 Mar 2023 10:01:12 +0300 Subject: [PATCH] fix get playlist tracks order + fix utils.get_home_res_path --- app/api/playlist.py | 41 +++++++++++++++++++++++++++++++---------- app/db/store.py | 17 ++++++++++++++++- app/lib/playlistlib.py | 2 +- app/models/playlist.py | 11 ++++++++++- app/serializer.py | 2 +- app/settings.py | 2 ++ app/utils/filesystem.py | 2 +- 7 files changed, 62 insertions(+), 15 deletions(-) diff --git a/app/api/playlist.py b/app/api/playlist.py index 32b49a4..48c5441 100644 --- a/app/api/playlist.py +++ b/app/api/playlist.py @@ -28,6 +28,7 @@ add_artist_to_playlist = PL.add_artist_to_playlist update_playlist = PL.update_playlist delete_playlist = PL.delete_playlist + # get_tracks_by_trackhashes = SQLiteTrackMethods.get_tracks_by_trackhashes @@ -116,11 +117,41 @@ def get_playlist(playlistid: str): tracks = Store.get_tracks_by_trackhashes(list(playlist.trackhashes)) tracks = remove_duplicates(tracks) + playlist.trackhashes = [] + duration = sum(t.duration for t in tracks) playlist.last_updated = serializer.date_string_to_time_passed(playlist.last_updated) playlist.duration = duration + if not playlist.has_image: + albums = [] + for track in tracks: + if track.albumhash not in albums: + albums.append(track.albumhash) + if len(albums) == 4: + break + + albums = Store.get_albums_by_hashes(albums) + playlist.images = [ + { + 'image': album.image, + 'color': ''.join(album.colors), + } + for album in albums + ] + + if len(playlist.images) == 1: + playlist.images = playlist.images * 4 + elif len(playlist.images) == 2: + playlist.images = playlist.images * 2 + elif len(playlist.images) == 3: + playlist.images = playlist.images + playlist.images[:1] + + # swap 3rd image with first (3rd image is the visible image in UI) + if len(playlist.images) > 2: + playlist.images[2], playlist.images[0] = playlist.images[0], playlist.images[2] + return {"info": playlist, "tracks": tracks} @@ -178,16 +209,6 @@ def update_playlist_info(playlistid: str): } -# @playlist_bp.route("/playlist/artists", methods=["POST"]) -# def get_playlist_artists(): -# data = request.get_json() - -# pid = data["pid"] -# artists = playlistlib.GetPlaylistArtists(pid)() - -# return {"data": artists} - - @api.route("/playlist/delete", methods=["POST"]) def remove_playlist(): """ diff --git a/app/db/store.py b/app/db/store.py index 8722133..51b3777 100644 --- a/app/db/store.py +++ b/app/db/store.py @@ -68,7 +68,10 @@ class Store: """ trackhashes = " ".join(trackhashes) - return [track for track in cls.tracks if track.trackhash in trackhashes] + tracks = [track for track in cls.tracks if track.trackhash in trackhashes] + + tracks.sort(key=lambda t: trackhashes.index(t.trackhash)) + return tracks @classmethod def remove_track_by_filepath(cls, filepath: str): @@ -355,6 +358,18 @@ class Store: except IndexError: return None + @classmethod + def get_albums_by_hashes(cls, albumhashes: list[str]) -> list[Album]: + """ + Returns albums by their hashes. + """ + albums_str = "-".join(albumhashes) + albums = [a for a in cls.albums if a.albumhash in albums_str] + + # sort albums by the order of the hashes + albums.sort(key=lambda x: albumhashes.index(x.albumhash)) + return albums + @classmethod def get_albums_by_artisthash(cls, artisthash: str) -> list[Album]: """ diff --git a/app/lib/playlistlib.py b/app/lib/playlistlib.py index 63c6866..104743b 100644 --- a/app/lib/playlistlib.py +++ b/app/lib/playlistlib.py @@ -60,7 +60,7 @@ def save_p_image(file, pid: str): filename = pid + str(random_str) + ".webp" - full_img_path = os.path.join(settings.APP_DIR, "images", "playlists", filename) + full_img_path = os.path.join(settings.PLAYLIST_IMG_PATH, filename) if file.content_type == "image/gif": frames = [] diff --git a/app/models/playlist.py b/app/models/playlist.py index 8d4ece3..d8c8824 100644 --- a/app/models/playlist.py +++ b/app/models/playlist.py @@ -1,5 +1,9 @@ +import dataclasses import json from dataclasses import dataclass +from pathlib import Path + +from app import settings @dataclass(slots=True) @@ -18,13 +22,18 @@ class Playlist: thumb: str = "" count: int = 0 duration: int = 0 + has_image: bool = False + images: list[str] = dataclasses.field(default_factory=list) def __post_init__(self): self.trackhashes = json.loads(str(self.trackhashes)) - self.artisthashes = json.loads(str(self.artisthashes)) + # self.artisthashes = json.loads(str(self.artisthashes)) + # commentted until we need it 👆 + self.artisthashes = [] self.count = len(self.trackhashes) self.has_gif = bool(int(self.has_gif)) + self.has_image = (Path(settings.PLAYLIST_IMG_PATH) / str(self.image)).exists() if self.image is not None: self.thumb = "thumb_" + self.image diff --git a/app/serializer.py b/app/serializer.py index 954216f..b73c584 100644 --- a/app/serializer.py +++ b/app/serializer.py @@ -14,7 +14,7 @@ def date_string_to_time_passed(prev_date: str) -> str: seconds = diff.total_seconds() if seconds < 0: - return "in the future" + return "-from a time machine 🛸" if seconds < 15: return "now" diff --git a/app/settings.py b/app/settings.py index ff07760..21db4c4 100644 --- a/app/settings.py +++ b/app/settings.py @@ -43,6 +43,8 @@ ARTIST_IMG_PATH = os.path.join(IMG_PATH, "artists") ARTIST_IMG_SM_PATH = os.path.join(ARTIST_IMG_PATH, "small") ARTIST_IMG_LG_PATH = os.path.join(ARTIST_IMG_PATH, "large") +PLAYLIST_IMG_PATH = os.path.join(IMG_PATH, "playlists") + THUMBS_PATH = os.path.join(IMG_PATH, "thumbnails") SM_THUMB_PATH = os.path.join(THUMBS_PATH, "small") LG_THUMBS_PATH = os.path.join(THUMBS_PATH, "large") diff --git a/app/utils/filesystem.py b/app/utils/filesystem.py index 17b9c11..304210b 100644 --- a/app/utils/filesystem.py +++ b/app/utils/filesystem.py @@ -45,6 +45,6 @@ def get_home_res_path(filename: str): Used to resolve resources in builds. """ try: - return (CWD / ".." / filename).resolve() + return (CWD / ".." / ".." / filename).resolve() except ValueError: return None