fix get playlist tracks order

+ fix utils.get_home_res_path
This commit is contained in:
geoffrey45 2023-03-15 10:01:12 +03:00
parent 64bb4f75df
commit d21a834a00
7 changed files with 62 additions and 15 deletions

View File

@ -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():
"""

View File

@ -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]:
"""

View File

@ -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 = []

View File

@ -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

View File

@ -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"

View File

@ -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")

View File

@ -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