mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-03 20:13:02 +00:00
move to xxh3 hashing algorithm
+ port: search
This commit is contained in:
+23
-51
@@ -2,16 +2,17 @@
|
||||
Contains all the search routes.
|
||||
"""
|
||||
|
||||
from flask import request
|
||||
from unidecode import unidecode
|
||||
from pydantic import BaseModel, Field
|
||||
from pydantic import Field
|
||||
from flask_openapi3 import Tag
|
||||
from flask_openapi3 import APIBlueprint
|
||||
|
||||
from app import models
|
||||
from app.api.apischemas import GenericLimitSchema
|
||||
from app.db.libdata import TrackTable
|
||||
from app.lib import searchlib
|
||||
from app.settings import Defaults
|
||||
from app.store.tracks import TrackStore
|
||||
|
||||
|
||||
tag = Tag(name="Search", description="Search for tracks, albums and artists")
|
||||
api = APIBlueprint("search", __name__, url_prefix="/search", abp_tags=[tag])
|
||||
@@ -20,30 +21,18 @@ SEARCH_COUNT = 30
|
||||
"""The max amount of items to return per request"""
|
||||
|
||||
|
||||
def query_in_quotes(query: str) -> bool:
|
||||
"""
|
||||
Returns True if the query is in quotes
|
||||
"""
|
||||
try:
|
||||
return query.startswith('"') and query.endswith('"')
|
||||
except AttributeError:
|
||||
return False
|
||||
|
||||
|
||||
class Search:
|
||||
def __init__(self, query: str) -> None:
|
||||
self.tracks: list[models.Track] = []
|
||||
self.query = unidecode(query)
|
||||
|
||||
def search_tracks(self, in_quotes=False):
|
||||
def search_tracks(self):
|
||||
"""
|
||||
Calls :class:`SearchTracks` which returns the tracks that fuzzily match
|
||||
the search terms. Then adds them to the `SearchResults` store.
|
||||
"""
|
||||
self.tracks = TrackStore.tracks
|
||||
return searchlib.TopResults().search(
|
||||
self.query, tracks_only=True, in_quotes=in_quotes
|
||||
)
|
||||
self.tracks = TrackTable.get_all()
|
||||
return searchlib.TopResults().search(self.query, tracks_only=True)
|
||||
|
||||
def search_artists(self):
|
||||
"""Calls :class:`SearchArtists` which returns the artists that fuzzily match
|
||||
@@ -51,25 +40,23 @@ class Search:
|
||||
"""
|
||||
return searchlib.SearchArtists(self.query)()
|
||||
|
||||
def search_albums(self, in_quotes=False):
|
||||
def search_albums(self):
|
||||
"""Calls :class:`SearchAlbums` which returns the albums that fuzzily match
|
||||
the search term. Then adds them to the `SearchResults` store.
|
||||
"""
|
||||
return searchlib.TopResults().search(
|
||||
self.query, albums_only=True, in_quotes=in_quotes
|
||||
)
|
||||
return searchlib.TopResults().search(self.query, albums_only=True)
|
||||
|
||||
def get_top_results(
|
||||
self,
|
||||
limit: int,
|
||||
in_quotes=False,
|
||||
):
|
||||
finder = searchlib.TopResults()
|
||||
return finder.search(self.query, in_quotes=in_quotes, limit=limit)
|
||||
return finder.search(self.query, limit=limit)
|
||||
|
||||
|
||||
class SearchQuery(BaseModel):
|
||||
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")
|
||||
@@ -77,11 +64,7 @@ def search_tracks(query: SearchQuery):
|
||||
"""
|
||||
Search tracks
|
||||
"""
|
||||
|
||||
query = query.q
|
||||
in_quotes = query_in_quotes(query)
|
||||
|
||||
tracks = Search(query).search_tracks(in_quotes)
|
||||
tracks = Search(query.q).search_tracks()
|
||||
|
||||
return {
|
||||
"tracks": tracks[:SEARCH_COUNT],
|
||||
@@ -90,15 +73,13 @@ def search_tracks(query: SearchQuery):
|
||||
|
||||
|
||||
@api.get("/albums")
|
||||
def search_albums(query: SearchQuery):
|
||||
def search_albums(
|
||||
query: SearchQuery,
|
||||
):
|
||||
"""
|
||||
Search albums.
|
||||
"""
|
||||
|
||||
query = query.q
|
||||
in_quotes = query_in_quotes(query)
|
||||
|
||||
albums = Search(query).search_albums(in_quotes)
|
||||
albums = Search(query.q).search_albums()
|
||||
|
||||
return {
|
||||
"albums": albums[:SEARCH_COUNT],
|
||||
@@ -111,13 +92,10 @@ def search_artists(query: SearchQuery):
|
||||
"""
|
||||
Search artists.
|
||||
"""
|
||||
|
||||
query = query.q
|
||||
|
||||
if not query:
|
||||
if not query.q:
|
||||
return {"error": "No query provided"}, 400
|
||||
|
||||
artists = Search(query).search_artists()
|
||||
artists = Search(query.q).search_artists()
|
||||
|
||||
return {
|
||||
"artists": artists[:SEARCH_COUNT],
|
||||
@@ -138,15 +116,10 @@ def get_top_results(query: TopResultsQuery):
|
||||
|
||||
Returns the top results for the given query.
|
||||
"""
|
||||
limit = query.limit
|
||||
query = query.q
|
||||
|
||||
in_quotes = query_in_quotes(query)
|
||||
|
||||
if not query:
|
||||
if not query.q:
|
||||
return {"error": "No query provided"}, 400
|
||||
|
||||
return Search(query).get_top_results(in_quotes=in_quotes, limit=limit)
|
||||
return Search(query.q).get_top_results(limit=query.limit)
|
||||
|
||||
|
||||
class SearchLoadMoreQuery(SearchQuery):
|
||||
@@ -166,17 +139,16 @@ def search_load_more(query: SearchLoadMoreQuery):
|
||||
query = query.q
|
||||
item_type = query.type
|
||||
index = query.index
|
||||
in_quotes = query_in_quotes(query)
|
||||
|
||||
if item_type == "tracks":
|
||||
t = Search(query).search_tracks(in_quotes)
|
||||
t = Search(query).search_tracks()
|
||||
return {
|
||||
"tracks": t[index : index + SEARCH_COUNT],
|
||||
"more": len(t) > index + SEARCH_COUNT,
|
||||
}
|
||||
|
||||
elif item_type == "albums":
|
||||
a = Search(query).search_albums(in_quotes)
|
||||
a = Search(query).search_albums()
|
||||
return {
|
||||
"albums": a[index : index + SEARCH_COUNT],
|
||||
"more": len(a) > index + SEARCH_COUNT,
|
||||
|
||||
Reference in New Issue
Block a user