mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-04 04:23:01 +00:00
fix search to read from database
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
+42
-35
@@ -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,
|
||||
}
|
||||
|
||||
@@ -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)},
|
||||
|
||||
+3
-16
@@ -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)
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -26,7 +26,7 @@ const queue = useQStore();
|
||||
const search = useSearchStore();
|
||||
|
||||
function loadMore() {
|
||||
search.updateLoadCounter("tracks", 5);
|
||||
search.updateLoadCounter("tracks");
|
||||
search.loadTracks(search.loadCounter.tracks);
|
||||
}
|
||||
|
||||
|
||||
@@ -43,6 +43,7 @@ async function searchTracks(query: string) {
|
||||
}
|
||||
|
||||
const data = await res.json();
|
||||
console.log(data)
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user