diff --git a/app/api/apischemas.py b/app/api/apischemas.py index eb024a90..e5ddd874 100644 --- a/app/api/apischemas.py +++ b/app/api/apischemas.py @@ -24,7 +24,6 @@ class ArtistHashSchema(BaseModel): """ Extending this class will give you a model with the `artisthash` field """ - artisthash: str = Field( description="The artist hash", example=Defaults.API_ARTISTHASH, @@ -68,6 +67,7 @@ class TrackLimitSchema(BaseModel): description="The number of tracks to return", example=Defaults.API_CARD_LIMIT, default=Defaults.API_CARD_LIMIT, + alias="tracklimit", ) diff --git a/app/api/artist.py b/app/api/artist.py index fff9d7d0..1a9c76d7 100644 --- a/app/api/artist.py +++ b/app/api/artist.py @@ -3,6 +3,7 @@ Contains all the artist(s) routes. """ import math +from pprint import pprint import random from datetime import datetime from itertools import groupby @@ -32,8 +33,18 @@ bp_tag = Tag(name="Artist", description="Single artist") api = APIBlueprint("artist", __name__, url_prefix="/artist", abp_tags=[bp_tag]) +class GetArtistAlbumsQuery(AlbumLimitSchema): + all: bool = Field( + description="Whether to ignore albumlimit and return all albums", default=False + ) + + +class GetArtistQuery(TrackLimitSchema, GetArtistAlbumsQuery): + albumlimit: int = Field(7, description="The number of albums to return") + + @api.get("/") -def get_artist(path: ArtistHashSchema, query: TrackLimitSchema): +def get_artist(path: ArtistHashSchema, query: GetArtistQuery): """ Get artist @@ -73,11 +84,18 @@ def get_artist(path: ArtistHashSchema, query: TrackLimitSchema): tracks = [ { **serialize_track(t), - "help_text": "unplayed" if t.playcount == 0 else f"{t.playcount} play{'' if t.playcount == 1 else 's'}" + "help_text": ( + "unplayed" + if t.playcount == 0 + else f"{t.playcount} play{'' if t.playcount == 1 else 's'}" + ), } for t in tracks[:limit] ] + query.limit = query.albumlimit + albums = get_artist_albums(path, query) + return { "artist": { **serialize_for_card(artist), @@ -88,15 +106,10 @@ def get_artist(path: ArtistHashSchema, query: TrackLimitSchema): "is_favorite": artist.is_favorite, }, "tracks": tracks, + "albums": albums, } -class GetArtistAlbumsQuery(AlbumLimitSchema): - all: bool = Field( - description="Whether to ignore limit and return all albums", default=False - ) - - @api.get("//albums") def get_artist_albums(path: ArtistHashSchema, query: GetArtistAlbumsQuery): """ @@ -176,7 +189,11 @@ def get_all_artist_tracks(path: ArtistHashSchema): tracks = [ { **serialize_track(t), - "help_text": "unplayed" if t.playcount == 0 else f"{t.playcount} play{'' if t.playcount == 1 else 's'}" + "help_text": ( + "unplayed" + if t.playcount == 0 + else f"{t.playcount} play{'' if t.playcount == 1 else 's'}" + ), } for t in tracks ] diff --git a/app/api/scrobble/__init__.py b/app/api/scrobble/__init__.py index f9d5fd18..0b73f12f 100644 --- a/app/api/scrobble/__init__.py +++ b/app/api/scrobble/__init__.py @@ -14,6 +14,7 @@ import locale from app.db.userdata import ScrobbleTable from app.lib.extras import get_extra_info from app.models.album import Album +from app.models.stats import StatItem from app.models.track import Track from app.serializers.artist import serialize_for_card from app.serializers.album import serialize_for_card as serialize_for_album_card @@ -304,35 +305,35 @@ def get_stats(): now = int(datetime.now().timestamp()) one_week_ago = now - 23731580 - total_tracks = { - "class": "trackcount", - "text": "Total tracks", - "value": len(TrackStore.get_flat_list()), - } + total_tracks = StatItem( + "trackcount", + "Total tracks", + len(TrackStore.get_flat_list()), + ) last_7_tracks, last_7_days_playcount, last_7_days_playduration = ( get_tracks_in_period(one_week_ago, now) ) - last_7_days_playcount = { - "class": "streams", - "text": "Track plays last week", - "value": last_7_days_playcount, - } + last_7_days_playcount = StatItem( + "streams", + "Track plays last week", + last_7_days_playcount, + ) - last_7_days_playduration = { - "class": "playtime", - "text": "Playtime last week", - "value": seconds_to_time_string(last_7_days_playduration), - } + last_7_days_playduration = StatItem( + "playtime", + "Playtime last week", + seconds_to_time_string(last_7_days_playduration), + ) last_7_tracks = sorted(last_7_tracks, key=lambda t: t.playduration, reverse=True) # Find the top track from the last 7 days - top_track = { - "class": "toptrack", - "text": "Top track last week", - "value": last_7_tracks[0].title, - } + top_track = StatItem( + "toptrack", + "Top track last week", + last_7_tracks[0].title, + ) return { "stats": [ diff --git a/app/models/stats.py b/app/models/stats.py new file mode 100644 index 00000000..3c1bc262 --- /dev/null +++ b/app/models/stats.py @@ -0,0 +1,8 @@ +from dataclasses import dataclass + + +@dataclass +class StatItem: + cssclass: str + text: str + value: str | int