From 71bd141cc8374508a406d56fd30f606d6454d25b Mon Sep 17 00:00:00 2001 From: dxstiny Date: Wed, 7 Feb 2024 20:08:39 +0100 Subject: [PATCH] fix, server: album search --- src/server/db/table/albums.py | 20 ++++++++++++-------- src/server/meta/releases.py | 2 +- src/server/meta/spotify.py | 9 ++++++--- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/server/db/table/albums.py b/src/server/db/table/albums.py index 68e53e641..9d8ed2c94 100644 --- a/src/server/db/table/albums.py +++ b/src/server/db/table/albums.py @@ -156,11 +156,6 @@ def _fromDict(cls, data: JDict) -> Tuple[Any, ...] | None: releaseDateString = data.ensure("release_date", str, data.ensure("releaseDate", str)) releaseDate = datetime.fromisoformat(releaseDateString) - import logging - - logging.debug("fromDict") - logging.debug("data: %s", data) - return ( data.ensure("id", str), data.ensure("genres", list), @@ -173,7 +168,7 @@ def _fromDict(cls, data: JDict) -> Tuple[Any, ...] | None: releaseDate, data.ensure("name", str), data.ensure("total_tracks", int), - data.ensure("images.[0].url", str), + data.chain().ensure("images.[0].url", str), ) @staticmethod @@ -308,6 +303,15 @@ def __eq__(self, other: object) -> bool: return False return True + @property + def id(self) -> Optional[int]: + """return id""" + return self._id + + @id.setter + def id(self, value: Optional[int]) -> None: + self._id = value + @property def name(self) -> str: """return name""" @@ -454,7 +458,7 @@ async def _createModel(cls, song: Song, spotify: Spotify, db: Database) -> Optio if not albumId: model = AlbumModel(song.album, "", song.model.cover, song.artist) if not alreadyExists: - model._id = await db.albums.insert(model) + model.id = await db.albums.insert(model) return model if albumId: spotifyAlbum = await cls._fetchMetadata(albumId, spotify) @@ -483,7 +487,7 @@ async def _createModel(cls, song: Song, spotify: Spotify, db: Database) -> Optio logger.debug("returning model %s", model) if not alreadyExists: - model._id = await db.albums.insert(model) + model.id = await db.albums.insert(model) return model @classmethod diff --git a/src/server/meta/releases.py b/src/server/meta/releases.py index d57895fb3..e1a75d9bd 100644 --- a/src/server/meta/releases.py +++ b/src/server/meta/releases.py @@ -39,7 +39,7 @@ def __init__(self, spotify: Spotify) -> None: self._tracks.sort(key=lambda x: self._dateToInt(x.releaseDate), reverse=True) def _dateToInt(self, string: str) -> int: - return int(string.replace("-", "")) + return int(string.split("T")[0].replace("-", "")) def toDict(self) -> List[Dict[str, Any]]: """serialise""" diff --git a/src/server/meta/spotify.py b/src/server/meta/spotify.py index 3b941ebd7..7442b917f 100644 --- a/src/server/meta/spotify.py +++ b/src/server/meta/spotify.py @@ -357,7 +357,9 @@ def searchAlbum(self, query: str) -> SpotifyResult[List[SpotifyAlbum]]: self._logger.debug("Searching for album %s", query) assert self._spotify is not None search = self._spotify.search(query, limit=10, type="album") - albums = JDict(search).chain().ensure("albums.items", JList).iterator().ensureCast(JDict) + albums = ( + JDict(search).chain().ensureCast("albums.items", JList).iterator().ensureCast(JDict) + ) spotifyAlbums = [SpotifyAlbum.fromDict(album) for album in albums] return SpotifyResult.successResult([album for album in spotifyAlbums if album]) @@ -441,8 +443,9 @@ def artistAlbums(self, artistId: str) -> SpotifyResult[List[SpotifyAlbum]]: self._logger.debug("Getting albums from artist %s", artistId) assert self._spotify is not None albums = self._spotify.artist_albums(artistId) - albums = JDict(albums).ensure("items", JList).iterator().ensureCast(JDict) - spotifyAlbums = [SpotifyAlbum.fromDict(album) for album in albums] + albums = JDict(albums).ensureCast("items", JList).iterator().ensureCast(JDict) + spotifyAlbums = [SpotifyAlbum.fromDict(JDict(album)) for album in albums] + self._logger.debug("found %d from artist %s", len(spotifyAlbums), artistId) return SpotifyResult.successResult([album for album in spotifyAlbums if album]) @_connectionRequired