add recommendations endpoints

This commit is contained in:
Matt Kane 2016-03-30 14:31:58 -04:00
parent dd021c4087
commit 7472f41948
3 changed files with 51 additions and 1 deletions

View File

@ -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",

View File

@ -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_<attribute> - 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:

View File

@ -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()