return albums in get artist

This commit is contained in:
cwilvx
2024-10-13 18:13:00 +03:00
parent 99b30b5894
commit 7ef63c1f10
4 changed files with 56 additions and 30 deletions
+1 -1
View File
@@ -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",
)
+26 -9
View File
@@ -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("/<string:artisthash>")
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("/<artisthash>/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
]
+21 -20
View File
@@ -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": [
+8
View File
@@ -0,0 +1,8 @@
from dataclasses import dataclass
@dataclass
class StatItem:
cssclass: str
text: str
value: str | int