def get_track_list(search_term: str, criterion: str, display: int = 10): """ Allows the user to search for tracks by artist or title, then prints and returns the result :param search_term: user input used to query spotify :param criterion: determines whether to query by artist or track title :param display: number of tracks to display in results (default 10) :return: data frame of track information and their corresponding IDs """ # Query spotify by artist if such criterion is given, otherwise by track title if criterion == 'artist': search_result = spotify.get_top_tracks_by_artist(search_term) else: search_result = spotify.get_tracks(search_term) # Construct a data frame of track titles and their albums with query result track_data = pd.DataFrame( data={ 'Selection Number': list(range(1, len(search_result) + 1)), 'Song Title': [item['name'] for item in search_result], 'Album': [item['album']['name'] for item in search_result] } ).set_index('Selection Number') # Create a list of track IDs from the query result track_ids = [item['id'] for item in search_result] # If the track query is not empty, print the data and return the data and ID list if not track_data.empty: print('\nWe found the following tracks...\n') print_menu(track_data.head(display)) return track_data, track_ids # Otherwise, return None else: print('\nNo tracks found!') return None, None
from helpers import spotify import helpers import pprint import json def write_to_file(results): file_path = helpers.get_file_path('spotify_tracks.json', subdirectory='results') f = open(file_path, 'w') f.write(json.dumps(results)) f.close() urls = [] search_term = 'Beyonce' data = spotify.get_tracks(search_term) write_to_file(data) # pprint.pprint(data, depth=7) ''' Challenge: using the same strategy as in file 14, how do I print all of the album cover images? Hint: Here is what the data structure looks like: https://codebeautify.org/jsonviewer/cbd57f4e You can also view the results in the results/spotify_tracks.json file. '''