Listening stats: Last.fm user.* methods and /api/v1/me
CI / lint (push) Successful in 12s
CI / test (push) Successful in 30s
CI / notify (push) Successful in 5s

- Stats service shared by both APIs: recent scrobbles, top artists,
  albums and tracks over Last.fm periods, per-day/week/month counts
  (with empty days filled) and totals
- Case-insensitive grouping that displays the most common spelling
- user.getInfo, user.getRecentTracks (now playing first, from/to),
  user.getTopArtists/Albums/Tracks with Last.fm paging attributes
- /api/v1/me: now-playing, recent, top/{artists,albums,tracks},
  counts and summary

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MK52BGoJbPhZ1vSFuKcGUW
This commit is contained in:
mattandClaude Opus 5.5 committed 2026-09-25 14:05:20 +01:00
1 parent 2702815a1e
commit 0d49311848
7 files changed
+769 -2

No files matched your search

+2 -1
View File
@@ -43,9 +43,10 @@ def init_app(app: Flask) -> None:
},
)
from scrobbler.api import apps, auth, sessions, tokens
from scrobbler.api import apps, auth, sessions, stats, tokens
api.register_blueprint(auth.blp, url_prefix="/api/v1/auth")
api.register_blueprint(apps.blp, url_prefix="/api/v1/apps")
api.register_blueprint(sessions.blp, url_prefix="/api/v1/sessions")
api.register_blueprint(tokens.blp, url_prefix="/api/v1/tokens")
api.register_blueprint(stats.blp, url_prefix="/api/v1/me")
+90
View File
@@ -0,0 +1,90 @@
from flask import g
from flask_smorest import Blueprint
from scrobbler.api.decorators import authenticated
from scrobbler.schemas import (
CountsArgsSchema,
CountSchema,
NowPlayingSchema,
PageArgsSchema,
RecentPageSchema,
SummarySchema,
TopArgsSchema,
TopPageSchema,
)
from scrobbler.services import scrobbles, stats
blp = Blueprint("ui_stats", __name__, description="Your listening history and statistics")
TOP_KINDS = {
"artists": stats.top_artists,
"albums": stats.top_albums,
"tracks": stats.top_tracks,
}
def _page_body(page: stats.Page) -> dict:
return {
"items": page.items,
"page": page.page,
"per_page": page.per_page,
"total": page.total,
"total_pages": page.total_pages,
}
@blp.route("/now-playing", methods=["GET"])
@authenticated(blp)
@blp.response(200, NowPlayingSchema)
def now_playing():
"""What you're listening to now"""
return {"now_playing": scrobbles.get_now_playing(g.user)}
@blp.route("/recent", methods=["GET"])
@authenticated(blp)
@blp.arguments(PageArgsSchema, location="query")
@blp.response(200, RecentPageSchema)
def recent(args):
"""Recent scrobbles, newest first"""
return _page_body(stats.recent(g.user, page=args["page"], per_page=args["limit"]))
def _top(kind):
def view(args):
page = TOP_KINDS[kind](g.user, args["period"], args["page"], args["limit"])
return {**_page_body(page), "period": args["period"]}
view.__name__ = f"top_{kind}"
view.__doc__ = f"Top {kind} over a period"
return view
for _kind in TOP_KINDS:
blp.route(f"/top/{_kind}", methods=["GET"])(
authenticated(blp)(
blp.arguments(TopArgsSchema, location="query")(
blp.response(200, TopPageSchema)(_top(_kind))
)
)
)
@blp.route("/counts", methods=["GET"])
@authenticated(blp)
@blp.arguments(CountsArgsSchema, location="query")
@blp.response(200, CountSchema(many=True))
def counts(args):
"""Scrobbles per day, week or month
Day buckets include days with no scrobbles, so the series is continuous.
"""
return stats.listen_counts(g.user, args["period"], args["bucket"])
@blp.route("/summary", methods=["GET"])
@authenticated(blp)
@blp.response(200, SummarySchema)
def summary():
"""Totals: scrobbles, distinct artists and tracks"""
return stats.summary(g.user)