blind write search methods

This commit is contained in:
geoffrey45
2022-06-13 20:40:23 +03:00
parent b6c5c57186
commit 88247e0553
4 changed files with 130 additions and 103 deletions
+102 -42
View File
@@ -24,62 +24,91 @@ class SearchResults:
""" """
query: str = "" query: str = ""
tracks: list[models.Track]
class Tracks: albums: list[models.Album]
""" playlists: list[models.Playlist]
Holds all the tracks search results. artists: list[models.Artist]
"""
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: class DoSearch:
"""Class containing the methods that perform searching."""
def __init__(self, query: str) -> None: def __init__(self, query: str) -> None:
"""
:param :str:`query`: the search query.
"""
self.query = query self.query = query
self.tracks = helpers.Get.get_all_tracks() SearchResults.query = query
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): def search_tracks(self):
results = searchlib.SearchTracks(self.tracks, self.query) """Calls :class:`SearchTracks` which returns the tracks that fuzzily match
the search terms. Then adds them to the `SearchResults` store.
"""
self.tracks = helpers.Get.get_all_tracks()
tracks = searchlib.SearchTracks(self.tracks, self.query)()
SearchResults.tracks = tracks
return tracks
def search_artists(self): def search_artists(self):
SearchResults.Artists.results = searchlib.SearchArtists( """Calls :class:`SearchArtists` which returns the artists that fuzzily match
self.artists, self.query the search term. Then adds them to the `SearchResults` store.
) """
self.artists = helpers.Get.get_all_artists()
artists = searchlib.SearchArtists(self.artists, self.query)()
SearchResults.artists = artists
return artists
def search_albums(self):
"""Calls :class:`SearchAlbums` which returns the albums that fuzzily match
the search term. Then adds them to the `SearchResults` store.
"""
self.albums = helpers.Get.get_all_albums()
albums = searchlib.SearchAlbums(self.albums, self.query)()
SearchResults.albums = albums
return albums
def search_playlists(self):
"""Calls :class:`SearchPlaylists` which returns the playlists that fuzzily match
the search term. Then adds them to the `SearchResults` store.
"""
self.playlists = helpers.Get.get_all_playlists()
playlists = searchlib.SearchPlaylists(self.playlists, self.query)
SearchResults.playlists = playlists
return playlists
def search_all(self):
"""Calls all the search methods."""
self.search_tracks()
self.search_albums()
self.search_artists()
self.search_playlists()
@search_bp.route("/search/tracks", methods=["GET"]) @search_bp.route("/search/tracks", methods=["GET"])
def search_tracks(): def search_tracks():
""" """
Searches for tracks. Searches for tracks that match the search query.
""" """
query = request.args.get("q") query = request.args.get("q")
if not query: if not query:
return {"error": "No query provided"}, 400 return {"error": "No query provided"}, 400
results = searchlib.SearchTracks(query)() if SearchResults.query == query and len(SearchResults.tracks) > 0:
SEARCH_RESULTS["tracks"] = results return {
"tracks": SearchResults.tracks[:5],
"more": len(SearchResults.tracks) > 5,
}, 200
tracks = DoSearch(query).search_tracks()
return { return {
"tracks": results[:5], "tracks": tracks[:5],
"more": len(results) > 5, "more": len(tracks) > 5,
}, 200 }, 200
@@ -93,12 +122,17 @@ def search_albums():
if not query: if not query:
return {"error": "No query provided"}, 400 return {"error": "No query provided"}, 400
results = searchlib.SearchAlbums(query)() if SearchResults.query == query and len(SearchResults.albums) > 0:
SEARCH_RESULTS["albums"] = results return {
"albums": SearchResults.albums[:6],
"more": len(SearchResults.albums) > 6,
}, 200
tracks = DoSearch(query).search_albums()
return { return {
"albums": results[:6], "albums": tracks[:6],
"more": len(results) > 6, "more": len(tracks) > 6,
}, 200 }, 200
@@ -112,15 +146,41 @@ def search_artists():
if not query: if not query:
return {"error": "No query provided"}, 400 return {"error": "No query provided"}, 400
results = searchlib.SearchArtists(query)() if SearchResults.query == query and len(SearchResults.artists) > 0:
SEARCH_RESULTS["artists"] = results return {
"artists": SearchResults.artists[:6],
"more": len(SearchResults.artists) > 6,
}, 200
artists = DoSearch(query).search_artists()
return { return {
"artists": results[:6], "artists": artists[:6],
"more": len(results) > 6, "more": len(artists) > 6,
}, 200 }, 200
@search_bp.route("/search/top", methods=["GET"])
def get_top_results():
"""
Returns the top results for the search query.
"""
query = request.args.get("q")
if not query:
return {"error": "No query provided"}, 400
DoSearch(query).search_all()
max = 2
return {
"tracks": SearchResults.tracks[:max],
"albums": SearchResults.albums[:max],
"artists": SearchResults.artists[:max],
"playlists": SearchResults.playlists[:max],
}
@search_bp.route("/search") @search_bp.route("/search")
def search(): def search():
""" """
+3 -3
View File
@@ -5,7 +5,7 @@ import os
import random import random
import threading import threading
from datetime import datetime from datetime import datetime
from typing import Dict from typing import Dict, Set
from typing import List from typing import List
from app import models from app import models
@@ -185,9 +185,9 @@ class Get:
a = instances.album_instance.get_all_albums() a = instances.album_instance.get_all_albums()
return [models.Album(a) for a in a] return [models.Album(a) for a in a]
def get_all_artists(self) -> set[str]: def get_all_artists(self) -> Set[str]:
tracks = self.get_all_tracks() tracks = self.get_all_tracks()
artists: set[str] = set() artists: Set[str] = set()
for track in tracks: for track in tracks:
for artist in track.artists: for artist in track.artists:
-18
View File
@@ -157,21 +157,3 @@ def create_album(track: dict, tracklist: list) -> dict:
# album["image"] = "".join(x for x in albumhash if x not in "\/:*?<>|") # album["image"] = "".join(x for x in albumhash if x not in "\/:*?<>|")
return album return album
def search_albums_by_name(query: str) -> List[models.Album]:
"""
Searches albums by album name.
"""
title_albums: List[models.Album] = []
artist_albums: List[models.Album] = []
for album in api.ALBUMS:
if query.lower() in album.title.lower():
title_albums.append(album)
for album in api.ALBUMS:
if query.lower() in album.artist.lower():
artist_albums.append(album)
return [*title_albums, *artist_albums]
+25 -40
View File
@@ -22,6 +22,7 @@ class Cutoff:
tracks: int = 80 tracks: int = 80
albums: int = 80 albums: int = 80
artists: int = 80 artists: int = 80
playlists: int = 80
class Limit: class Limit:
@@ -32,18 +33,20 @@ class Limit:
tracks: int = 50 tracks: int = 50
albums: int = 50 albums: int = 50
artists: int = 50 artists: int = 50
playlists: int = 50
class SearchTracks: class SearchTracks:
def __init__(self, query) -> None: def __init__(self, tracks: List[models.Track], query: str) -> None:
self.query = query self.query = query
self.tracks = tracks
def __call__(self) -> List[models.Track]: def __call__(self) -> List[models.Track]:
""" """
Gets all songs with a given title. Gets all songs with a given title.
""" """
tracks = [track.title for track in api.TRACKS] tracks = [track.title for track in self.tracks]
results = process.extract( results = process.extract(
self.query, self.query,
tracks, tracks,
@@ -52,11 +55,11 @@ class SearchTracks:
limit=Limit.tracks, limit=Limit.tracks,
) )
return [api.TRACKS[i[2]] for i in results] return [self.tracks[i[2]] for i in results]
class SearchArtists: class SearchArtists:
def __init__(self, artists: set[str], query) -> None: def __init__(self, artists: set[str], query: str) -> None:
self.query = query self.query = query
self.artists = artists self.artists = artists
@@ -85,24 +88,16 @@ class SearchArtists:
class SearchAlbums: class SearchAlbums:
def __init__(self, query) -> None: def __init__(self, albums: List[models.Album], query: str) -> None:
self.query = query self.query = query
self.albums = albums
def get_albums_by_name(self) -> List[models.Album]:
"""
Gets all albums with a given title.
"""
albums = [album.title for album in api.ALBUMS]
results = process.extract(self.query, albums)
return [api.ALBUMS[i[2]] for i in results]
def __call__(self) -> List[models.Album]: def __call__(self) -> List[models.Album]:
""" """
Gets all albums with a given title. Gets all albums with a given title.
""" """
albums = [a.title for a in api.ALBUMS] albums = [a.title for a in self.albums]
results = process.extract( results = process.extract(
self.query, self.query,
albums, albums,
@@ -111,7 +106,7 @@ class SearchAlbums:
limit=Limit.albums, limit=Limit.albums,
) )
return [api.ALBUMS[i[2]] for i in results] return [self.albums[i[2]] for i in results]
# get all artists that matched the query # get all artists that matched the query
# for get all albums from the artists # for get all albums from the artists
@@ -121,29 +116,19 @@ class SearchAlbums:
# recheck next and previous artist on play next or add to playlist # recheck next and previous artist on play next or add to playlist
class GetTopArtistTracks: class SearchPlaylists:
def __init__(self, artist: str) -> None: def __init__(self, playlists: List[models.Playlist], query: str) -> None:
self.artist = artist self.playlists = playlists
self.query = query
def __call__(self) -> List[models.Track]: def __call__(self) -> List[models.Playlist]:
""" playlists = [p.name for p in self.playlists]
Gets all tracks from a given artist. results = process.extract(
""" self.query,
playlists,
scorer=fuzz.WRatio,
score_cutoff=Cutoff.playlists,
limit=Limit.playlists,
)
return [track for track in api.TRACKS if self.artist in track.artists] return [self.playlists[i[2]] for i in results]
def get_search_albums(query: str) -> List[models.Album]:
"""
Gets all songs with a given album.
"""
return albumslib.search_albums_by_name(query)
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()
]