mirror of
				https://github.com/THIS-IS-NOT-A-BACKUP/zspotify.git
				synced 2025-11-04 05:20:34 +00:00 
			
		
		
		
	Merge pull request #207 from Mikescher/master-pr-008
Better error output if get_song_info fails
This commit is contained in:
		
						commit
						642f37ce9d
					
				@ -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
 | 
				
			||||||
 | 
				
			|||||||
@ -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()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -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])
 | 
				
			||||||
 | 
				
			|||||||
@ -35,8 +35,12 @@ 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')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if not TRACKS in info:
 | 
				
			||||||
 | 
					        raise ValueError(f'Invalid response from TRACKS_URL:\n{raw}')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    try:
 | 
				
			||||||
        artists = []
 | 
					        artists = []
 | 
				
			||||||
        for data in info[TRACKS][0][ARTISTS]:
 | 
					        for data in info[TRACKS][0][ARTISTS]:
 | 
				
			||||||
            artists.append(data[NAME])
 | 
					            artists.append(data[NAME])
 | 
				
			||||||
@ -51,11 +55,14 @@ def get_song_info(song_id) -> Tuple[List[str], str, str, Any, Any, Any, Any, Any
 | 
				
			|||||||
        duration_ms = info[TRACKS][0][DURATION_MS]
 | 
					        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
 | 
					        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}')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
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:
 | 
				
			||||||
 | 
				
			|||||||
@ -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:
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user