diff --git a/zspotify/album.py b/zspotify/album.py index 8aea119..901844a 100644 --- a/zspotify/album.py +++ b/zspotify/album.py @@ -26,18 +26,18 @@ def get_album_tracks(album_id): def get_album_name(album_id): """ 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]) def get_artist_albums(artist_id): """ 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 album_ids = [resp[ITEMS][i][ID] for i in range(len(resp[ITEMS]))] # Recursive requests to get all albums including singles an EPs 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]))]) return album_ids diff --git a/zspotify/playlist.py b/zspotify/playlist.py index 36cac39..03e489a 100644 --- a/zspotify/playlist.py +++ b/zspotify/playlist.py @@ -42,7 +42,7 @@ def get_playlist_songs(playlist_id): def get_playlist_info(playlist_id): """ 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() diff --git a/zspotify/podcast.py b/zspotify/podcast.py index 88cd431..032c9b7 100644 --- a/zspotify/podcast.py +++ b/zspotify/podcast.py @@ -14,7 +14,7 @@ SHOWS_URL = 'https://api.spotify.com/v1/shows' 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: return None, None return fix_filename(info[SHOW][NAME]), fix_filename(info[NAME]) diff --git a/zspotify/track.py b/zspotify/track.py index fb91b10..78106d1 100644 --- a/zspotify/track.py +++ b/zspotify/track.py @@ -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]: """ 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 = [] - for data in info[TRACKS][0][ARTISTS]: - artists.append(data[NAME]) - album_name = info[TRACKS][0][ALBUM][NAME] - name = info[TRACKS][0][NAME] - image_url = info[TRACKS][0][ALBUM][IMAGES][0][URL] - release_year = info[TRACKS][0][ALBUM][RELEASE_DATE].split('-')[0] - 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] + if not TRACKS in info: + raise ValueError(f'Invalid response from TRACKS_URL:\n{raw}') + + try: + artists = [] + for data in info[TRACKS][0][ARTISTS]: + artists.append(data[NAME]) + album_name = info[TRACKS][0][ALBUM][NAME] + name = info[TRACKS][0][NAME] + image_url = info[TRACKS][0][ALBUM][IMAGES][0][URL] + release_year = info[TRACKS][0][ALBUM][RELEASE_DATE].split('-')[0] + 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: """ 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 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: Printer.print(PrintChannel.ERRORS, '### SKIPPING SONG - FAILED TO QUERY METADATA ###') Printer.print(PrintChannel.ERRORS, str(e) + "\n") + Printer.print(PrintChannel.ERRORS, "".join(traceback.TracebackException.from_exception(e).format()) + "\n") else: try: if not is_playable: diff --git a/zspotify/zspotify.py b/zspotify/zspotify.py index 4e91842..caad7c5 100644 --- a/zspotify/zspotify.py +++ b/zspotify/zspotify.py @@ -86,7 +86,8 @@ class ZSpotify: @classmethod def invoke_url(cls, url): headers = cls.get_auth_header() - return requests.get(url, headers=headers).json() + response = requests.get(url, headers=headers) + return response.text, response.json() @classmethod def check_premium(cls) -> bool: