From 373218083fef144d9e9137aac024f9a18fcbf13b Mon Sep 17 00:00:00 2001 From: Paul Lamere Date: Sat, 25 Oct 2014 06:40:51 -0400 Subject: [PATCH] Added support for new_releases and featured_playlists endpoints. --- examples/show_featured_playlists.py | 33 ++++++++++++++++++++++ examples/show_new_releases.py | 32 +++++++++++++++++++++ setup.py | 2 +- spotipy/client.py | 43 +++++++++++++++++++++++++++++ tests/authtests.py | 9 ++++++ 5 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 examples/show_featured_playlists.py create mode 100644 examples/show_new_releases.py diff --git a/examples/show_featured_playlists.py b/examples/show_featured_playlists.py new file mode 100644 index 0000000..823f8df --- /dev/null +++ b/examples/show_featured_playlists.py @@ -0,0 +1,33 @@ +# shows artist info for a URN or URL + +import spotipy +import sys +import pprint +import spotipy.util as util + +if len(sys.argv) > 1: + username = sys.argv[1] +else: + print "Whoops, need your username!" + print "usage: python featured_playlists.py [username]" + sys.exit() + +token = util.prompt_for_user_token(username) + +if token: + sp = spotipy.Spotify(auth=token) + + response = sp.featured_playlists() + print response['message'] + + while response: + playlists = response['playlists'] + for i, item in enumerate(playlists['items']): + print playlists['offset'] + i, item['name'] + + if playlists['next']: + response = sp.next(playlists) + else: + response = None +else: + print "Can't get token for", username diff --git a/examples/show_new_releases.py b/examples/show_new_releases.py new file mode 100644 index 0000000..5485435 --- /dev/null +++ b/examples/show_new_releases.py @@ -0,0 +1,32 @@ +# shows artist info for a URN or URL + +import spotipy +import sys +import pprint +import spotipy.util as util + +if len(sys.argv) > 1: + username = sys.argv[1] +else: + print "Whoops, need your username!" + print "usage: python new_releases.py [username]" + sys.exit() + +token = util.prompt_for_user_token(username) + +if token: + sp = spotipy.Spotify(auth=token) + + response = sp.new_releases() + + while response: + albums = response['albums'] + for i, item in enumerate(albums['items']): + print albums['offset'] + i,item['name'] + + if albums['next']: + response = sp.next(albums) + else: + response = None +else: + print "Can't get token for", username diff --git a/setup.py b/setup.py index 62363b4..6384f01 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup setup( name='spotipy', - version='2.0.2', + version='2.0.3', 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 61285c7..6a53d4e 100644 --- a/spotipy/client.py +++ b/spotipy/client.py @@ -420,6 +420,49 @@ class Spotify(object): tlist = [self._get_id('track', t) for t in tracks] return self._put('me/tracks/?ids=' + ','.join(tlist)) + + def featured_playlists(self, locale=None, country=None, + timestamp=None, limit=20, offset = 0): + ''' Get a list of Spotify featured playlists + + Parameters: + - locale - The desired language, consisting of a lowercase ISO + 639 language code and an uppercase ISO 3166-1 alpha-2 country + code, joined by an underscore. + + - country - An ISO 3166-1 alpha-2 country code. + + - timestamp - A timestamp in ISO 8601 format: + yyyy-MM-ddTHH:mm:ss. Use this parameter to specify the user's + local time to get results tailored for that specific date and + time in the day + + - limit - The maximum number of items to return. Default: 20. + Minimum: 1. Maximum: 50 + + - offset - The index of the first item to return. Default: 0 + (the first object). Use with limit to get the next set of + items. + ''' + return self._get('browse/featured-playlists', locale=locale, + country=country, timestamp=timestamp, limit=limit, offset=offset) + + def new_releases(self, country=None, limit=20, offset = 0): + ''' Get a list of new album releases featured in Spotify + + Parameters: + - country - An ISO 3166-1 alpha-2 country code. + + - limit - The maximum number of items to return. Default: 20. + Minimum: 1. Maximum: 50 + + - offset - The index of the first item to return. Default: 0 + (the first object). Use with limit to get the next set of + items. + ''' + return self._get('browse/new-releases', country=country, + limit=limit, offset=offset) + def _get_id(self, type, id): fields = id.split(':') if len(fields) >= 3: diff --git a/tests/authtests.py b/tests/authtests.py index 8a9a74a..046f0db 100644 --- a/tests/authtests.py +++ b/tests/authtests.py @@ -86,6 +86,15 @@ class AuthTestSpotipy(unittest.TestCase): new_total = tracks['total'] self.assertTrue(new_total == total) + + def test_new_releases(self): + response = spotify.new_releases() + self.assertTrue(len(response['albums']) > 0) + + def test_featured_releases(self): + response = spotify.featured_playlists() + self.assertTrue(len(response['playlists']) > 0) + def get_or_create_spotify_playlist(self, username, playlist_name): playlists = spotify.user_playlists(username) while playlists: