Newer
Older
scover / app.py
from flask import Flask, request, jsonify
import requests

app = Flask(__name__)

def get_metadata_musicbrainz(artist, track):
    url = "https://musicbrainz.org/ws/2/recording/"
    params = {
        'query': f'artist:"{artist}" recording:"{track}"',
        'fmt': 'json'
    }
    response = requests.get(url, params=params)
    if response.status_code == 200:
        return response.json()
    else:
        return None

def get_cover_art(release_id):
    url = f"http://coverartarchive.org/release/{release_id}"
    response = requests.get(url)
    if response.status_code == 200:
        return response.json()
    else:
        return None

@app.route('/metadata', methods=['GET'])
def metadata():
    artist = request.args.get('artist')
    track = request.args.get('track')
    
    if not artist or not track:
        return jsonify({"error": "Params 'artist' & 'track' is require"}), 400

    metadata = get_metadata_musicbrainz(artist, track)
    if not metadata or 'recordings' not in metadata or len(metadata['recordings']) == 0:
        return jsonify({"error": "Metadata not found"}), 404

    recording = metadata['recordings'][0]
    
    cover_art = None
    if 'releases' in recording and len(recording['releases']) > 0:
        release_id = recording['releases'][0]['id']
        cover_art = get_cover_art(release_id)
    
    result = {
        "artist": artist,
        "track": track,
        "metadata": recording,
        "cover_art": cover_art  # None, if not found
    }
    return jsonify(result)

if __name__ == '__main__':
    app.run(port=5003, debug=True)