mirror of
https://github.com/tcsenpai/swingmusic.git
synced 2025-07-14 13:40:07 +00:00
create methods to fetch data from the database
- start using a class to hold search query results
This commit is contained in:
parent
030ab8a379
commit
b6c5c57186
@ -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():
|
||||
"""
|
||||
|
@ -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]
|
||||
|
@ -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()
|
||||
]
|
||||
|
@ -16,7 +16,7 @@ class Track:
|
||||
|
||||
trackid: str
|
||||
title: str
|
||||
artists: list
|
||||
artists: list[str]
|
||||
albumartist: str
|
||||
album: str
|
||||
folder: str
|
||||
|
Loading…
x
Reference in New Issue
Block a user