From fb1dae5400c5311e692ce6aa018f2f624185e550 Mon Sep 17 00:00:00 2001 From: logykk Date: Fri, 25 Feb 2022 13:40:28 +1300 Subject: [PATCH] fix json fetching --- zotify/track.py | 7 +++++-- zotify/zotify.py | 7 ++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/zotify/track.py b/zotify/track.py index 57a539c..777b9cb 100644 --- a/zotify/track.py +++ b/zotify/track.py @@ -92,7 +92,10 @@ def get_song_lyrics(song_id: str, file_save: str) -> None: raw, lyrics = Zotify.invoke_url(f'https://spclient.wg.spotify.com/color-lyrics/v2/track/{song_id}') if lyrics: - formatted_lyrics = lyrics['lyrics']['lines'] + try: + formatted_lyrics = lyrics['lyrics']['lines'] + except KeyError: + raise ValueError(f'Failed to fetch lyrics: {song_id}') if(lyrics['lyrics']['syncType'] == "UNSYNCED"): with open(file_save, 'w') as file: for line in formatted_lyrics: @@ -107,7 +110,7 @@ def get_song_lyrics(song_id: str, file_save: str) -> None: ts_millis = str(math.floor(timestamp % 1000))[:2].zfill(2) file.writelines(f'[{ts_minutes}:{ts_seconds}.{ts_millis}]' + line['words'] + '\n') return - raise ValueError(f'Filed to fetch lyrics: {song_id}') + raise ValueError(f'Failed to fetch lyrics: {song_id}') def get_song_duration(song_id: str) -> float: diff --git a/zotify/zotify.py b/zotify/zotify.py index 5a304e8..27fc5d0 100644 --- a/zotify/zotify.py +++ b/zotify/zotify.py @@ -1,3 +1,4 @@ +import json from pathlib import Path from pwinput import pwinput import time @@ -88,10 +89,10 @@ class Zotify: responsetext = response.text try: responsejson = response.json() - except requests.exceptions.JSONDecodeError: - responsejson = {} + except json.decoder.JSONDecodeError: + responsejson = {"error": {"status": "unknown", "message": "received an empty response"}} - if 'error' in responsejson: + if not responsejson or 'error' in responsejson: if tryCount < (cls.CONFIG.get_retry_attempts() - 1): Printer.print(PrintChannel.WARNINGS, f"Spotify API Error (try {tryCount + 1}) ({responsejson['error']['status']}): {responsejson['error']['message']}") time.sleep(5)