[Python] How to implement spotify recommendations API?

Discussão em 'Python' iniciado por Stack, Fevereiro 3, 2025.

  1. Stack

    Stack Membro Participativo

    Hello I've been working on this code that filters my top-200 songs on spotify, filters them by genre, then updates a playlist. Everything has been good until here, the problem is I wanted to implement the spotify reccomendations API to add also some of the spotify reccomended songs. I can't seem to figure out where the problem is, these are the errors I am getting:

    HTTP Error for GET to https://api.spotify.com/v1/recommendations with Params: {'limit': 20, 'seed_genres': 'pop'} returned 404 due to None
    Traceback (most recent call last):
    File "C:\Users\Byte\Documents\Python\Spotify Playlist POP\.venv\Lib\site-packages\spotipy\client.py", line 275, in _internal_call
    response.raise_for_status()
    File "C:\Users\Byte\Documents\Python\Spotify Playlist POP\.venv\Lib\site-packages\requests\models.py", line 1024, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
    requests.exceptions.HTTPError: 404 Client Error: Not Found for url: https://api.spotify.com/v1/recommendations?limit=20&seed_genres=pop

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
    File "C:\Users\Byte\Documents\Python\Spotify Playlist POP\test2.py", line 31, in <module>
    print(combine())
    ^^^^^^^^^
    File "C:\Users\Byte\Documents\Python\Spotify Playlist POP\test2.py", line 24, in combine
    recommendations = sp.recommendations(seed_tracks=None,seed_genres=['pop'],seed_artists=None,limit=20)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\Byte\Documents\Python\Spotify Playlist POP\.venv\Lib\site-packages\spotipy\client.py", line 1728, in recommendations
    return self._get("recommendations", **params)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\Byte\Documents\Python\Spotify Playlist POP\.venv\Lib\site-packages\spotipy\client.py", line 327, in _get
    return self._internal_call("GET", url, payload, kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    File "C:\Users\Byte\Documents\Python\Spotify Playlist POP\.venv\Lib\site-packages\spotipy\client.py", line 297, in _internal_call
    raise SpotifyException(
    spotipy.exceptions.SpotifyException: http status: 404, code:-1 - https://api.spotify.com/v1/recommendations?limit=20&seed_genres=pop:
    None, reason: None

    Process finished with exit code 1


    and this is my code:

    import spotipy
    from spotipy.oauth2 import SpotifyOAuth

    # Spotify API credentials
    CLIENT_ID = 'xxxxx'
    CLIENT_SECRET = 'xxxxxx'
    REDIRECT_URI = 'http://localhost:8080/callback'
    SCOPE = "user-top-read playlist-modify-public playlist-modify-private"

    # Playlist ID for the existing playlist
    TARGET_PLAYLIST_ID = '3Eu0FwJvtjDifEJVSzW8m0'

    # Initialize Spotify API client
    sp = spotipy.Spotify(auth_manager=SpotifyOAuth(
    client_id=CLIENT_ID,
    client_secret=CLIENT_SECRET,
    redirect_uri=REDIRECT_URI,
    scope=SCOPE
    ))

    def combine():
    # Calculate how many more tracks are needed to reach 50
    seed_tracks = ['0cGG2EouYCEEC3xfa0tDFV','7lQ8MOhq6IN2w8EYcFNSUk','68Dni7IE4VyPkTOH9mRWHr','2cYqizR4lgvp4Qu6IQ3qGN']
    recommendations = sp.recommendations(seed_tracks=None,seed_genres=['pop'],seed_artists=None,limit=20)

    recommended_track_ids = [track['id'] for track in recommendations['tracks']]


    return recommended_track_ids

    print(combine())

    Continue reading...

Compartilhe esta Página