From e20483084f91b71e8d0cf7f2427c7b8a369f3723 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Wed, 15 Dec 2021 15:15:16 +0100 Subject: [PATCH] Print errors to stderr --- zspotify/config.py | 2 +- zspotify/termoutput.py | 9 ++++++++- zspotify/zspotify.py | 12 +++++++----- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/zspotify/config.py b/zspotify/config.py index dd82564..9aee422 100644 --- a/zspotify/config.py +++ b/zspotify/config.py @@ -158,7 +158,7 @@ class Config: return cls.get(SPLIT_ALBUM_DISCS) @classmethod - def get_chunk_size(cls) -> int(): + def get_chunk_size(cls) -> int: return cls.get(CHUNK_SIZE) @classmethod diff --git a/zspotify/termoutput.py b/zspotify/termoutput.py index be46356..ea14844 100644 --- a/zspotify/termoutput.py +++ b/zspotify/termoutput.py @@ -1,3 +1,4 @@ +import sys from enum import Enum from tqdm import tqdm @@ -16,11 +17,17 @@ class PrintChannel(Enum): PROGRESS_INFO = PRINT_PROGRESS_INFO +ERROR_CHANNEL = [PrintChannel.ERRORS, PrintChannel.API_ERRORS] + + class Printer: @staticmethod def print(channel: PrintChannel, msg: str) -> None: if ZSpotify.CONFIG.get(channel.value): - print(msg) + if channel in ERROR_CHANNEL: + print(msg, file=sys.stderr) + else: + print(msg) @staticmethod def print_loader(channel: PrintChannel, msg: str) -> None: diff --git a/zspotify/zspotify.py b/zspotify/zspotify.py index 197cfd9..977c4b0 100644 --- a/zspotify/zspotify.py +++ b/zspotify/zspotify.py @@ -89,12 +89,14 @@ class ZSpotify: responsetext = response.text responsejson = response.json() - if 'error' in responsejson and tryCount < 5: - + if 'error' in responsejson: + if tryCount < 5: + Printer.print(PrintChannel.WARNINGS, f"Spotify API Error (try {tryCount}) ({responsejson['error']['status']}): {responsejson['error']['message']}") + time.sleep(5) + return cls.invoke_url(url, tryCount + 1) + Printer.print(PrintChannel.API_ERRORS, f"Spotify API Error ({responsejson['error']['status']}): {responsejson['error']['message']}") - time.sleep(5) - return cls.invoke_url(url, tryCount + 1) - + return responsetext, responsejson @classmethod