spotipy-dev / spotipy

A light weight Python library for the Spotify Web API
http://spotipy.readthedocs.org
MIT License
5.03k stars 957 forks source link

sp.recommendation() can't handle >5 artists/songs/genres as input - gives 401 error #1070

Closed macafra closed 10 months ago

macafra commented 10 months ago

Describe the bug When I use sp.recommendations(), the total amount of artists, songs and genres I put in cannot exceed 5 before I get a 401 (no token provided) error. If I put in 5 or less artists/songs/genres, it outputs a recommendation as expected.

Your code

import spotipy
from spotipy.oauth2 import SpotifyClientCredentials

spotify = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials())
genre_list = spotify.recommendation_genre_seeds()['genres']

def get_artist_id(name):
    results = spotify.search(q='artist:' + name, type='artist')
    items = results['artists']['items']
    if len(items) > 0:
        return items[0]['id']
    else:
        return None

def get_song_id(name):
    results = spotify.search(q='track:' + name, type='track')
    items = results['tracks']['items']
    if len(items) > 0:
        return items[0]['id']
    else:
        return None

def filter_artist_list(artists):
    artist_id_list = []
    for artist in artists:
        artist_id = get_artist_id(artist)
        if artist_id != None:
            artist_id_list.append(artist_id)
        if len(artist_id_list) >= 5:
            break
    return artist_id_list

def filter_song_list(songs):
    song_list = []
    song_id_list = []
    for song in songs:
        song_id = get_song_id(song)
        if song_id != None:
            song_list.append(song)
            song_id_list.append(song_id)
        if len(song_list) >= 5:
            break
    print(song_list)
    return song_id_list

def filter_genre_list(genres):
    usable_genre_list = []
    for genre in genres:
        if genre in genre_list:
            usable_genre_list.append(genre)
        if len(usable_genre_list) >= 5:
            break
    return usable_genre_list

def show_recommendations(artists=None, genres=None, songs=None):
    results = spotify.recommendations(seed_artists=artists, seed_genres=genres, seed_tracks=songs, limit=20)
    track = results['tracks'][0]
    print('Recommendation: '+ track['name'] +  ' - ' +  track['artists'][0]['name'] + '\nLink: ' + track['external_urls']['spotify'])

liked_artists = ['Birdy', 'Rihanna', 'Beyonce', 'Ariana Grande', 'Lady Gaga']
liked_genres = ['pop', 'metal']
liked_songs = ['california king bed', 'Girls']

show_recommendations(filter_artist_list(liked_artists), filter_genre_list(liked_genres), filter_song_list(liked_songs))

Expected behavior I expect recommendations() to output a recommendation regardless of list length

Output HTTP Error for GET to https://api.spotify.com/v1/recommendations with Params: {'limit': 20, 'seed_artists': '2WX2uTcsvV5OnS0inACecP,5pKCCKE2ajJHZ9KAiaK11H,6vWDO969PvNqNYHIOW5v0m,66CXWjxzNUsdJxJ2JdwvnR,1HY2Jd0NmPuamShAr6KMms', 'seed_genres': 'pop,metal', 'seed_tracks': '1NpDnkQO0yWxTtntrC0UNz,4VXIryQMWpIdGgYR4TrjT1'} returned 400 due to invalid request Traceback (most recent call last): File "[...]\spotipy\client.py", line 271, in _internal_call response.raise_for_status() File "[...]\requests\models.py", line 1021, in raise_for_status raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://api.spotify.com/v1/recommendations?limit=20&seed_artists=2WX2uTcsvV5OnS0inACecP%2C5pKCCKE2ajJHZ9KAiaK11H%2C6vWDO969PvNqNYHIOW5v0m%2C66CXWjxzNUsdJxJ2JdwvnR%2C1HY2Jd0NmPuamShAr6KMms&seed_genres=pop%2Cmetal&seed_tracks=1NpDnkQO0yWxTtntrC0UNz%2C4VXIryQMWpIdGgYR4TrjT1

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "[...]\spotipy_test.py", line 72, in show_recommendations(filter_artist_list(liked_artists), filter_genre_list(liked_genres), filter_song_list(liked_songs)) File "[...]\spotipy_test.py", line 61, in show_recommendations results = spotify.recommendations(seed_artists=artists, seed_genres=genres, seed_tracks=songs, limit=20) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "[...]\spotipy\client.py", line 1712, in recommendations return self._get("recommendations", **params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File [...]\spotipy\client.py", line 323, in _get return self._internal_call("GET", url, payload, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "[...]\spotipy\client.py", line 293, in _internal_call raise SpotifyException( spotipy.exceptions.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/recommendations?limit=20&seed_artists=2WX2uTcsvV5OnS0inACecP%2C5pKCCKE2ajJHZ9KAiaK11H%2C6vWDO969PvNqNYHIOW5v0m%2C66CXWjxzNUsdJxJ2JdwvnR%2C1HY2Jd0NmPuamShAr6KMms&seed_genres=pop%2Cmetal&seed_tracks=1NpDnkQO0yWxTtntrC0UNz%2C4VXIryQMWpIdGgYR4TrjT1: invalid request, reason: None

Environment:

Additional context Add any other context about the problem here.

macafra commented 10 months ago

I noticed the intention of the function is max 5 seeds