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:
@@ -166,12 +166,7 @@ def add_item_to_playlist(path: PlaylistIDPath, body: AddItemToPlaylistBody):
|
||||
else:
|
||||
trackhashes = []
|
||||
|
||||
# insert_count = PL.add_tracks_to_playlist(int(playlist_id), trackhashes)
|
||||
PlaylistTable.append_to_playlist(int(playlist_id), trackhashes)
|
||||
|
||||
# if insert_count == 0:
|
||||
# return {"error": "Item already exists in playlist"}, 409
|
||||
|
||||
return {"msg": "Done"}, 200
|
||||
|
||||
|
||||
@@ -211,15 +206,12 @@ def get_playlist(path: PlaylistIDPath, query: GetPlaylistQuery):
|
||||
playlist, tracks = handler()
|
||||
return format_custom_playlist(playlist, tracks)
|
||||
|
||||
# playlist = PL.get_playlist_by_id(int(playlistid))
|
||||
playlist = PlaylistTable.get_by_id(playlistid)
|
||||
|
||||
if playlist is None:
|
||||
return {"msg": "Playlist not found"}, 404
|
||||
|
||||
# tracks = TrackStore.get_tracks_by_trackhashes(list(playlist.trackhashes))
|
||||
tracks = TrackTable.get_tracks_by_trackhashes(playlist.trackhashes)
|
||||
tracks = remove_duplicates(tracks)
|
||||
|
||||
duration = sum(t.duration for t in tracks)
|
||||
playlist.last_updated = date_string_to_time_passed(playlist.last_updated)
|
||||
@@ -250,7 +242,6 @@ def update_playlist_info(path: PlaylistIDPath, form: UpdatePlaylistForm):
|
||||
Update playlist
|
||||
"""
|
||||
playlistid = path.playlistid
|
||||
# db_playlist = PL.get_playlist_by_id(playlistid)
|
||||
db_playlist = PlaylistTable.get_by_id(playlistid)
|
||||
|
||||
if db_playlist is None:
|
||||
@@ -270,7 +261,6 @@ def update_playlist_info(path: PlaylistIDPath, form: UpdatePlaylistForm):
|
||||
"last_updated": create_new_date(),
|
||||
"name": str(form.name).strip(),
|
||||
"settings": settings,
|
||||
"trackhashes": json.dumps([]),
|
||||
}
|
||||
|
||||
if image:
|
||||
@@ -290,7 +280,6 @@ def update_playlist_info(path: PlaylistIDPath, form: UpdatePlaylistForm):
|
||||
|
||||
p_tuple = (*playlist.values(),)
|
||||
|
||||
# PL.update_playlist(playlistid, playlist)
|
||||
PlaylistTable.update_one(playlistid, playlist)
|
||||
|
||||
playlist = models.Playlist(*p_tuple)
|
||||
@@ -306,8 +295,6 @@ def pin_unpin_playlist(path: PlaylistIDPath):
|
||||
"""
|
||||
Pin playlist.
|
||||
"""
|
||||
# playlist = PL.get_playlist_by_id(path.playlistid)
|
||||
|
||||
playlist = PlaylistTable.get_by_id(path.playlistid)
|
||||
|
||||
if playlist is None:
|
||||
@@ -320,7 +307,6 @@ def pin_unpin_playlist(path: PlaylistIDPath):
|
||||
except KeyError:
|
||||
settings["pinned"] = True
|
||||
|
||||
# PL.update_settings(path.playlistid, settings)
|
||||
PlaylistTable.update_settings(path.playlistid, settings)
|
||||
return {"msg": "Done"}, 200
|
||||
|
||||
@@ -330,13 +316,11 @@ def remove_playlist_image(path: PlaylistIDPath):
|
||||
"""
|
||||
Clear playlist image.
|
||||
"""
|
||||
# playlist = PL.get_playlist_by_id(path.playlistid)
|
||||
playlist = PlaylistTable.get_by_id(path.playlistid)
|
||||
|
||||
if playlist is None:
|
||||
return {"error": "Playlist not found"}, 404
|
||||
|
||||
# PL.remove_banner(path.playlistid)
|
||||
PlaylistTable.remove_image(path.playlistid)
|
||||
|
||||
playlist.image = None
|
||||
@@ -355,7 +339,6 @@ def remove_playlist(path: PlaylistIDPath):
|
||||
"""
|
||||
Delete playlist
|
||||
"""
|
||||
# PL.delete_playlist(path.playlistid)
|
||||
PlaylistTable.remove_one(path.playlistid)
|
||||
|
||||
return {"msg": "Done"}, 200
|
||||
@@ -378,7 +361,6 @@ def remove_tracks_from_playlist(
|
||||
# index: int;
|
||||
# }
|
||||
|
||||
# PL.remove_tracks_from_playlist(path.playlistid, body.tracks)
|
||||
PlaylistTable.remove_from_playlist(path.playlistid, body.tracks)
|
||||
|
||||
return {"msg": "Done"}, 200
|
||||
|
||||
+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,
|
||||
|
||||
+1
-1
@@ -73,7 +73,7 @@ def send_file_as_chunks(filepath: str, audio_type: str) -> Response:
|
||||
"""
|
||||
# NOTE: +1 makes sure the last byte is included in the range.
|
||||
# NOTE: -1 is used to convert the end index to a 0-based index.
|
||||
chunk_size = 1024 * 360 # 360 KB
|
||||
chunk_size = 1024 * 512 # 0.5MB
|
||||
|
||||
# Get file size
|
||||
file_size = os.path.getsize(filepath)
|
||||
|
||||
Reference in New Issue
Block a user