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
|
||||||
|
|
||||||
@ -23,6 +24,18 @@ class Network:
|
|||||||
if 200 <= req.status_code < 300:
|
if 200 <= req.status_code < 300:
|
||||||
logger.debug(f'{method} get {req.status_code}')
|
logger.debug(f'{method} get {req.status_code}')
|
||||||
return req.json()
|
return req.json()
|
||||||
|
else:
|
||||||
|
|
||||||
|
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:
|
else:
|
||||||
error_text = req.json()['error']['message']
|
error_text = req.json()['error']['message']
|
||||||
logger.error(f'{method} get {req.status_code} {error_text}')
|
logger.error(f'{method} get {req.status_code} {error_text}')
|
||||||
@ -38,6 +51,18 @@ class Network:
|
|||||||
if 200 <= req.status_code < 300:
|
if 200 <= req.status_code < 300:
|
||||||
logger.debug(f'{method} post {req.status_code}')
|
logger.debug(f'{method} post {req.status_code}')
|
||||||
return req
|
return req
|
||||||
|
else:
|
||||||
|
|
||||||
|
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:
|
else:
|
||||||
error_text = str(req.text)
|
error_text = str(req.text)
|
||||||
logger.error(f'{method} post {req.status_code} {error_text}')
|
logger.error(f'{method} post {req.status_code} {error_text}')
|
||||||
@ -53,6 +78,18 @@ class Network:
|
|||||||
if 200 <= req.status_code < 300:
|
if 200 <= req.status_code < 300:
|
||||||
logger.debug(f'{method} put {req.status_code}')
|
logger.debug(f'{method} put {req.status_code}')
|
||||||
return req
|
return req
|
||||||
|
else:
|
||||||
|
|
||||||
|
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:
|
else:
|
||||||
error_text = str(req.text)
|
error_text = str(req.text)
|
||||||
logger.error(f'{method} put {req.status_code} {error_text}')
|
logger.error(f'{method} put {req.status_code} {error_text}')
|
||||||
@ -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,14 +187,13 @@ class Network:
|
|||||||
tracks += resp['items']
|
tracks += resp['items']
|
||||||
else:
|
else:
|
||||||
logger.warning(f'{playlistid} no items returned')
|
logger.warning(f'{playlistid} no items returned')
|
||||||
else:
|
|
||||||
logger.warning(f'{playlistid} error on response')
|
|
||||||
|
|
||||||
if resp.get('next', None):
|
if resp.get('next', None):
|
||||||
|
|
||||||
more_tracks = self.get_playlist_tracks(playlistid, offset + limit)
|
more_tracks = self.get_playlist_tracks(playlistid, offset + limit)
|
||||||
if more_tracks:
|
if more_tracks:
|
||||||
tracks += more_tracks
|
tracks += more_tracks
|
||||||
|
else:
|
||||||
|
logger.warning(f'{playlistid} error on response')
|
||||||
|
|
||||||
return tracks
|
return tracks
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user