From 9a48746b790ea122a383cf7c47a64d2292741caa Mon Sep 17 00:00:00 2001 From: Leon Bohmann Date: Tue, 30 Nov 2021 21:41:48 +0100 Subject: [PATCH] added exception handling and retry-logic to zspotify --- zspotify/zspotify.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/zspotify/zspotify.py b/zspotify/zspotify.py index f398d5d..868e9e2 100644 --- a/zspotify/zspotify.py +++ b/zspotify/zspotify.py @@ -9,7 +9,7 @@ It's like youtube-dl, but for Spotify. import os import os.path from getpass import getpass - +import time import requests from librespot.audio.decoders import VorbisOnlyAudioQuality from librespot.core import Session @@ -19,7 +19,6 @@ from const import TYPE, \ PLAYLIST_READ_PRIVATE, USER_LIBRARY_READ from config import Config - class ZSpotify: SESSION: Session = None DOWNLOAD_QUALITY = None @@ -82,10 +81,19 @@ class ZSpotify: return requests.get(url, headers=headers, params=params).json() @classmethod - def invoke_url(cls, url): + def invoke_url(cls, url, tryCount = 0): headers = cls.get_auth_header() response = requests.get(url, headers=headers) - return response.text, response.json() + responseText = response.text + responseJson = response.json() + + if 'error' in responseJson and tryCount < 20: + + print(f"Spotify API Error ({responseJson['error']['status']}): {responseJson['error']['message']}") + time.sleep(5) + return cls.invoke_url(url, tryCount + 1) + + return responseText, responseJson @classmethod def check_premium(cls) -> bool: