From 7472f41948cfb7e7b630c892592b5f92fc23aaa3 Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Wed, 30 Mar 2016 14:31:58 -0400 Subject: [PATCH] add recommendations endpoints --- setup.py | 2 +- spotipy/client.py | 46 +++++++++++++++++++++++++++++++++++++++++++++ tests/authtests2.py | 4 ++++ 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 779bb74..efcd94a 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup setup( name='spotipy', - version='2.3.7', + version='2.3.8', description='simple client for the Spotify Web API', author="@plamere", author_email="paul@echonest.com", diff --git a/spotipy/client.py b/spotipy/client.py index d0636d8..be9162b 100644 --- a/spotipy/client.py +++ b/spotipy/client.py @@ -651,6 +651,52 @@ class Spotify(object): return self._get('browse/categories/' + category_id + '/playlists', country=country, limit=limit, offset=offset) + def recommendations(self, seed_artists=[], seed_genres=[], seed_tracks=[], + limit = 20, country=None, **kwargs): + ''' Get a list of recommended tracks for one to five seeds. + + Parameters: + - seed_artists - a list of artist IDs, URIs or URLs + + - seed_tracks - a list of artist IDs, URIs or URLs + + - seed_genres - a list of genre names. Available genres for + recommendations can be found by calling recommendation_genre_seeds + + - country - An ISO 3166-1 alpha-2 country code. If provided, all + results will be playable in this country. + + - limit - The maximum number of items to return. Default: 20. + Minimum: 1. Maximum: 100 + + - min/max/target_ - For the tuneable track attributes listed + in the documentation, these values provide filters and targeting on + results. + ''' + params = dict(limit=limit) + if seed_artists: + params['seed_artists'] = [self._get_id('artist', a) for a in seed_artists] + if seed_genres: + params['seed_genres'] = seed_genres + if seed_tracks: + params['seed_tracks'] = [self._get_id('track', t) for t in seed_tracks] + if country: + params['market'] = country + + for attribute in ["acousticness", "danceability", "duration_ms", "energy", + "instrumentalness", "key", "liveness", "loudness", "mode", "popularity", + "speechiness", "tempo", "time_signature", "valence"]: + for prefix in ["min_", "max_", "target_"]: + param = prefix + attribute + if param in kwargs: + params[param] = kwargs[param] + return self._get('recommendations', **params) + + def recommendation_genre_seeds(self): + ''' Get a list of genres available for the recommendations function. + ''' + return self._get('recommendations/available-genre-seeds') + def audio_features(self, tracks=[]): ''' Get audio features for multiple tracks based upon their Spotify IDs Parameters: diff --git a/tests/authtests2.py b/tests/authtests2.py index f72e45f..8bf513f 100644 --- a/tests/authtests2.py +++ b/tests/authtests2.py @@ -53,6 +53,10 @@ class AuthTestSpotipy(unittest.TestCase): assert('speechiness' in track) self.assertTrue(results[-1] == None) + def test_recommendations(self): + results = spotify.recommendations(self.four_tracks, min_danceability=0, max_loudness=0, target_popularity=50) + self.assertTrue(len(results['tracks']) == 20) + if __name__ == '__main__': client_credentials_manager = SpotifyClientCredentials()