VSU
VSU

Reputation: 41

Spotify Python API call timeout issues

I am getting below error on Spotify API call. is there work around to resolve these timeout issues. Python Spotify Code

TimeoutError Traceback (most recent call last) C:\ProgramData\Anaconda3\lib\site-packages\urllib3\util\connection.py in create_connection(address, timeout, source_address, socket_options) 73 sock.bind(source_address) ---> 74 sock.connect(sa) 75 return sock

TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

During handling of the above exception, another exception occurred:

KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-11-eb4cef5ffdbb> in <module>
      5     #print(artist)
      6     #time.sleep(2)
----> 7     result = spotify.search(artist,search_type='artist')['artists']['items']
      8     #print(result)
      9     try:

<ipython-input-7-6a2d183a2f3a> in search(self, query, search_type)
     80         data = urlencode({"q": query, "type": search_type.lower()})
     81         lookup_url = f"{endpoint}?{data}"
---> 82         r = requests.get(lookup_url, headers=headers)#.json()
     83         if r.status_code not in range(200, 299):
     84             return {}

C:\ProgramData\Anaconda3\lib\site-packages\requests\api.py in get(url, params, **kwargs)
     74 
     75     kwargs.setdefault('allow_redirects', True)
---> 76     return request('get', url, params=params, **kwargs)
     77 
     78 

C:\ProgramData\Anaconda3\lib\site-packages\requests\api.py in request(method, url, **kwargs)
     59     # cases, and look like a memory leak in others.
     60     with sessions.Session() as session:
---> 61         return session.request(method=method, url=url, **kwargs)
     62 
     63 

C:\ProgramData\Anaconda3\lib\site-packages\requests\sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    528         }
    529         send_kwargs.update(settings)
--> 530         resp = self.send(prep, **send_kwargs)
    531 
    532         return resp

C:\ProgramData\Anaconda3\lib\site-packages\requests\sessions.py in send(self, request, **kwargs)
    641 
    642         # Send the request
--> 643         r = adapter.send(request, **kwargs)
    644 
    645         # Total elapsed time of the request (approximately)

C:\ProgramData\Anaconda3\lib\site-packages\requests\adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    437         try:
    438             if not chunked:
--> 439                 resp = conn.urlopen(
    440                     method=request.method,
    441                     url=url,

C:\ProgramData\Anaconda3\lib\site-packages\urllib3\connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    668 
    669             # Make the request on the httplib connection object.
--> 670             httplib_response = self._make_request(
    671                 conn,
    672                 method,

C:\ProgramData\Anaconda3\lib\site-packages\urllib3\connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    379         # Trigger any extra validation we need to do.
    380         try:
--> 381             self._validate_conn(conn)
    382         except (SocketTimeout, BaseSSLError) as e:
    383             # Py2 raises this as a BaseSSLError, Py3 raises it as socket timeout.

C:\ProgramData\Anaconda3\lib\site-packages\urllib3\connectionpool.py in _validate_conn(self, conn)
    974         # Force connect early to allow us to validate the connection.
    975         if not getattr(conn, "sock", None):  # AppEngine might not have  `.sock`
--> 976             conn.connect()
    977 
    978         if not conn.is_verified:

C:\ProgramData\Anaconda3\lib\site-packages\urllib3\connection.py in connect(self)
    306     def connect(self):
    307         # Add certificate verification
--> 308         conn = self._new_conn()
    309         hostname = self.host
    310 

C:\ProgramData\Anaconda3\lib\site-packages\urllib3\connection.py in _new_conn(self)
    157 
    158         try:
--> 159             conn = connection.create_connection(
    160                 (self._dns_host, self.port), self.timeout, **extra_kw
    161             )

C:\ProgramData\Anaconda3\lib\site-packages\urllib3\util\connection.py in create_connection(address, timeout, source_address, socket_options)
     72             if source_address:
     73                 sock.bind(source_address)
---> 74             sock.connect(sa)
     75             return sock
     76 

KeyboardInterrupt: 

1
###########Create Data frame with required columns to be extracte


  [1]: https://i.sstatic.net/lCaz4.p

ng

Upvotes: 4

Views: 3105

Answers (1)

Alec Mather
Alec Mather

Reputation: 828

I really wish spotipy would address this issue in their package, but nonetheless, here's my workaround:

  1. Import the error type

If you don't catch this exact error type, you'll cause problems for yourself by leaving your catch open-ended. For example, if you have a TypeError in your syntax or something, it will error that out and go to your catch block, and you'll have no way of knowing that it errored in the first place.

from requests.exceptions import ReadTimeout

  1. When you init your spotipy client, I recommend adding in a couple of params that might be helpful.
spotify = spotipy.Spotify(auth=spotify_token, requests_timeout=10, retries=10)
  • requests_timeout | throws the same error you're looking for after a number of seconds. I use this just so that it doesn't hang forever, and it'll just retry the request if nothing comes back after 10 seconds.
  • retries | the maximum number of retries spotipy will request if it gets an error. I think it defaults to 3, so I just like to increase this a little bit just in case.
  1. Run a try/except type retry system

You can definitely expand this to make it keep retrying (using a while loop or something) if it keeps hanging, but this is the basic idea:

try:

  result = spotify.search(artist,search_type='artist')['artists']['items']

except ReadTimeout:

  print('Spotify timed out... trying again...')
  result = spotify.search(artist,search_type='artist')['artists']['items']

Upvotes: 3

Related Questions