Added support for new_releases and featured_playlists endpoints.

This commit is contained in:
Paul Lamere 2014-10-25 06:40:51 -04:00
parent 06b54e4c1d
commit 373218083f
5 changed files with 118 additions and 1 deletions

View File

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

View File

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

View File

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

View File

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

View File

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