From 02549e56183c7df2724c63ca7e59d35fa647089b Mon Sep 17 00:00:00 2001 From: David Madl Date: Sun, 31 May 2026 11:41:46 +0200 Subject: [PATCH] feat: fetch beat metadata from api --- api.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/api.py b/api.py index 3306f1a..4c06336 100644 --- a/api.py +++ b/api.py @@ -753,6 +753,44 @@ def playlist(playlist_id): }) +@app.route("/metadata", methods=["GET"]) +@require_auth +def get_metadata(): + track_id = request.args.get("trackId") + meta_type = request.args.get("type", "beats") + + if not track_id: + return jsonify({"ok": False, "error": "Missing trackId"}), 400 + + access_token = g.spotify_access_token + profile = spotify_get_me(access_token) + spotify_user_id = profile["id"] + + conn = db() + row = conn.execute(""" + SELECT file_name FROM uploaded_metadata + WHERE spotify_user_id = ? AND track_id = ? AND type = ? + ORDER BY created_at DESC + LIMIT 1 + """, (spotify_user_id, track_id, meta_type)).fetchone() + conn.close() + + if not row: + return jsonify({"ok": False, "error": "Not found"}), 404 + + file_path = os.path.join(METADATA_UPLOAD_DIR, row["file_name"]) + if not os.path.isfile(file_path): + return jsonify({"ok": False, "error": "Not found"}), 404 + + with open(file_path, "r", encoding="utf-8") as f: + collection = json.load(f) + + if not isinstance(collection, dict): + return jsonify({"ok": False, "error": "Invalid metadata file"}), 500 + + return jsonify({"ok": True, "collection": collection}) + + @app.route("/metadata", methods=["POST"]) @require_auth def upload_metadata():