fixed iteration error handling on get_playlist, added api rate limit handling
This commit is contained in:
parent
3c50bf7a5a
commit
11f4ac7b56
@ -1,6 +1,7 @@
|
|||||||
import requests
|
import requests
|
||||||
import random
|
import random
|
||||||
import logging
|
import logging
|
||||||
|
import time
|
||||||
from . import const
|
from . import const
|
||||||
from spotframework.model.playlist import Playlist
|
from spotframework.model.playlist import Playlist
|
||||||
|
|
||||||
@ -24,8 +25,20 @@ class Network:
|
|||||||
logger.debug(f'{method} get {req.status_code}')
|
logger.debug(f'{method} get {req.status_code}')
|
||||||
return req.json()
|
return req.json()
|
||||||
else:
|
else:
|
||||||
error_text = req.json()['error']['message']
|
|
||||||
logger.error(f'{method} get {req.status_code} {error_text}')
|
if req.status_code == 429:
|
||||||
|
retry_after = req.headers.get('Retry-After', None)
|
||||||
|
|
||||||
|
if retry_after:
|
||||||
|
logger.warning(f'{method} rate limit reached: retrying in {retry_after} seconds')
|
||||||
|
time.sleep(int(retry_after))
|
||||||
|
return self._make_get_request(method, url, params, headers)
|
||||||
|
else:
|
||||||
|
logger.error(f'{method} rate limit reached: cannot find Retry-After header')
|
||||||
|
|
||||||
|
else:
|
||||||
|
error_text = req.json()['error']['message']
|
||||||
|
logger.error(f'{method} get {req.status_code} {error_text}')
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@ -39,8 +52,20 @@ class Network:
|
|||||||
logger.debug(f'{method} post {req.status_code}')
|
logger.debug(f'{method} post {req.status_code}')
|
||||||
return req
|
return req
|
||||||
else:
|
else:
|
||||||
error_text = str(req.text)
|
|
||||||
logger.error(f'{method} post {req.status_code} {error_text}')
|
if req.status_code == 429:
|
||||||
|
retry_after = req.headers.get('Retry-After', None)
|
||||||
|
|
||||||
|
if retry_after:
|
||||||
|
logger.warning(f'{method} rate limit reached: retrying in {retry_after} seconds')
|
||||||
|
time.sleep(int(retry_after))
|
||||||
|
return self._make_post_request(method, url, params, json, headers)
|
||||||
|
else:
|
||||||
|
logger.error(f'{method} rate limit reached: cannot find Retry-After header')
|
||||||
|
|
||||||
|
else:
|
||||||
|
error_text = str(req.text)
|
||||||
|
logger.error(f'{method} post {req.status_code} {error_text}')
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@ -54,8 +79,20 @@ class Network:
|
|||||||
logger.debug(f'{method} put {req.status_code}')
|
logger.debug(f'{method} put {req.status_code}')
|
||||||
return req
|
return req
|
||||||
else:
|
else:
|
||||||
error_text = str(req.text)
|
|
||||||
logger.error(f'{method} put {req.status_code} {error_text}')
|
if req.status_code == 429:
|
||||||
|
retry_after = req.headers.get('Retry-After', None)
|
||||||
|
|
||||||
|
if retry_after:
|
||||||
|
logger.warning(f'{method} rate limit reached: retrying in {retry_after} seconds')
|
||||||
|
time.sleep(int(retry_after))
|
||||||
|
return self._make_put_request(method, url, params, json, headers)
|
||||||
|
else:
|
||||||
|
logger.error(f'{method} rate limit reached: cannot find Retry-After header')
|
||||||
|
|
||||||
|
else:
|
||||||
|
error_text = str(req.text)
|
||||||
|
logger.error(f'{method} put {req.status_code} {error_text}')
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@ -112,7 +149,7 @@ class Network:
|
|||||||
|
|
||||||
# playlists = playlists + resp['items']
|
# playlists = playlists + resp['items']
|
||||||
|
|
||||||
if resp['next']:
|
if resp.get('next', None):
|
||||||
more_playlists = self.get_playlists(offset + limit)
|
more_playlists = self.get_playlists(offset + limit)
|
||||||
if more_playlists:
|
if more_playlists:
|
||||||
playlists += more_playlists
|
playlists += more_playlists
|
||||||
@ -150,15 +187,14 @@ class Network:
|
|||||||
tracks += resp['items']
|
tracks += resp['items']
|
||||||
else:
|
else:
|
||||||
logger.warning(f'{playlistid} no items returned')
|
logger.warning(f'{playlistid} no items returned')
|
||||||
|
|
||||||
|
if resp.get('next', None):
|
||||||
|
more_tracks = self.get_playlist_tracks(playlistid, offset + limit)
|
||||||
|
if more_tracks:
|
||||||
|
tracks += more_tracks
|
||||||
else:
|
else:
|
||||||
logger.warning(f'{playlistid} error on response')
|
logger.warning(f'{playlistid} error on response')
|
||||||
|
|
||||||
if resp.get('next', None):
|
|
||||||
|
|
||||||
more_tracks = self.get_playlist_tracks(playlistid, offset + limit)
|
|
||||||
if more_tracks:
|
|
||||||
tracks += more_tracks
|
|
||||||
|
|
||||||
return tracks
|
return tracks
|
||||||
|
|
||||||
def get_available_devices(self):
|
def get_available_devices(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user