fix playlists

This commit is contained in:
cwilvx
2024-07-27 22:23:02 +03:00
parent b0e904c84f
commit 881e1d6581
5 changed files with 31 additions and 101 deletions
+19 -12
View File
@@ -22,10 +22,9 @@ from app.lib.home.recentlyplayed import get_recently_played_playlist
from app.models.playlist import Playlist
from app.serializers.playlist import serialize_for_card
from app.serializers.track import serialize_tracks
from app.store.playlists import PlaylistStore
from app.store.tracks import TrackStore
from app.utils.dates import create_new_date, date_string_to_time_passed
from app.utils.remove_duplicates import remove_duplicates
from app.settings import Paths
tag = Tag(name="Playlists", description="Get and manage playlists")
@@ -99,7 +98,16 @@ def send_all_playlists(query: SendAllPlaylistsQuery):
"""
Gets all the playlists.
"""
playlists = PlaylistStore.get_flat_list()
playlists = PlaylistTable.get_all()
for playlist in playlists:
if not playlist.has_image:
playlist.images = playlistlib.get_first_4_images(
trackhashes=playlist.trackhashes
)
playlist.clear_lists()
playlists.sort(
key=lambda p: datetime.strptime(p.last_updated, "%Y-%m-%d %H:%M:%S"),
reverse=True,
@@ -129,7 +137,6 @@ def create_playlist(body: CreatePlaylistBody):
if playlist is None:
return {"error": "Playlist could not be created"}, 500
PlaylistStore.add_playlist(playlist)
return {"playlist": playlist}, 201
@@ -199,20 +206,22 @@ def get_playlist(path: PlaylistIDPath, query: GetPlaylistQuery):
playlist, tracks = handler()
return format_custom_playlist(playlist, tracks)
entry = PlaylistStore.playlistmap.get(playlistid)
playlist = PlaylistTable.get_by_id(int(playlistid))
if entry is None:
if playlist is None:
return {"msg": "Playlist not found"}, 404
playlist = entry.playlist
if query.limit == -1:
query.limit = None
tracks = PlaylistStore.get_playlist_tracks(playlistid, query.start, query.limit)
query.limit = len(playlist.trackhashes) - 1
tracks = TrackStore.get_tracks_by_trackhashes(
playlist.trackhashes[query.start : query.start + query.limit]
)
duration = sum(t.duration for t in tracks)
playlist._last_updated = date_string_to_time_passed(playlist.last_updated)
playlist.duration = duration
playlist.images = playlistlib.get_first_4_images(tracks)
playlist.clear_lists()
return {
"info": playlist,
@@ -334,7 +343,6 @@ def remove_playlist(path: PlaylistIDPath):
Delete playlist
"""
PlaylistTable.remove_one(path.playlistid)
PlaylistStore.playlistmap.pop(path.playlistid, None)
return {"msg": "Done"}, 200
@@ -356,7 +364,6 @@ def remove_tracks_from_playlist(
# }
PlaylistTable.remove_from_playlist(path.playlistid, body.tracks)
PlaylistStore.remove_from_playlist(path.playlistid, body.tracks)
return {"msg": "Done"}, 200
+2 -4
View File
@@ -1,13 +1,12 @@
from datetime import datetime
import os
from app.db.userdata import FavoritesTable, ScrobbleTable
from app.db.userdata import FavoritesTable, PlaylistTable, ScrobbleTable
from app.models.playlist import Playlist
from app.serializers.track import serialize_track
from app.serializers.album import album_serializer
from app.lib.playlistlib import get_first_4_images
from app.store.folder import FolderStore
from app.store.playlists import PlaylistStore
from app.utils.dates import (
create_new_date,
date_string_to_time_passed,
@@ -148,8 +147,7 @@ def get_recently_played(limit=7):
)
continue
# playlist = pdb.get_playlist_by_id(entry.type_src)
playlist = PlaylistStore.get_playlist_by_id(entry.type_src)
playlist = PlaylistTable.get_by_id(entry.type_src)
if playlist is None:
continue
+9 -2
View File
@@ -13,6 +13,8 @@ from app import settings
from app.db.libdata import TrackTable
from app.models.track import Track
from app.store.albums import AlbumStore
from app.store.tracks import TrackStore
def create_thumbnail(image: Any, img_path: str) -> str:
"""
@@ -103,9 +105,14 @@ def duplicate_images(images: list):
def get_first_4_images(
tracks: list[Track] = [], trackhashes: list[str] = []
) -> list[dict["str", str]]:
"""
Returns images of the first 4 albums that appear in the track list.
When tracks are not passed, trackhashes need to be passed.
Tracks are then resolved from the store.
"""
if len(trackhashes) > 0:
# tracks = TrackStore.get_tracks_by_trackhashes(trackhashes)
tracks = TrackTable.get_tracks_by_trackhashes(trackhashes)
tracks = TrackStore.get_tracks_by_trackhashes(trackhashes)
albums = []
+1 -6
View File
@@ -8,7 +8,6 @@ from app.setup.sqlite import run_migrations, setup_sqlite
from app.store.albums import AlbumStore
from app.store.artists import ArtistStore
from app.store.folder import FolderStore
from app.store.playlists import PlaylistStore
from app.store.tracks import TrackStore
from app.utils.generators import get_random_str
from app.config import UserConfig
@@ -43,12 +42,8 @@ def load_into_mem():
"""
# instance_key = get_random_str()
# INFO: Load all tracks, albums, and artists into memory
# TrackStore.load_all_tracks(instance_key)
# AlbumStore.load_albums(instance_key)
# ArtistStore.load_artists(instance_key)
# INFO: Load all tracks, albums, and artists data into memory
TrackStore.load_all_tracks(get_random_str())
AlbumStore.load_albums('a')
ArtistStore.load_artists('a')
PlaylistStore.load_playlists()
FolderStore.load_filepaths()
-77
View File
@@ -1,77 +0,0 @@
from operator import index
from app.db.userdata import PlaylistTable
from app.lib.playlistlib import get_first_4_images
from app.models.playlist import Playlist
from app.store.tracks import TrackStore
class PlaylistEntry:
def __init__(self, playlist: Playlist) -> None:
self.playlist = playlist
self.trackhashes: list[str] = playlist.trackhashes
self.playlist.clear_lists()
if not playlist.has_image:
self.rebuild_images()
def rebuild_images(self):
self.playlist.images = get_first_4_images(
TrackStore.get_tracks_by_trackhashes(self.trackhashes)
)
class PlaylistStore:
playlistmap: dict[str, PlaylistEntry] = {}
@classmethod
def load_playlists(cls):
"""
Loads all playlists into the store.
"""
cls.playlistmap = {str(p.id): PlaylistEntry(p) for p in PlaylistTable.get_all()}
print(cls.playlistmap)
@classmethod
def get_playlist_tracks(cls, playlist_id: str, start: int, limit: int | None):
"""
Returns the trackhashes for a playlist.
"""
entry = cls.playlistmap.get(playlist_id)
if entry is None:
return []
if limit is None:
return TrackStore.get_tracks_by_trackhashes(entry.trackhashes[start:])
return TrackStore.get_tracks_by_trackhashes(
entry.trackhashes[start : start + limit]
)
@classmethod
def get_flat_list(cls):
return [p.playlist for p in cls.playlistmap.values()]
@classmethod
def add_playlist(cls, playlist: Playlist):
cls.playlistmap[str(playlist.id)] = PlaylistEntry(playlist)
@classmethod
def get_playlist_by_id(cls, id: str):
entry = cls.playlistmap.get(id)
if entry is not None:
return entry.playlist
@classmethod
def remove_from_playlist(cls, pid: str, tracks: list[dict[str, str]]):
playlist = cls.playlistmap.get(pid)
if not playlist:
return
for track in tracks:
if playlist.trackhashes.index(track["trackhash"]) == track["index"]:
playlist.trackhashes.remove(track["trackhash"])
playlist.rebuild_images()