Exemplo n.º 1
0
 def wrapped(*args, **kwargs):
     db = get_db()
     if request.method == 'GET':
         playlist = request.args.get('playlist')
     if request.method == 'POST':
         json = request.get_json() or {}
         playlist = json.get('playlist')
     if not playlist:
         return error('invalid payload: missing playlist id'), 400
     if not db.sismember('playlists', playlist):
         return error('playlist does not exist'), 404
     kwargs["playlist"] = playlist
     return view(*args, **kwargs)
Exemplo n.º 2
0
def search():
    query = request.args.get('query')
    if not query:
        return error('invalid payload: missing query string'), 400
    r = requests.get(current_app.config['SPOTIFY_SEARCH'],
                     params={
                         'q': query,
                         'type': 'track',
                         'limit': 10,
                     },
                     headers={'Authorization': f'Bearer {g.spotify_token}'})
    json = r.json() or {}
    if 'tracks' not in json:
        return error('bad response from spotify API'), 500
    tracks = json["tracks"]["items"]
    return jsonify({'songs': tracks}), 200
Exemplo n.º 3
0
def add_song(addr_hash, playlist, song):
    db = get_db()
    if not verify_song(song):
        return error('invalid song: bad song uri')
    db.sadd(f'playlist:{playlist}:songs', song)
    db.sadd(f'playlist:{playlist}:song:{song}', addr_hash)
    return ':)', 201
Exemplo n.º 4
0
def r_vote(addr_hash, playlist):
    db = get_db()
    json = request.get_json() or {}
    song = json.get('song')
    if not song:
        return error('invalid payload: missing song id'), 400
    if not db.sismember(f'playlist:{playlist}:songs', song):
        return add_song(addr_hash, playlist, song)
    db.sadd(f'playlist:{playlist}:song:{song}', addr_hash)
    return ':)', 200
Exemplo n.º 5
0
def r_create():
    db = get_db()
    json = request.get_json() or {}
    name = json.get('name')
    if not name:
        return error('invalid payload: missing playlist name'), 400
    next_id = db.incr('next_id')
    hash_id = get_hash(next_id)
    db.sadd('playlists', hash_id)
    db.set(f'playlist:{hash_id}', name)
    return jsonify({'id': hash_id}), 201
Exemplo n.º 6
0
 def wrapped(*args, **kwargs):
     if not refresh_token():
         return error('failed to authenticate with spotify API'), 500
     return view(*args, **kwargs)