Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recommendations timeout #946

Open
yuma-dev opened this issue Mar 4, 2023 · 3 comments
Open

recommendations timeout #946

yuma-dev opened this issue Mar 4, 2023 · 3 comments
Labels
api-bug A bug in the Spotify API, not in the library itself

Comments

@yuma-dev
Copy link

yuma-dev commented Mar 4, 2023

Describe the bug
Whenever I try to use the recommendations function I get this error:
requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='api.spotify.com', port=443): Read timed out. (read timeout=5)
I've tried at different times because I thought the Spotify servers might be messed up rn but it keeps happening with new code and different parameters.

Your code
only one of many examples:

def create_recommendations_playlist():
    # Set up the client credentials manager with your client ID and secret
    client_id = "********************************"
    client_secret = "********************************"

    # initializing the Spotify client using client id and client secret
    sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=client_id, client_secret=client_secret, redirect_uri="http://localhost:8000/callback", scope="playlist-modify-public user-top-read"))


    # get the user's top tracks 
    top_tracks = sp.current_user_top_tracks(limit=50, time_range="short_term")
    top_tracks_ids = [track['id'] for track in top_tracks["items"]]

    # get recommended tracks based on your top tracks
    recommendations = pd.DataFrame(sp.recommendations(seed_tracks=top_tracks_ids, limit=20)["tracks"])
    recommendations_ids = recommendations['id'].tolist()

    # create a new playlist with the recommended tracks
    playlist = sp.user_playlist_create(user=sp.current_user()["id"], name="Your Recommendations", public=True)
    playlist_id = playlist["id"]

    # add the recommended tracks to the playlist
    add_tracks_to_playlist = sp.playlist_add_items(playlist_id=playlist_id, items=recommendations_ids)
    return "Successfully created a new playlist with {} recommended tracks added.".format(len(recommendations_ids))

Expected behavior
doesn't Time out

Output

Traceback (most recent call last):
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 449, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 444, in _make_request
    httplib_response = conn.getresponse()
                       ^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\http\client.py", line 1374, in getresponse
    response.begin()
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\http\client.py", line 318, in begin
    version, status, reason = self._read_status()
                              ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\http\client.py", line 279, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\socket.py", line 705, in readinto
    return self._sock.recv_into(b)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\ssl.py", line 1278, in recv_into
    return self.read(nbytes, buffer)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\ssl.py", line 1134, in read
    return self._sslobj.read(len, buffer)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TimeoutError: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\adapters.py", line 489, in send
    resp = conn.urlopen(
           ^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 787, in urlopen
    retries = retries.increment(
              ^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\util\retry.py", line 550, in increment
    raise six.reraise(type(error), error, _stacktrace)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\packages\six.py", line 770, in reraise
    raise value
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
                       ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 451, in _make_request
    self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\urllib3\connectionpool.py", line 340, in _raise_timeout
    raise ReadTimeoutError(
urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='api.spotify.com', port=443): Read timed out. (read timeout=5)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Fabia\OneDrive\Dokumente\Coding\Python\captainai\lib\test.py", line 35, in <module>
    create_playlist()
  File "C:\Users\Fabia\OneDrive\Dokumente\Coding\Python\captainai\lib\test.py", line 24, in create_playlist
    recommendations = pd.DataFrame(sp.recommendations(seed_tracks=top_tracks_ids, limit=20)["tracks"])
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\spotipy\client.py", line 1667, in recommendations
    return self._get("recommendations", **params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\spotipy\client.py", line 297, in _get
    return self._internal_call("GET", url, payload, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\spotipy\client.py", line 240, in _internal_call
    response = self._session.request(
               ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\sessions.py", line 587, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\sessions.py", line 701, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Fabia\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\adapters.py", line 578, in send
    raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='api.spotify.com', port=443): Read timed out. (read timeout=5)

Process finished with exit code 1

Environment:

  • OS: Windows 11
  • Python version: 3.11.0
  • spotipy version: 2.22.0
@yuma-dev yuma-dev added the bug label Mar 4, 2023
@Heroesluk
Copy link

It works when you limit number of provided seed tracks to 5 or less though?

spotipy.recommendations function specifies that
""" Get a list of recommended tracks for one to five seeds.
(at least one of seed_artists, seed_tracks and seed_genres
are needed)"""

If i understand correctly this is intended behaviour that you can't provide more then 5 tracks/artists/genres

@jwreiser
Copy link

jwreiser commented Mar 7, 2024

I will provide a list of one seed and still get a time out. This was working before without a code change or updating spotipy so I imagine there is some issue on Spotify's side (is it rate limiting just this call (the others are fine)? Is it having an issue finding a recommendation?) I even lowered the limit from 20 (where it used to work) to 3 and it still times out. An even bigger problem is I can not get the requests_timeout to be honored so my application freezes a long time waiting for the timeout.
Here is my setup:

scope = "user-read-playback-state,user-modify-playback-state,user-library-modify,user-follow-modify"#
sp = spotipy.Spotify(client_credentials_manager=SpotifyOAuth(username='XXXXXXXXX', scope=scope),requests_timeout=15)

# and one of my recommendation calls:
try:
    recommendations = sp.recommendations(seed_artists=[artist_id], limit=10)
    for track in recommendations['tracks']:
        if not filter_results or track['uri'] not in list(songs_df):
            songs.append(track['uri'])
except:
    print('could not get song recommendations')

@dieser-niko dieser-niko added api-bug A bug in the Spotify API, not in the library itself and removed bug labels Aug 5, 2024
@dieser-niko
Copy link
Member

You could try out the same request on the Web API Documentation, just to make sure that this is not caused by the library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-bug A bug in the Spotify API, not in the library itself
Projects
None yet
Development

No branches or pull requests

4 participants