Better error output if get_song_info fails

This commit is contained in:
Mike Schwörer 2021-11-25 10:25:25 +01:00
parent cb42b4a878
commit 41c5ea79c6
No known key found for this signature in database
GPG Key ID: D3C7172E0A70F8CF
5 changed files with 30 additions and 21 deletions

View File

@ -26,18 +26,18 @@ def get_album_tracks(album_id):
def get_album_name(album_id): def get_album_name(album_id):
""" Returns album name """ """ Returns album name """
resp = ZSpotify.invoke_url(f'{ALBUM_URL}/{album_id}') (raw, resp) = ZSpotify.invoke_url(f'{ALBUM_URL}/{album_id}')
return resp[ARTISTS][0][NAME], fix_filename(resp[NAME]) return resp[ARTISTS][0][NAME], fix_filename(resp[NAME])
def get_artist_albums(artist_id): def get_artist_albums(artist_id):
""" Returns artist's albums """ """ Returns artist's albums """
resp = ZSpotify.invoke_url(f'{ARTIST_URL}/{artist_id}/albums?include_groups=album%2Csingle') (raw, resp) = ZSpotify.invoke_url(f'{ARTIST_URL}/{artist_id}/albums?include_groups=album%2Csingle')
# Return a list each album's id # Return a list each album's id
album_ids = [resp[ITEMS][i][ID] for i in range(len(resp[ITEMS]))] album_ids = [resp[ITEMS][i][ID] for i in range(len(resp[ITEMS]))]
# Recursive requests to get all albums including singles an EPs # Recursive requests to get all albums including singles an EPs
while resp['next'] is not None: while resp['next'] is not None:
resp = ZSpotify.invoke_url(resp['next']) (raw, resp) = ZSpotify.invoke_url(resp['next'])
album_ids.extend([resp[ITEMS][i][ID] for i in range(len(resp[ITEMS]))]) album_ids.extend([resp[ITEMS][i][ID] for i in range(len(resp[ITEMS]))])
return album_ids return album_ids

View File

@ -42,7 +42,7 @@ def get_playlist_songs(playlist_id):
def get_playlist_info(playlist_id): def get_playlist_info(playlist_id):
""" Returns information scraped from playlist """ """ Returns information scraped from playlist """
resp = ZSpotify.invoke_url(f'{PLAYLISTS_URL}/{playlist_id}?fields=name,owner(display_name)&market=from_token') (raw, resp) = ZSpotify.invoke_url(f'{PLAYLISTS_URL}/{playlist_id}?fields=name,owner(display_name)&market=from_token')
return resp['name'].strip(), resp['owner']['display_name'].strip() return resp['name'].strip(), resp['owner']['display_name'].strip()

View File

@ -14,7 +14,7 @@ SHOWS_URL = 'https://api.spotify.com/v1/shows'
def get_episode_info(episode_id_str) -> Tuple[Optional[str], Optional[str]]: def get_episode_info(episode_id_str) -> Tuple[Optional[str], Optional[str]]:
info = ZSpotify.invoke_url(f'{EPISODE_INFO_URL}/{episode_id_str}') (raw, info) = ZSpotify.invoke_url(f'{EPISODE_INFO_URL}/{episode_id_str}')
if ERROR in info: if ERROR in info:
return None, None return None, None
return fix_filename(info[SHOW][NAME]), fix_filename(info[NAME]) return fix_filename(info[SHOW][NAME]), fix_filename(info[NAME])

View File

@ -35,27 +35,34 @@ def get_saved_tracks() -> list:
def get_song_info(song_id) -> Tuple[List[str], str, str, Any, Any, Any, Any, Any, Any, int]: def get_song_info(song_id) -> Tuple[List[str], str, str, Any, Any, Any, Any, Any, Any, int]:
""" Retrieves metadata for downloaded songs """ """ Retrieves metadata for downloaded songs """
info = ZSpotify.invoke_url(f'{TRACKS_URL}?ids={song_id}&market=from_token') (raw, info) = ZSpotify.invoke_url(f'{TRACKS_URL}?ids={song_id}&market=from_token')
artists = [] if not TRACKS in info:
for data in info[TRACKS][0][ARTISTS]: raise ValueError(f'Invalid response from TRACKS_URL:\n{raw}')
artists.append(data[NAME])
album_name = info[TRACKS][0][ALBUM][NAME] try:
name = info[TRACKS][0][NAME] artists = []
image_url = info[TRACKS][0][ALBUM][IMAGES][0][URL] for data in info[TRACKS][0][ARTISTS]:
release_year = info[TRACKS][0][ALBUM][RELEASE_DATE].split('-')[0] artists.append(data[NAME])
disc_number = info[TRACKS][0][DISC_NUMBER] album_name = info[TRACKS][0][ALBUM][NAME]
track_number = info[TRACKS][0][TRACK_NUMBER] name = info[TRACKS][0][NAME]
scraped_song_id = info[TRACKS][0][ID] image_url = info[TRACKS][0][ALBUM][IMAGES][0][URL]
is_playable = info[TRACKS][0][IS_PLAYABLE] release_year = info[TRACKS][0][ALBUM][RELEASE_DATE].split('-')[0]
duration_ms = info[TRACKS][0][DURATION_MS] disc_number = info[TRACKS][0][DISC_NUMBER]
track_number = info[TRACKS][0][TRACK_NUMBER]
scraped_song_id = info[TRACKS][0][ID]
is_playable = info[TRACKS][0][IS_PLAYABLE]
duration_ms = info[TRACKS][0][DURATION_MS]
return artists, album_name, name, image_url, release_year, disc_number, track_number, scraped_song_id, is_playable, duration_ms
except Exception as e:
raise ValueError(f'Failed to parse TRACKS_URL response: {str(e)}\n{raw}')
return artists, album_name, name, image_url, release_year, disc_number, track_number, scraped_song_id, is_playable, duration_ms
def get_song_duration(song_id: str) -> float: def get_song_duration(song_id: str) -> float:
""" Retrieves duration of song in second as is on spotify """ """ Retrieves duration of song in second as is on spotify """
resp = ZSpotify.invoke_url(f'{TRACK_STATS_URL}{song_id}') (raw, resp) = ZSpotify.invoke_url(f'{TRACK_STATS_URL}{song_id}')
# get duration in miliseconds # get duration in miliseconds
ms_duration = resp['duration_ms'] ms_duration = resp['duration_ms']
@ -113,6 +120,7 @@ def download_track(mode: str, track_id: str, extra_keys={}, disable_progressbar=
except Exception as e: except Exception as e:
Printer.print(PrintChannel.ERRORS, '### SKIPPING SONG - FAILED TO QUERY METADATA ###') Printer.print(PrintChannel.ERRORS, '### SKIPPING SONG - FAILED TO QUERY METADATA ###')
Printer.print(PrintChannel.ERRORS, str(e) + "\n") Printer.print(PrintChannel.ERRORS, str(e) + "\n")
Printer.print(PrintChannel.ERRORS, "".join(traceback.TracebackException.from_exception(e).format()) + "\n")
else: else:
try: try:
if not is_playable: if not is_playable:

View File

@ -86,7 +86,8 @@ class ZSpotify:
@classmethod @classmethod
def invoke_url(cls, url): def invoke_url(cls, url):
headers = cls.get_auth_header() headers = cls.get_auth_header()
return requests.get(url, headers=headers).json() response = requests.get(url, headers=headers)
return response.text, response.json()
@classmethod @classmethod
def check_premium(cls) -> bool: def check_premium(cls) -> bool: