def test_get_similar_tracks_simplify_default(self): url = 'https://api.spotify.com/v1/recommendations?seed_artists=6vWDO969PvNqNYHIOW5v0m&seed_tracks=4JehYebiI9JE8sR8MisGVb&seed_genres=pop,rock,indie' data = {'tracks': {'items': [{}] }} #spoof _issue_get_request and _simplify_tracks spotify._issue_get_request = MagicMock(return_value=data) spotify._simplify_tracks = MagicMock() # call function: spotify.get_similar_tracks( artist_ids=[self.artist_id], track_ids=[self.track_id], genres=self.genres ) # check that spoofed functions called with correct data: spotify._issue_get_request.assert_called_with(url) spotify._simplify_tracks.assert_called_with(data['tracks'])
def fetch_and_handle_recommendation_data(artist_input, genre_input): print( '\nNow creating a brand new playlist of recommendations, just for you...' ) recommendations = spotify.get_similar_tracks(artist_ids=artist_input, genres=genre_input) print('\nHere it is! Take a look:') print_tracklist_table(recommendations) print('\nYou can now either:') print('1. Download this playlist as an HTML file to your computer') print('2. Email this playlist to yourself (with the file attached)') print('3. Email this playlist to a friend') print('4. Do nothing and return to the main menu') while True: tracklist_choice = input('\nWhat would you like to do? ') if tracklist_choice == '1': save_tracklist_as_file(recommendations) break elif tracklist_choice == '2': email_to_self(recommendations) break elif tracklist_choice == '3': email_to_others(recommendations) break elif tracklist_choice == '4': break else: print('\nThat\'s not a valid option. Please try again.')
def test_get_similar_tracks_validation(self): with self.assertRaises(Exception) as cm: spotify.get_similar_tracks() self.assertEqual( 'Either artist_ids or track_ids or genres required', str(cm.exception) ) with self.assertRaises(Exception) as cm: spotify.get_similar_tracks( artist_ids=[self.artist_id], track_ids=[self.track_id], genres=self.genres + ['punk', 'emo']) error = 'You can only have 5 "seed values" in your recommendations query.\n' + \ 'In other words, (len(artist_ids) + len(track_ids) + len(genres)) must be less than or equal to 5.' self.assertEqual( error, str(cm.exception) )
def get_recommendations(): artist_list = list(artist_dict.values()) track_list = list(track_dict.keys()) total_length = len(artist_list) + len(tracks) + len(display_genre) if total_length < 1 or total_length > 5: print( "Sorry, but number of preferred genres and artists must be between 1 and 5." ) print( "Please go back to the main menu and ensure that the amount of your preferences lies within this range" ) return print('Handle retrieving a list of recommendations here...') temp = spotify.get_similar_tracks(artist_list, track_list, display_genre) data = {} track_list = [] for item in temp['tracks']: new_track = { 'name': item['name'], 'artist_name': item['artists'][0]['name'], 'album_image_url_small': item['album']['images'][2]['url'], 'album_name': item['album']['name'], 'share_url': item['external_urls']['spotify'] } track_list.append(new_track) df = pd.DataFrame(track_list) print(df[['name', 'artist_name', 'share_url']]) html_content = spotify.get_formatted_tracklist_table_html(track_list) #generate html file html_file = open('recommendedtracks.html', 'w') html_file.write(html_content) html_file.close() send_email = input('Would you like to email this list to a friend (y/n)?') while True: if send_email == 'y' or send_email == 'Y': from_email = input('What is your email?') to_emails = input('What is the email of your friend?') subject = "Playlist Recommendation (from Spotify)" html_content = input( 'What would you like to say to them about this playlist?' ) + html_content sendgrid.send_mail(from_email, to_emails, subject, html_content) break else: print("Canceling...") break
from apis import spotify, utilities tracks = spotify.get_similar_tracks(genres=['pop', 'rock'], simplify=True) # print(tracks) df = utilities.get_dataframe(tracks) print(df)