fix json fetching

This commit is contained in:
logykk 2022-02-25 13:40:28 +13:00
parent fcb823a474
commit fb1dae5400
2 changed files with 9 additions and 5 deletions

View File

@ -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:
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:

View File

@ -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)