port search to stores

+ fix favorites
This commit is contained in:
cwilvx
2024-07-27 21:44:33 +03:00
parent 5d32536758
commit b0e904c84f
25 changed files with 428 additions and 666 deletions
+4
View File
@@ -2,6 +2,7 @@
Contains all the album routes.
"""
from pprint import pprint
import random
from pydantic import BaseModel, Field
@@ -54,6 +55,9 @@ def get_album_tracks_and_info(body: AlbumHashSchema):
track_total = sum({int(t.extra.get("track_total", 1) or 1) for t in tracks})
avg_bitrate = sum(t.bitrate for t in tracks) // (len(tracks) or 1)
album.fav_userids = [1]
pprint(album)
return {
"info": album,
"extra": {
+35 -13
View File
@@ -11,7 +11,11 @@ from app.db.libdata import AlbumTable, TrackTable
from app.db.userdata import FavoritesTable
from app.models import FavType
from app.settings import Defaults
from app.utils.bisection import use_bisection
from app.store.albums import AlbumStore
from app.store.artists import ArtistStore
from app.store.tracks import TrackStore
from app.serializers.track import serialize_track, serialize_tracks
from app.serializers.artist import (
serialize_for_card as serialize_artist,
@@ -46,14 +50,27 @@ def toggle_favorite(body: FavoritesAddBody):
"""
Adds a favorite to the database.
"""
FavoritesTable.insert_item({"hash": body.hash, "type": body.type})
try:
FavoritesTable.insert_item({"hash": body.hash, "type": body.type})
except:
return {"msg": "Failed! An error occured"}, 500
if body.type == FavType.track:
TrackTable.set_is_favorite(body.hash, True)
entry = TrackStore.trackhashmap.get(body.hash)
if entry is not None:
entry.toggle_favorite_user()
elif body.type == FavType.album:
AlbumTable.set_is_favorite(body.hash, True)
entry = AlbumStore.albummap.get(body.hash)
if entry is not None:
entry.toggle_favorite_user()
elif body.type == FavType.artist:
ArtistTable.set_is_favorite(body.hash, True)
entry = ArtistStore.artistmap.get(body.hash)
if entry is not None:
entry.toggle_favorite_user()
return {"msg": "Added to favorites"}
@@ -95,7 +112,8 @@ def get_favorite_albums(query: GetAllOfTypeQuery):
fav_albums, total = FavoritesTable.get_fav_albums(query.start, query.limit)
fav_albums.reverse()
return {"albums": serialize_for_card_many(fav_albums), "total": total}
albums = AlbumStore.get_albums_by_hashes(a.hash for a in fav_albums)
return {"albums": serialize_for_card_many(albums), "total": total}
@api.get("/tracks")
@@ -104,6 +122,10 @@ def get_favorite_tracks(query: GetAllOfTypeQuery):
Get favorite tracks
"""
tracks, total = FavoritesTable.get_fav_tracks(query.start, query.limit)
tracks.reverse()
tracks = TrackTable.get_tracks_by_trackhashes([t.hash for t in tracks])
return {"tracks": serialize_tracks(tracks), "total": total}
@@ -118,6 +140,7 @@ def get_favorite_artists(query: GetAllOfTypeQuery):
)
artists.reverse()
artists = ArtistStore.get_artists_by_hashes(a.hash for a in artists)
return {"artists": [serialize_artist(a) for a in artists], "total": total}
@@ -164,9 +187,9 @@ def get_all_favorites(query: GetAllFavoritesQuery):
albums = []
artists = []
track_master_hash = TrackTable.get_all_hashes()
album_master_hash = AlbumTable.get_all_hashes()
artist_master_hash = ArtistTable.get_all_hashes()
track_master_hash = TrackStore.trackhashmap.keys()
album_master_hash = AlbumStore.albummap.keys()
artist_master_hash = ArtistStore.artistmap.keys()
# INFO: Filter out invalid hashes (file not found or tags edited)
for fav in favs:
@@ -188,12 +211,11 @@ def get_all_favorites(query: GetAllFavoritesQuery):
"artists": len(artists),
}
tracks = TrackTable.get_tracks_by_trackhashes(tracks, limit=track_limit)
albums = AlbumTable.get_albums_by_albumhashes(albums, limit=album_limit)
artists = ArtistTable.get_artists_by_artisthashes(artists, limit=artist_limit)
tracks = TrackStore.get_tracks_by_trackhashes(tracks[:track_limit])
albums = AlbumStore.get_albums_by_hashes(albums[:album_limit])
artists = ArtistStore.get_artists_by_hashes(artists[:artist_limit])
recents = []
# first_n = favs
for fav in favs:
if len(recents) >= largest:
+22 -7
View File
@@ -3,9 +3,11 @@ from flask_openapi3 import APIBlueprint
from pydantic import Field
from app.api.apischemas import TrackHashSchema
from app.db.libdata import AlbumTable, ArtistTable, TrackTable
from app.db.userdata import ScrobbleTable
from app.settings import Defaults
from app.store.albums import AlbumStore
from app.store.artists import ArtistStore
from app.store.tracks import TrackStore
bp_tag = Tag(name="Logger", description="Log item plays")
api = APIBlueprint("logger", __name__, url_prefix="/logger", abp_tags=[bp_tag])
@@ -33,14 +35,27 @@ def log_track(body: LogTrackBody):
if not timestamp or duration < 5:
return {"msg": "Invalid entry."}, 400
track = TrackTable.get_track_by_trackhash(body.trackhash)
if track is None:
trackentry = TrackStore.trackhashmap.get(body.trackhash)
if trackentry is None:
return {"msg": "Track not found."}, 404
ScrobbleTable.add(dict(body))
TrackTable.increment_playcount(body.trackhash, duration, timestamp)
AlbumTable.increment_playcount(track.albumhash, duration, timestamp)
ArtistTable.increment_playcount(track.artisthashes, duration, timestamp)
# Update play data on the in-memory stores
track = trackentry.tracks[0]
album = AlbumStore.albummap.get(track.albumhash)
if album:
album.increment_playcount(duration, timestamp)
for hash in track.artisthashes:
artist = ArtistStore.artistmap.get(hash)
if artist:
artist.increment_playcount(duration, timestamp)
track = TrackStore.trackhashmap.get(body.trackhash)
if track:
track.increment_playcount(duration, timestamp)
return {"msg": "recorded"}, 201
+8 -8
View File
@@ -9,9 +9,9 @@ from flask_openapi3 import APIBlueprint
from app import models
from app.api.apischemas import GenericLimitSchema
from app.db.libdata import TrackTable
from app.lib import searchlib
from app.settings import Defaults
from app.store.tracks import TrackStore
tag = Tag(name="Search", description="Search for tracks, albums and artists")
@@ -31,7 +31,7 @@ class Search:
Calls :class:`SearchTracks` which returns the tracks that fuzzily match
the search terms. Then adds them to the `SearchResults` store.
"""
self.tracks = TrackTable.get_all()
self.tracks = TrackStore.get_flat_list()
return searchlib.TopResults().search(self.query, tracks_only=True)
def search_artists(self):
@@ -124,7 +124,7 @@ def get_top_results(query: TopResultsQuery):
class SearchLoadMoreQuery(SearchQuery):
type: str = Field(description="The type of search", example="tracks")
index: int = Field(description="The index to start from", default=0)
start: int = Field(description="The index to start from", default=0)
@api.get("/loadmore")
@@ -136,26 +136,26 @@ def search_load_more(query: SearchLoadMoreQuery):
NOTE: You must first initiate a search using the `/search` endpoint.
"""
query = query.q
q = query.q
item_type = query.type
index = query.index
index = query.start
if item_type == "tracks":
t = Search(query).search_tracks()
t = Search(q).search_tracks()
return {
"tracks": t[index : index + SEARCH_COUNT],
"more": len(t) > index + SEARCH_COUNT,
}
elif item_type == "albums":
a = Search(query).search_albums()
a = Search(q).search_albums()
return {
"albums": a[index : index + SEARCH_COUNT],
"more": len(a) > index + SEARCH_COUNT,
}
elif item_type == "artists":
a = Search(query).search_artists()
a = Search(q).search_artists()
return {
"artists": a[index : index + SEARCH_COUNT],
"more": len(a) > index + SEARCH_COUNT,
+3 -2
View File
@@ -10,7 +10,7 @@ from app.db.sqlite.plugins import PluginsMethods as pdb
from app.db.sqlite.tracks import SQLiteTrackMethods as trackdb
from app.db.userdata import PluginTable
from app.lib import populate
from app.lib.tagger import index_everything
from app.lib.index import index_everything
from app.lib.watchdogg import Watcher as WatchDog
from app.logger import log
from app.settings import Info, Paths, SessionVarKeys
@@ -238,7 +238,8 @@ def set_setting(body: SetSettingBody):
@background
def run_populate():
populate.Populate(instance_key=get_random_str())
# populate.Populate(instance_key=get_random_str())
pass
@api.get("/trigger-scan")