Add song lookup API: search a track and get its play history + metadata
New GET /api/v1/me/tracks/search finds songs by track or artist name; GET /api/v1/me/tracks returns a song's aggregated metadata (album, duration, mbid, etc. - most common non-null value per field) plus its paginated scrobble history, identified by artist+track query params since there's no normalized Track model. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
50e2360d14
commit
d2f457a84d
6 files changed
+552
-2
No files matched your search
@@ -0,0 +1,52 @@
|
||||
from flask import g
|
||||
from flask_smorest import Blueprint, abort
|
||||
|
||||
from scrobbler.api.decorators import authenticated
|
||||
from scrobbler.schemas import (
|
||||
ErrorSchema,
|
||||
TrackDetailArgsSchema,
|
||||
TrackDetailSchema,
|
||||
TrackSearchArgsSchema,
|
||||
TrackSearchResultSchema,
|
||||
)
|
||||
from scrobbler.services import stats
|
||||
|
||||
blp = Blueprint("Tracks", __name__, description="Look up a specific song's history and metadata")
|
||||
|
||||
|
||||
@blp.route("/search", methods=["GET"])
|
||||
@authenticated(blp)
|
||||
@blp.arguments(TrackSearchArgsSchema, location="query")
|
||||
@blp.response(200, TrackSearchResultSchema(many=True))
|
||||
def search(args):
|
||||
"""Find a song by track or artist name"""
|
||||
return stats.track_search(g.user, args["q"], args["limit"])
|
||||
|
||||
|
||||
@blp.route("", methods=["GET"])
|
||||
@authenticated(blp)
|
||||
@blp.arguments(TrackDetailArgsSchema, location="query")
|
||||
@blp.response(200, TrackDetailSchema)
|
||||
@blp.alt_response(404, schema=ErrorSchema, description="You've never scrobbled this song")
|
||||
def detail(args):
|
||||
"""A song's play history and recorded metadata"""
|
||||
metadata = stats.track_metadata(g.user, args["artist"], args["track"])
|
||||
if metadata is None:
|
||||
abort(404, code="not_found", message="You've never scrobbled this song")
|
||||
history = stats.recent(
|
||||
g.user,
|
||||
page=args["page"],
|
||||
per_page=args["limit"],
|
||||
artist=args["artist"],
|
||||
track=args["track"],
|
||||
)
|
||||
return {
|
||||
"artist": metadata.artist,
|
||||
"track": metadata.track,
|
||||
"metadata": metadata,
|
||||
"items": history.items,
|
||||
"page": history.page,
|
||||
"per_page": history.per_page,
|
||||
"total": history.total,
|
||||
"total_pages": history.total_pages,
|
||||
}
|
||||
Reference in new issue
Block a user