mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-03 20:13:02 +00:00
port recents endpoints to use stores
This commit is contained in:
@@ -59,8 +59,8 @@
|
|||||||
- Replace the DbManager class with cls.execute()
|
- Replace the DbManager class with cls.execute()
|
||||||
|
|
||||||
- Paginate the following endpoints:
|
- Paginate the following endpoints:
|
||||||
|
|
||||||
1. Folder tracks
|
1. Folder tracks
|
||||||
2. Playlist tracks
|
2. Playlist tracks ⭐
|
||||||
|
|
||||||
|
- When you update a playlist, update the store as well!
|
||||||
- When you update a playlist, update the store as well!
|
|
||||||
|
|||||||
+4
-4
@@ -81,13 +81,12 @@ def get_artist_trackhashes(artisthash: str):
|
|||||||
|
|
||||||
|
|
||||||
def format_custom_playlist(playlist: models.Playlist, tracks: list[models.Track]):
|
def format_custom_playlist(playlist: models.Playlist, tracks: list[models.Track]):
|
||||||
duration = sum(t.duration for t in tracks)
|
playlist.duration = sum(t.duration for t in tracks)
|
||||||
|
playlist.count = len(tracks)
|
||||||
playlist.duration = duration
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"info": serialize_for_card(playlist),
|
"info": serialize_for_card(playlist),
|
||||||
"tracks": tracks,
|
"tracks": serialize_tracks(tracks),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -354,6 +353,7 @@ def remove_tracks_from_playlist(
|
|||||||
# }
|
# }
|
||||||
|
|
||||||
PlaylistTable.remove_from_playlist(path.playlistid, body.tracks)
|
PlaylistTable.remove_from_playlist(path.playlistid, body.tracks)
|
||||||
|
PlaylistStore.remove_from_playlist(path.playlistid, body.tracks)
|
||||||
|
|
||||||
return {"msg": "Done"}, 200
|
return {"msg": "Done"}, 200
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,13 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pprint import pprint
|
|
||||||
from time import time
|
from time import time
|
||||||
|
|
||||||
from app.db.libdata import AlbumTable, ArtistTable, TrackTable
|
|
||||||
from app.lib.playlistlib import get_first_4_images
|
from app.lib.playlistlib import get_first_4_images
|
||||||
from app.models.playlist import Playlist
|
from app.models.playlist import Playlist
|
||||||
from app.models.track import Track
|
from app.models.track import Track
|
||||||
|
|
||||||
# from app.store.tracks import TrackStore
|
from app.store.tracks import TrackStore
|
||||||
# from app.store.albums import AlbumStore
|
from app.store.albums import AlbumStore
|
||||||
# from app.store.artists import ArtistStore
|
from app.store.artists import ArtistStore
|
||||||
|
|
||||||
from app.serializers.track import serialize_track
|
from app.serializers.track import serialize_track
|
||||||
from app.serializers.album import album_serializer
|
from app.serializers.album import album_serializer
|
||||||
@@ -89,21 +87,21 @@ def create_track(t: Track):
|
|||||||
|
|
||||||
|
|
||||||
# INFO: Keys: folder, tracks, time (timestamp)
|
# INFO: Keys: folder, tracks, time (timestamp)
|
||||||
group_type = dict[str, list[Track], float]
|
# group_type = dict[str, str | list[Track] | float]
|
||||||
|
|
||||||
|
|
||||||
def check_folder_type(group_: group_type):
|
def check_folder_type(group_: dict):
|
||||||
# check if all tracks in group have the same albumhash
|
# check if all tracks in group have the same albumhash
|
||||||
# if so, return "album"
|
# if so, return "album"
|
||||||
key = group_["folder"]
|
key: str = group_["folder"]
|
||||||
tracks = group_["tracks"]
|
tracks: list[Track] = group_["tracks"]
|
||||||
time = group_["time"]
|
time: float = group_["time"]
|
||||||
|
|
||||||
print(f"Checking folder: {key}")
|
print(f"Checking folder: {key}")
|
||||||
print(f"Tracks: {len(tracks)}")
|
print(f"Tracks: {len(tracks)}")
|
||||||
|
|
||||||
existing_artist_hashes: set[str] = set(ArtistTable.get_all_hashes(time))
|
existing_artist_hashes: set[str] = set(ArtistStore.artistmap.keys())
|
||||||
existing_album_hashes: set[str] = set(AlbumTable.get_all_hashes(time))
|
existing_album_hashes: set[str] = set(AlbumStore.albummap.keys())
|
||||||
|
|
||||||
if len(tracks) == 1:
|
if len(tracks) == 1:
|
||||||
entry = create_track(tracks[0])
|
entry = create_track(tracks[0])
|
||||||
@@ -112,13 +110,14 @@ def check_folder_type(group_: group_type):
|
|||||||
|
|
||||||
is_album, albumhash, _ = check_is_album_folder(tracks)
|
is_album, albumhash, _ = check_is_album_folder(tracks)
|
||||||
if is_album:
|
if is_album:
|
||||||
album = AlbumTable.get_album_by_albumhash(albumhash)
|
# album = AlbumTable.get_album_by_albumhash(albumhash)
|
||||||
|
entry = AlbumStore.albummap.get(albumhash)
|
||||||
|
|
||||||
if album is None:
|
if entry is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
album = album_serializer(
|
album = album_serializer(
|
||||||
album,
|
entry.album,
|
||||||
to_remove={
|
to_remove={
|
||||||
"genres",
|
"genres",
|
||||||
"og_title",
|
"og_title",
|
||||||
@@ -141,12 +140,12 @@ def check_folder_type(group_: group_type):
|
|||||||
|
|
||||||
is_artist, artisthash, trackcount = check_is_artist_folder(tracks)
|
is_artist, artisthash, trackcount = check_is_artist_folder(tracks)
|
||||||
if is_artist:
|
if is_artist:
|
||||||
artist = ArtistTable.get_artist_by_hash(artisthash)
|
entry = ArtistStore.artistmap.get(artisthash)
|
||||||
|
|
||||||
if artist is None:
|
if entry is None:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
artist = serialize_for_card(artist)
|
artist = serialize_for_card(entry.artist)
|
||||||
artist["trackcount"] = trackcount
|
artist["trackcount"] = trackcount
|
||||||
artist["help_text"] = (
|
artist["help_text"] = (
|
||||||
"NEW ARTIST" if artisthash not in existing_artist_hashes else "NEW MUSIC"
|
"NEW ARTIST" if artisthash not in existing_artist_hashes else "NEW MUSIC"
|
||||||
@@ -200,7 +199,7 @@ def get_recently_added_items(limit: int = 7):
|
|||||||
print(f"Time taken to get tracks: {then - now}")
|
print(f"Time taken to get tracks: {then - now}")
|
||||||
groups = group_track_by_folders(tracks, {})
|
groups = group_track_by_folders(tracks, {})
|
||||||
# print(groups)
|
# print(groups)
|
||||||
last_trackcount: int = len(tracks)
|
# last_trackcount: int = len(tracks)
|
||||||
|
|
||||||
# while len(groups.keys()) < limit and last_trackcount > 0:
|
# while len(groups.keys()) < limit and last_trackcount > 0:
|
||||||
# distracks = get_recently_added_tracks(start=len(tracks), limit=100)
|
# distracks = get_recently_added_tracks(start=len(tracks), limit=100)
|
||||||
@@ -223,8 +222,6 @@ def get_recently_added_items(limit: int = 7):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
pprint(f"😅😅😅😅😅😅😅😅😅😅😅😅😅😅😅😅 {grouplist[0]['len']}")
|
|
||||||
|
|
||||||
# sort groups by last modified date
|
# sort groups by last modified date
|
||||||
grouplist = sorted(grouplist, key=lambda group: group["time"], reverse=True)
|
grouplist = sorted(grouplist, key=lambda group: group["time"], reverse=True)
|
||||||
|
|
||||||
@@ -267,16 +264,14 @@ def get_recently_added_playlist(limit: int = 100):
|
|||||||
except IndexError:
|
except IndexError:
|
||||||
return playlist, []
|
return playlist, []
|
||||||
|
|
||||||
playlist.last_updated = date_string_to_time_passed(create_new_date(date))
|
playlist._last_updated = date_string_to_time_passed(create_new_date(date))
|
||||||
|
|
||||||
images = get_first_4_images(tracks=tracks)
|
images = get_first_4_images(tracks=tracks)
|
||||||
playlist.images = images
|
playlist.images = images
|
||||||
playlist.set_count(len(tracks))
|
playlist.duration = sum(t.duration for t in tracks)
|
||||||
|
playlist.count = len(tracks)
|
||||||
|
|
||||||
return playlist, tracks
|
return playlist, tracks
|
||||||
|
|
||||||
|
|
||||||
def get_recently_added_tracks(start: int = 0, limit: int = 100):
|
def get_recently_added_tracks(start: int = 0, limit: int | None = 100):
|
||||||
# tracks = sorted(TrackStore.tracks, key=lambda t: t.created_date, reverse=True)
|
return TrackStore.get_recently_added(start, limit)
|
||||||
return TrackTable.get_recently_added(start, limit)
|
|
||||||
# return tracks[:limit]
|
|
||||||
|
|||||||
@@ -1,18 +1,13 @@
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import os
|
import os
|
||||||
from app.db.libdata import AlbumTable, ArtistTable, TrackTable
|
|
||||||
from app.db.userdata import FavoritesTable, ScrobbleTable
|
from app.db.userdata import FavoritesTable, ScrobbleTable
|
||||||
from app.models.logger import TrackLog
|
|
||||||
|
|
||||||
# from app.db.sqlite.logger.tracks import SQLiteTrackLogger as db
|
|
||||||
# from app.db.sqlite.playlists import SQLitePlaylistMethods as pdb
|
|
||||||
# from app.db.sqlite.favorite import SQLiteFavoriteMethods as fdb
|
|
||||||
|
|
||||||
from app.models.playlist import Playlist
|
from app.models.playlist import Playlist
|
||||||
from app.serializers.track import serialize_track
|
from app.serializers.track import serialize_track
|
||||||
from app.serializers.album import album_serializer
|
from app.serializers.album import album_serializer
|
||||||
from app.lib.playlistlib import get_first_4_images
|
from app.lib.playlistlib import get_first_4_images
|
||||||
from app.store.folder import FolderStore
|
from app.store.folder import FolderStore
|
||||||
|
from app.store.playlists import PlaylistStore
|
||||||
from app.utils.dates import (
|
from app.utils.dates import (
|
||||||
create_new_date,
|
create_new_date,
|
||||||
date_string_to_time_passed,
|
date_string_to_time_passed,
|
||||||
@@ -22,14 +17,13 @@ from app.serializers.artist import serialize_for_card
|
|||||||
from app.serializers.playlist import serialize_for_card as serialize_playlist
|
from app.serializers.playlist import serialize_for_card as serialize_playlist
|
||||||
from app.lib.home.recentlyadded import get_recently_added_playlist
|
from app.lib.home.recentlyadded import get_recently_added_playlist
|
||||||
|
|
||||||
# from app.store.albums import AlbumStore
|
from app.store.albums import AlbumStore
|
||||||
# from app.store.tracks import TrackStore
|
from app.store.tracks import TrackStore
|
||||||
# from app.store.artists import ArtistStore
|
from app.store.artists import ArtistStore
|
||||||
|
|
||||||
|
|
||||||
def get_recently_played(limit=7):
|
def get_recently_played(limit=7):
|
||||||
# TODO: Paginate this
|
# TODO: Paginate this
|
||||||
# entries = db.get_all()
|
|
||||||
entries = ScrobbleTable.get_all(0, 200)
|
entries = ScrobbleTable.get_all(0, 200)
|
||||||
items = []
|
items = []
|
||||||
added = set()
|
added = set()
|
||||||
@@ -43,16 +37,13 @@ def get_recently_played(limit=7):
|
|||||||
if len(items) >= limit:
|
if len(items) >= limit:
|
||||||
break
|
break
|
||||||
|
|
||||||
# entry = TrackLog(*entry)
|
|
||||||
|
|
||||||
if entry.source in added:
|
if entry.source in added:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
added.add(entry.source)
|
added.add(entry.source)
|
||||||
|
|
||||||
if entry.type == "album":
|
if entry.type == "album":
|
||||||
# album = AlbumStore.get_album_by_hash(entry.type_src)
|
album = AlbumStore.get_album_by_hash(entry.type_src)
|
||||||
album = AlbumTable.get_album_by_albumhash(entry.type_src)
|
|
||||||
|
|
||||||
if album is None:
|
if album is None:
|
||||||
continue
|
continue
|
||||||
@@ -80,8 +71,7 @@ def get_recently_played(limit=7):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
if entry.type == "artist":
|
if entry.type == "artist":
|
||||||
# artist = ArtistStore.get_artist_by_hash(entry.type_src)
|
artist = ArtistStore.get_artist_by_hash(entry.type_src)
|
||||||
artist = ArtistTable.get_artist_by_hash(entry.type_src)
|
|
||||||
|
|
||||||
if artist is None:
|
if artist is None:
|
||||||
continue
|
continue
|
||||||
@@ -132,7 +122,6 @@ def get_recently_played(limit=7):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
if entry.type == "playlist":
|
if entry.type == "playlist":
|
||||||
continue
|
|
||||||
is_custom = entry.type_src in [i["name"] for i in custom_playlists]
|
is_custom = entry.type_src in [i["name"] for i in custom_playlists]
|
||||||
# is_recently_added = entry.type_src == "recentlyadded"
|
# is_recently_added = entry.type_src == "recentlyadded"
|
||||||
|
|
||||||
@@ -159,7 +148,8 @@ def get_recently_played(limit=7):
|
|||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
playlist = pdb.get_playlist_by_id(entry.type_src)
|
# playlist = pdb.get_playlist_by_id(entry.type_src)
|
||||||
|
playlist = PlaylistStore.get_playlist_by_id(entry.type_src)
|
||||||
if playlist is None:
|
if playlist is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@@ -195,13 +185,12 @@ def get_recently_played(limit=7):
|
|||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# track = TrackStore.get_tracks_by_trackhashes([entry.trackhash])[0]
|
t = TrackStore.trackhashmap.get(entry.trackhash)
|
||||||
track = TrackTable.get_track_by_trackhash(entry.trackhash)
|
|
||||||
|
|
||||||
if track is None:
|
if t is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
track = serialize_track(track)
|
track = serialize_track(t.get_best())
|
||||||
track["help_text"] = "track"
|
track["help_text"] = "track"
|
||||||
track["time"] = timestamp_to_time_passed(entry.timestamp)
|
track["time"] = timestamp_to_time_passed(entry.timestamp)
|
||||||
|
|
||||||
@@ -225,12 +214,11 @@ def get_recently_played_playlist(limit: int = 100):
|
|||||||
trackhashes=[],
|
trackhashes=[],
|
||||||
)
|
)
|
||||||
|
|
||||||
tracks = TrackTable.get_recently_played(limit)
|
tracks = TrackStore.get_recently_played(limit)
|
||||||
date = datetime.fromtimestamp(tracks[0].lastplayed)
|
date = datetime.fromtimestamp(tracks[0].lastplayed)
|
||||||
playlist.last_updated = date_string_to_time_passed(create_new_date(date))
|
playlist._last_updated = date_string_to_time_passed(create_new_date(date))
|
||||||
|
|
||||||
images = get_first_4_images(tracks=tracks)
|
images = get_first_4_images(tracks=tracks)
|
||||||
playlist.images = images
|
playlist.images = images
|
||||||
playlist.count = len(tracks)
|
|
||||||
|
|
||||||
return playlist, tracks
|
return playlist, tracks
|
||||||
|
|||||||
+3
-5
@@ -125,11 +125,9 @@ class AlbumStore:
|
|||||||
"""
|
"""
|
||||||
Returns an album by its hash.
|
Returns an album by its hash.
|
||||||
"""
|
"""
|
||||||
for album in cls.albums:
|
entry = cls.albummap.get(albumhash)
|
||||||
if album.albumhash == albumhash:
|
if entry is not None:
|
||||||
return album
|
return entry.album
|
||||||
|
|
||||||
return None
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_albums_by_hashes(cls, albumhashes: Iterable[str]) -> list[Album]:
|
def get_albums_by_hashes(cls, albumhashes: Iterable[str]) -> list[Album]:
|
||||||
|
|||||||
+27
-3
@@ -1,3 +1,4 @@
|
|||||||
|
from operator import index
|
||||||
from app.db.userdata import PlaylistTable
|
from app.db.userdata import PlaylistTable
|
||||||
from app.lib.playlistlib import get_first_4_images
|
from app.lib.playlistlib import get_first_4_images
|
||||||
from app.models.playlist import Playlist
|
from app.models.playlist import Playlist
|
||||||
@@ -11,9 +12,12 @@ class PlaylistEntry:
|
|||||||
self.playlist.clear_lists()
|
self.playlist.clear_lists()
|
||||||
|
|
||||||
if not playlist.has_image:
|
if not playlist.has_image:
|
||||||
self.playlist.images = get_first_4_images(
|
self.rebuild_images()
|
||||||
TrackStore.get_tracks_by_trackhashes(self.trackhashes)
|
|
||||||
)
|
def rebuild_images(self):
|
||||||
|
self.playlist.images = get_first_4_images(
|
||||||
|
TrackStore.get_tracks_by_trackhashes(self.trackhashes)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class PlaylistStore:
|
class PlaylistStore:
|
||||||
@@ -48,3 +52,23 @@ class PlaylistStore:
|
|||||||
@classmethod
|
@classmethod
|
||||||
def add_playlist(cls, playlist: Playlist):
|
def add_playlist(cls, playlist: Playlist):
|
||||||
cls.playlistmap[str(playlist.id)] = PlaylistEntry(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()
|
||||||
@@ -350,3 +350,20 @@ class TrackStore:
|
|||||||
predicate=predicate,
|
predicate=predicate,
|
||||||
including_duplicates=True,
|
including_duplicates=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_recently_added(cls, start: int, limit: int | None):
|
||||||
|
"""
|
||||||
|
Returns the most recently added tracks.
|
||||||
|
"""
|
||||||
|
tracks = cls.get_flat_list()
|
||||||
|
|
||||||
|
if limit is None:
|
||||||
|
return sorted(tracks, key=lambda x: x.last_mod, reverse=True)[start:]
|
||||||
|
|
||||||
|
return sorted(tracks, key=lambda x: x.last_mod, reverse=True)[start:limit]
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def get_recently_played(cls, limit: int):
|
||||||
|
tracks = cls.get_flat_list()
|
||||||
|
return sorted(tracks, key=lambda x: x.lastplayed, reverse=True)[:limit]
|
||||||
|
|||||||
Reference in New Issue
Block a user