merge search endpoints

+ fix: favorites endpoint not filtering by logged in user
This commit is contained in:
cwilvx
2024-10-21 08:50:09 +03:00
parent e689d81d81
commit a7666ad935
3 changed files with 60 additions and 93 deletions
+10 -1
View File
@@ -117,6 +117,9 @@ class GetAllOfTypeQuery(GenericLimitSchema):
def get_favorite_albums(query: GetAllOfTypeQuery): def get_favorite_albums(query: GetAllOfTypeQuery):
""" """
Get favorite albums Get favorite albums
Note: Only the first request will return the total number of favorites.
Others will return -1
""" """
fav_albums, total = FavoritesTable.get_fav_albums(query.start, query.limit) fav_albums, total = FavoritesTable.get_fav_albums(query.start, query.limit)
fav_albums.reverse() fav_albums.reverse()
@@ -129,6 +132,9 @@ def get_favorite_albums(query: GetAllOfTypeQuery):
def get_favorite_tracks(query: GetAllOfTypeQuery): def get_favorite_tracks(query: GetAllOfTypeQuery):
""" """
Get favorite tracks Get favorite tracks
Note: Only the first request will return the total number of favorites.
Others will return -1
""" """
tracks, total = FavoritesTable.get_fav_tracks(query.start, query.limit) tracks, total = FavoritesTable.get_fav_tracks(query.start, query.limit)
tracks.reverse() tracks.reverse()
@@ -141,6 +147,9 @@ def get_favorite_tracks(query: GetAllOfTypeQuery):
def get_favorite_artists(query: GetAllOfTypeQuery): def get_favorite_artists(query: GetAllOfTypeQuery):
""" """
Get favorite artists Get favorite artists
Note: Only the first request will return the total number of favorites.
Others will return -1
""" """
artists, total = FavoritesTable.get_fav_artists( artists, total = FavoritesTable.get_fav_artists(
start=query.start, start=query.start,
@@ -188,7 +197,7 @@ def get_all_favorites(query: GetAllFavoritesQuery):
# largest is x2 to accound for broken hashes if any # largest is x2 to accound for broken hashes if any
largest = max(track_limit, album_limit, artist_limit) largest = max(track_limit, album_limit, artist_limit)
favs = FavoritesTable.get_all() favs = FavoritesTable.get_all(with_user=True)
favs = sorted(favs, key=lambda x: x.timestamp, reverse=True) favs = sorted(favs, key=lambda x: x.timestamp, reverse=True)
tracks = [] tracks = []
+43 -90
View File
@@ -2,6 +2,7 @@
Contains all the search routes. Contains all the search routes.
""" """
from typing import Any, Literal
from unidecode import unidecode from unidecode import unidecode
from pydantic import Field from pydantic import Field
from flask_openapi3 import Tag from flask_openapi3 import Tag
@@ -18,7 +19,29 @@ tag = Tag(name="Search", description="Search for tracks, albums and artists")
api = APIBlueprint("search", __name__, url_prefix="/search", abp_tags=[tag]) api = APIBlueprint("search", __name__, url_prefix="/search", abp_tags=[tag])
SEARCH_COUNT = 30 SEARCH_COUNT = 30
"""The max amount of items to return per request""" """
The max amount of items to return per request
"""
class SearchQuery(GenericLimitSchema):
q: str = Field(description="The search query", example=Defaults.API_ARTISTNAME)
start: int = Field(description="The index to start from", default=0, example=0)
limit: int = Field(
description="The number of items to return", default=SEARCH_COUNT
)
class TopResultsQuery(SearchQuery):
limit: int = Field(
description="The number of items to return", default=Defaults.API_CARD_LIMIT
)
class SearchLoadMoreQuery(SearchQuery):
itemtype: Literal["tracks", "albums", "artists"] = Field(
description="The type of search", example="tracks"
)
class Search: class Search:
@@ -54,61 +77,6 @@ class Search:
return finder.search(self.query, limit=limit) return finder.search(self.query, limit=limit)
class SearchQuery(GenericLimitSchema):
q: str = Field(description="The search query", example=Defaults.API_ARTISTNAME)
start: int = Field(description="The index to start from", default=0, example=0)
@api.get("/tracks")
def search_tracks(query: SearchQuery):
"""
Search tracks
"""
tracks = Search(query.q).search_tracks()
return {
"tracks": tracks[:SEARCH_COUNT],
"more": len(tracks) > SEARCH_COUNT,
}
@api.get("/albums")
def search_albums(
query: SearchQuery,
):
"""
Search albums.
"""
albums = Search(query.q).search_albums()
return {
"albums": albums[:SEARCH_COUNT],
"more": len(albums) > SEARCH_COUNT,
}
@api.get("/artists")
def search_artists(query: SearchQuery):
"""
Search artists.
"""
if not query.q:
return {"error": "No query provided"}, 400
artists = Search(query.q).search_artists()
return {
"artists": artists[:SEARCH_COUNT],
"more": len(artists) > SEARCH_COUNT,
}
class TopResultsQuery(SearchQuery):
limit: int = Field(
description="The number of items to return", default=Defaults.API_CARD_LIMIT
)
@api.get("/top") @api.get("/top")
def get_top_results(query: TopResultsQuery): def get_top_results(query: TopResultsQuery):
""" """
@@ -122,44 +90,29 @@ def get_top_results(query: TopResultsQuery):
return Search(query.q).get_top_results(limit=query.limit) return Search(query.q).get_top_results(limit=query.limit)
class SearchLoadMoreQuery(SearchQuery): @api.get("/")
type: str = Field(description="The type of search", example="tracks") def search_items(query: SearchLoadMoreQuery):
start: int = Field(description="The index to start from", default=0)
@api.get("/loadmore")
def search_load_more(query: SearchLoadMoreQuery):
""" """
Load more Find tracks, albums or artists from a search query.
Returns more songs, albums or artists from a search query.
NOTE: You must first initiate a search using the `/search` endpoint.
""" """
q = query.q results: Any = []
item_type = query.type
index = query.start
if item_type == "tracks": match query.itemtype:
t = Search(q).search_tracks() case "tracks":
return { results = Search(query.q).search_tracks()
"tracks": t[index : index + SEARCH_COUNT], case "albums":
"more": len(t) > index + SEARCH_COUNT, results = Search(query.q).search_albums()
} case "artists":
results = Search(query.q).search_artists()
case _:
return {
"error": "Invalid item type. Valid types are 'tracks', 'albums' and 'artists'"
}, 400
elif item_type == "albums": return {
a = Search(q).search_albums() "results": results[query.start : query.start + query.limit],
return { "more": len(results) > query.start + query.limit,
"albums": a[index : index + SEARCH_COUNT], }
"more": len(a) > index + SEARCH_COUNT,
}
elif item_type == "artists":
a = Search(q).search_artists()
return {
"artists": a[index : index + SEARCH_COUNT],
"more": len(a) > index + SEARCH_COUNT,
}
# TODO: Rewrite this file using generators where possible # TODO: Rewrite this file using generators where possible
+7 -2
View File
@@ -184,9 +184,14 @@ class FavoritesTable(Base):
) )
@classmethod @classmethod
def get_all(cls): def get_all(cls, with_user: bool = False):
with DbEngine.manager() as conn: with DbEngine.manager() as conn:
result = conn.execute(select(cls)) if with_user:
result = conn.execute(
select(cls).where(cls.userid == get_current_userid())
)
else:
result = conn.execute(select(cls))
return favorites_to_dataclass(result.fetchall()) return favorites_to_dataclass(result.fetchall())
@classmethod @classmethod