Skip to content

Commit

Permalink
fix, server: album search
Browse files Browse the repository at this point in the history
  • Loading branch information
dxstiny committed Feb 7, 2024
1 parent 49fd88e commit 71bd141
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
20 changes: 12 additions & 8 deletions src/server/db/table/albums.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
Expand Down Expand Up @@ -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"""
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/server/meta/releases.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down
9 changes: 6 additions & 3 deletions src/server/meta/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 71bd141

Please sign in to comment.