diff --git a/server/app/api/album.py b/server/app/api/album.py index 2624966d..1ae004af 100644 --- a/server/app/api/album.py +++ b/server/app/api/album.py @@ -91,6 +91,7 @@ def get_albumartists(): for track in tracks: for artist in track.artists: + artist = artist.lower() if artist not in artists: artists.append(artist) @@ -98,7 +99,7 @@ def get_albumartists(): for artist in artists: artist_obj = { "name": artist, - "image": helpers.check_artist_image(artist), + "image": helpers.check_artist_image(helpers.create_safe_name(artist)), } final_artists.append(artist_obj) diff --git a/server/app/api/search.py b/server/app/api/search.py index f4d2fc42..15ab35ca 100644 --- a/server/app/api/search.py +++ b/server/app/api/search.py @@ -1,13 +1,15 @@ """ Contains all the search routes. """ +from pprint import pprint 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 +from app import models +from app import serializer search_bp = Blueprint("search", __name__, url_prefix="/") @@ -24,10 +26,10 @@ class SearchResults: """ query: str = "" - tracks: list[models.Track] - albums: list[models.Album] - playlists: list[models.Playlist] - artists: list[models.Artist] + tracks: list[models.Track] = [] + albums: list[models.Album] = [] + playlists: list[models.Playlist] = [] + artists: list[models.Artist] = [] class DoSearch: @@ -64,8 +66,8 @@ class DoSearch: """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)() + albums = helpers.Get.get_all_albums() + albums = searchlib.SearchAlbums(albums, self.query)() SearchResults.albums = albums return albums @@ -74,8 +76,10 @@ class DoSearch: """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) + playlists = helpers.Get.get_all_playlists() + playlists = [serializer.Playlist(playlist) for playlist in playlists] + + playlists = searchlib.SearchPlaylists(playlists, self.query)() SearchResults.playlists = playlists return playlists @@ -98,17 +102,11 @@ def search_tracks(): if not query: return {"error": "No query provided"}, 400 - if SearchResults.query == query and len(SearchResults.tracks) > 0: - return { - "tracks": SearchResults.tracks[:5], - "more": len(SearchResults.tracks) > 5, - }, 200 - tracks = DoSearch(query).search_tracks() return { - "tracks": tracks[:5], - "more": len(tracks) > 5, + "tracks": tracks[:6], + "more": len(tracks) > 6, }, 200 @@ -122,12 +120,6 @@ def search_albums(): if not query: return {"error": "No query provided"}, 400 - if SearchResults.query == query and len(SearchResults.albums) > 0: - return { - "albums": SearchResults.albums[:6], - "more": len(SearchResults.albums) > 6, - }, 200 - tracks = DoSearch(query).search_albums() return { @@ -146,12 +138,6 @@ def search_artists(): if not query: return {"error": "No query provided"}, 400 - if SearchResults.query == query and len(SearchResults.artists) > 0: - return { - "artists": SearchResults.artists[:6], - "more": len(SearchResults.artists) > 6, - }, 200 - artists = DoSearch(query).search_artists() return { @@ -160,6 +146,24 @@ def search_artists(): }, 200 +@search_bp.route("/search/playlists", methods=["GET"]) +def search_playlists(): + """ + Searches for playlists. + """ + + query = request.args.get("q") + if not query: + return {"error": "No query provided"}, 400 + + playlists = DoSearch(query).search_playlists() + + return { + "playlists": playlists[:6], + "more": len(playlists) > 6, + }, 200 + + @search_bp.route("/search/top", methods=["GET"]) def get_top_results(): """ @@ -220,19 +224,22 @@ def search_load_more(): index = int(request.args.get("index")) if type == "tracks": + t = SearchResults.tracks return { - "tracks": SEARCH_RESULTS["tracks"][index : index + 5], - "more": len(SEARCH_RESULTS["tracks"]) > index + 5, + "tracks": t[index : index + 5], + "more": len(t) > index + 5, } elif type == "albums": + a = SearchResults.albums return { - "albums": SEARCH_RESULTS["albums"][index : index + 6], - "more": len(SEARCH_RESULTS["albums"]) > index + 6, + "albums": a[index : index + 6], + "more": len(a) > index + 6, } elif type == "artists": + a = SearchResults.artists return { - "artists": SEARCH_RESULTS["artists"][index : index + 6], - "more": len(SEARCH_RESULTS["artists"]) > index + 6, + "artists": a[index : index + 6], + "more": len(a) > index + 6, } diff --git a/server/app/db/mongodb/playlists.py b/server/app/db/mongodb/playlists.py index 7c588169..9b730c48 100644 --- a/server/app/db/mongodb/playlists.py +++ b/server/app/db/mongodb/playlists.py @@ -4,7 +4,7 @@ This file contains the Playlists class for interacting with the playlist documen from app.db.mongodb import convert_many from app.db.mongodb import convert_one from app.db.mongodb import MongoPlaylists -from app.helpers import create_new_date +from app import helpers from bson import ObjectId @@ -41,7 +41,7 @@ class Playlists(MongoPlaylists): """ Sets the lastUpdated field to the current date. """ - date = create_new_date() + date = helpers.create_new_date() return self.collection.update_one( {"_id": ObjectId(playlistid)}, diff --git a/server/app/functions.py b/server/app/functions.py index f91f5ff4..9c972192 100644 --- a/server/app/functions.py +++ b/server/app/functions.py @@ -14,9 +14,8 @@ from app.lib.populate import Populate from PIL import Image from concurrent.futures import ThreadPoolExecutor -from app.lib.trackslib import validate_tracks from app.lib import trackslib -from server.app import instances, models +from app import instances, models @helpers.background @@ -100,18 +99,6 @@ class CheckArtistImages: else: return False - def gather_artists(self): - """ - Loops through all the tracks and gathers all the artists. - """ - ar = instances.tracks_instance.get_all_tracks() - tracks = [models.Track(t) for t in ar] - - for t in tracks: - for artist in t.artists: - if artist not in self.artists: - self.artists.append(artist) - @classmethod def download_image(cls, artistname: str): """ @@ -123,7 +110,7 @@ class CheckArtistImages: img_path = ( helpers.app_dir + "/images/artists/" - + artistname.replace("/", "::") + + helpers.create_safe_name(artistname) + ".webp" ) @@ -138,7 +125,7 @@ class CheckArtistImages: useImageDownloader(url, img_path)() def __call__(self): - self.gather_artists() + self.artists = helpers.Get.get_all_artists() with ThreadPoolExecutor() as pool: pool.map(self.download_image, self.artists) diff --git a/server/app/helpers.py b/server/app/helpers.py index dbe0981c..e6196ecc 100644 --- a/server/app/helpers.py +++ b/server/app/helpers.py @@ -185,8 +185,9 @@ class Get: 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() + @classmethod + def get_all_artists(cls) -> Set[str]: + tracks = cls.get_all_tracks() artists: Set[str] = set() for track in tracks: diff --git a/server/app/lib/searchlib.py b/server/app/lib/searchlib.py index 30744dc8..4f543b15 100644 --- a/server/app/lib/searchlib.py +++ b/server/app/lib/searchlib.py @@ -97,7 +97,13 @@ class SearchAlbums: Gets all albums with a given title. """ - albums = [a.title for a in self.albums] + albums = [] + + for album in self.albums: + title = album.title.lower() + if title not in albums: + albums.append(title) + results = process.extract( self.query, albums, diff --git a/src/components/RightSideBar/Search/AlbumGrid.vue b/src/components/RightSideBar/Search/AlbumGrid.vue index 910fe279..7c590cad 100644 --- a/src/components/RightSideBar/Search/AlbumGrid.vue +++ b/src/components/RightSideBar/Search/AlbumGrid.vue @@ -19,7 +19,7 @@ import useSearchStore from "../../../stores/search"; const search = useSearchStore(); function loadMore() { - search.updateLoadCounter("albums", 6); + search.updateLoadCounter("albums"); search.loadAlbums(search.loadCounter.albums); } diff --git a/src/components/RightSideBar/Search/ArtistGrid.vue b/src/components/RightSideBar/Search/ArtistGrid.vue index 26f6819c..76ecb489 100644 --- a/src/components/RightSideBar/Search/ArtistGrid.vue +++ b/src/components/RightSideBar/Search/ArtistGrid.vue @@ -19,7 +19,7 @@ import useSearchStore from "../../../stores/search"; const search = useSearchStore(); function loadMore() { - search.updateLoadCounter("artists", 6); + search.updateLoadCounter("artists"); search.loadArtists(search.loadCounter.artists); } diff --git a/src/components/RightSideBar/Search/TracksGrid.vue b/src/components/RightSideBar/Search/TracksGrid.vue index 696216d3..3ad045cb 100644 --- a/src/components/RightSideBar/Search/TracksGrid.vue +++ b/src/components/RightSideBar/Search/TracksGrid.vue @@ -26,7 +26,7 @@ const queue = useQStore(); const search = useSearchStore(); function loadMore() { - search.updateLoadCounter("tracks", 5); + search.updateLoadCounter("tracks"); search.loadTracks(search.loadCounter.tracks); } diff --git a/src/composables/loadmore.js b/src/composables/loadmore.js deleted file mode 100644 index e69de29b..00000000 diff --git a/src/composables/searchMusic.ts b/src/composables/searchMusic.ts index d18d784b..75bdac4f 100644 --- a/src/composables/searchMusic.ts +++ b/src/composables/searchMusic.ts @@ -43,6 +43,7 @@ async function searchTracks(query: string) { } const data = await res.json(); + console.log(data) return data; } diff --git a/src/stores/search.ts b/src/stores/search.ts index 4bdcb1c2..b36dccd8 100644 --- a/src/stores/search.ts +++ b/src/stores/search.ts @@ -123,16 +123,16 @@ export default defineStore("search", () => { .then(() => scrollOnLoad()); } - function updateLoadCounter(type: string, value: number) { + function updateLoadCounter(type: string) { switch (type) { case "tracks": - loadCounter.tracks += value; + loadCounter.tracks += 6; break; case "albums": - loadCounter.albums += value; + loadCounter.albums += 6; break; case "artists": - loadCounter.artists += value; + loadCounter.artists += 6; break; } }