feat: implemented artwork fetching/downloading
This commit is contained in:
45
app.py
45
app.py
@@ -615,6 +615,51 @@ def save_settings():
|
||||
return jsonify({"ok": True})
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Artwork routes
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
@app.route("/api/artwork", methods=["GET"])
|
||||
def get_artwork():
|
||||
from monochrome.spotify_to_ids import parse_spotify_url, fetch_spotify_embed
|
||||
spotify_url = request.args.get("url", "").strip()
|
||||
if not spotify_url:
|
||||
return jsonify({"error": "No URL provided"}), 400
|
||||
|
||||
sp_type, sp_id = parse_spotify_url(spotify_url)
|
||||
if not sp_type:
|
||||
return jsonify({"error": "Invalid Spotify URL"}), 400
|
||||
|
||||
embed_data = fetch_spotify_embed(sp_type, sp_id)
|
||||
if not embed_data:
|
||||
return jsonify({"error": "Could not fetch Spotify metadata"}), 502
|
||||
|
||||
try:
|
||||
entity = embed_data["props"]["pageProps"]["state"]["data"]["entity"]
|
||||
except (KeyError, TypeError):
|
||||
return jsonify({"error": "Unexpected Spotify response format"}), 502
|
||||
|
||||
# entity.visualIdentity.image[] — confirmed structure from Spotify embed page
|
||||
# Each entry: {"url": "https://image-cdn-ak.spotifycdn.com/image/...", "maxWidth": N, "maxHeight": N}
|
||||
images = (entity.get("visualIdentity") or {}).get("image", [])
|
||||
if not images:
|
||||
app.logger.warning("Artwork not found. Entity keys: %s", list(entity.keys()))
|
||||
return jsonify({"error": "No artwork found for this URL"}), 404
|
||||
|
||||
best = max(images, key=lambda img: img.get("maxWidth", 0))
|
||||
url = best["url"]
|
||||
|
||||
# Upscale to 2000×2000 using the same CDN key technique as votify-fix.
|
||||
# Spotify CDN filenames: first 16 hex chars = size key, remainder = image hash.
|
||||
# Source: GladistonXD/votify-fix constants.py COVER_SIZE_X_KEY_MAPPING_SONG
|
||||
EXTRA_LARGE_KEY = "ab67616d000082c1"
|
||||
parts = url.rsplit("/", 1)
|
||||
if len(parts) == 2 and len(parts[1]) >= 16:
|
||||
url = parts[0] + "/" + EXTRA_LARGE_KEY + parts[1][16:]
|
||||
|
||||
return jsonify({"image_url": url})
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Job routes
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user