From b6c5c57186df135ed9b30e316ec41d51a3c7608b Mon Sep 17 00:00:00 2001 From: geoffrey45 Date: Mon, 13 Jun 2022 17:11:58 +0300 Subject: [PATCH] create methods to fetch data from the database - start using a class to hold search query results --- server/app/api/search.py | 49 +++++++++++++++++++++++++++++++++++++ server/app/helpers.py | 40 ++++++++++++++++++++++++++++-- server/app/lib/searchlib.py | 35 ++++++-------------------- server/app/models.py | 2 +- 4 files changed, 95 insertions(+), 31 deletions(-) diff --git a/server/app/api/search.py b/server/app/api/search.py index f5aac90..4ba3834 100644 --- a/server/app/api/search.py +++ b/server/app/api/search.py @@ -1,11 +1,14 @@ """ Contains all the search routes. """ +from typing import List from app import helpers from app.lib import searchlib from flask import Blueprint from flask import request +from server.app import instances, models + search_bp = Blueprint("search", __name__, url_prefix="/") SEARCH_RESULTS = { @@ -15,6 +18,52 @@ SEARCH_RESULTS = { } +class SearchResults: + """ + Holds all the search results. + """ + + query: str = "" + + class Tracks: + """ + Holds all the tracks search results. + """ + + results: List[models.Track] + + class Albums: + """ + Holds all the albums search results. + """ + + results: List[models.Album] + + class Artists: + """ + Holds all the artists search results. + """ + + results: List[models.Artist] + + +class DoSearch: + def __init__(self, query: str) -> None: + self.query = query + self.tracks = helpers.Get.get_all_tracks() + self.albums = helpers.Get.get_all_albums() + self.artists = helpers.Get.get_all_artists() + self.playlists = helpers.Get.get_all_playlists() + + def search_tracks(self): + results = searchlib.SearchTracks(self.tracks, self.query) + + def search_artists(self): + SearchResults.Artists.results = searchlib.SearchArtists( + self.artists, self.query + ) + + @search_bp.route("/search/tracks", methods=["GET"]) def search_tracks(): """ diff --git a/server/app/helpers.py b/server/app/helpers.py index e57b240..2db6098 100644 --- a/server/app/helpers.py +++ b/server/app/helpers.py @@ -10,6 +10,7 @@ from typing import List from app import models from app import settings +from app import instances app_dir = settings.APP_DIR @@ -136,9 +137,9 @@ def create_safe_name(name: str) -> str: class UseBisection: """ - Uses bisection to find a list of items in another list. + Uses bisection to find a list of items in another list. - returns a list of found items with `None` items being not found + returns a list of found items with `None` items being not found items. """ @@ -166,3 +167,38 @@ class UseBisection: def __call__(self) -> List: return [self.find(query) for query in self.queries] + + +class Get: + @staticmethod + def get_all_tracks() -> List[models.Track]: + """ + Returns all tracks + """ + t = instances.tracks_instance.get_all_tracks() + return [models.Track(t) for t in t] + + def get_all_albums() -> List[models.Album]: + """ + Returns all albums + """ + a = instances.album_instance.get_all_albums() + return [models.Album(a) for a in a] + + def get_all_artists(self) -> set[str]: + tracks = self.get_all_tracks() + artists: set[str] = set() + + for track in tracks: + for artist in track.artists: + artists.add(artist.lower()) + + return artists + + @staticmethod + def get_all_playlists() -> List[models.Playlist]: + """ + Returns all playlists + """ + p = instances.playlist_instance.get_all_playlists() + return [models.Playlist(p) for p in p] diff --git a/server/app/lib/searchlib.py b/server/app/lib/searchlib.py index 6b8ff22..6d0016b 100644 --- a/server/app/lib/searchlib.py +++ b/server/app/lib/searchlib.py @@ -19,9 +19,9 @@ class Cutoff: Holds all the default cutoff values. """ - tracks: int = 70 - albums: int = 70 - artists: int = 70 + tracks: int = 80 + albums: int = 80 + artists: int = 80 class Limit: @@ -35,7 +35,6 @@ class Limit: class SearchTracks: - def __init__(self, query) -> None: self.query = query @@ -57,35 +56,18 @@ class SearchTracks: class SearchArtists: - - def __init__(self, query) -> None: + def __init__(self, artists: set[str], query) -> None: self.query = query - - @staticmethod - def get_all_artist_names() -> List[str]: - """ - Gets all artist names. - """ - - artists = [track.artists for track in api.TRACKS] - - f_artists = set() - - for artist in artists: - for a in artist: - f_artists.add(a) - - return f_artists + self.artists = artists def __call__(self) -> list: """ Gets all artists with a given name. """ - artists = self.get_all_artist_names() results = process.extract( self.query, - artists, + self.artists, scorer=fuzz.WRatio, score_cutoff=Cutoff.artists, limit=Limit.artists, @@ -103,7 +85,6 @@ class SearchArtists: class SearchAlbums: - def __init__(self, query) -> None: self.query = query @@ -141,7 +122,6 @@ class SearchAlbums: class GetTopArtistTracks: - def __init__(self, artist: str) -> None: self.artist = artist @@ -165,6 +145,5 @@ def get_artists(artist: str) -> List[models.Track]: Gets all songs with a given artist. """ return [ - track for track in api.TRACKS - if artist.lower() in str(track.artists).lower() + track for track in api.TRACKS if artist.lower() in str(track.artists).lower() ] diff --git a/server/app/models.py b/server/app/models.py index 3456a70..b8bbfe8 100644 --- a/server/app/models.py +++ b/server/app/models.py @@ -16,7 +16,7 @@ class Track: trackid: str title: str - artists: list + artists: list[str] albumartist: str album: str folder: str