From 75df13d894d19b3dd0f4b7dbe2608cb4475bb817 Mon Sep 17 00:00:00 2001 From: Paul Lamere Date: Fri, 16 May 2014 07:56:59 -0400 Subject: [PATCH] Added examples, improved docs --- README.md | 3 ++ examples/show_artist_top_tracks.py | 16 +++++++ examples/show_user.py | 17 ++++++++ setup.py | 2 +- spotipy.py | 68 +++++++++++++++++++++++------- tests/tests.py | 24 +++++++++-- 6 files changed, 110 insertions(+), 20 deletions(-) create mode 100644 examples/show_artist_top_tracks.py create mode 100644 examples/show_user.py diff --git a/README.md b/README.md index 082ff62..3321fd4 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,8 @@ A full set of examples can be found in the [Spotipy examples directory](https:// - albums - gets info for a set of albums - artist - gets info for an artist - artists - gets info for a set of artists + - artist_albums - gets info about an artist's albums + - artist_top_tracks - gets info about an artist's top tracks - user - gets profile info for a user - search - searches for artists, albums or tracks @@ -68,4 +70,5 @@ at [paul@echonest.com](mailto:paul@echonest.com). Or just send me a pull request ## Version - 1.0 - 04/05/2014 - Initial release +- 1.1 - 05/16/2014 - Adapt to web API changes. Early auth support. diff --git a/examples/show_artist_top_tracks.py b/examples/show_artist_top_tracks.py new file mode 100644 index 0000000..e710d82 --- /dev/null +++ b/examples/show_artist_top_tracks.py @@ -0,0 +1,16 @@ +# shows artist info for a URN or URL + +import spotipy +import sys +import pprint + +if len(sys.argv) > 1: + urn = sys.argv[1] +else: + urn = 'spotify:artist:3jOstUTkEu2JkjvRdBA5Gu' + +sp = spotipy.Spotify() +response = sp.artist_top_tracks(urn) + +for track in response['tracks']: + print track['name'] diff --git a/examples/show_user.py b/examples/show_user.py new file mode 100644 index 0000000..07b102f --- /dev/null +++ b/examples/show_user.py @@ -0,0 +1,17 @@ + +# shows artist info for a URN or URL + +import spotipy +import sys +import pprint + +if len(sys.argv) > 1: + username = sys.argv[1] +else: + username = 'plamere' + +sp = spotipy.Spotify() +sp.trace = True +user = sp.user(username) +pprint.pprint(user) + diff --git a/setup.py b/setup.py index e45fd58..e41679c 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ from setuptools import setup setup( name='spotipy', - version='0.931', + version='1.100', description='simple client for the Spotify Web API', author="@plamere", author_email="paul@echonest.com", diff --git a/spotipy.py b/spotipy.py index d1127f0..f377a43 100644 --- a/spotipy.py +++ b/spotipy.py @@ -7,7 +7,6 @@ import requests ''' A simple and thin Python library for the Spotify Web API ''' - class SpotifyException(Exception): def __init__(self, http_status, code, msg): self.http_status = http_status @@ -20,28 +19,51 @@ class SpotifyException(Exception): class Spotify(object): - - auth = None + ''' + Example usage: + + import spotipy + + urn = 'spotify:artist:3jOstUTkEu2JkjvRdBA5Gu' + sp = spotipy.Spotify() + + sp.trace = True # turn on tracing + + artist = sp.artist(urn) + print(artist) + + user = sp.user('plamere') + print(user) + ''' + + trace = False + _auth = None def __init__(self, auth=None): self.prefix = 'https://api.spotify.com/v1/' - self.auth = auth + self._auth = auth - def auth_headers(self): - if self.auth: - return {'Authorization': 'Bearer {0}'.format(self.auth)} + def _auth_headers(self): + if self._auth: + return {'Authorization': 'Bearer {0}'.format(self._auth)} else: return None def _internal_call(self, verb, method, params): url = self.prefix + method args = dict(params=params) - headers = self.auth_headers() - print(headers) + headers = self._auth_headers() r = requests.request(verb, url, headers=headers, **args) + if self.trace: + print() + print(verb, r.url) if r.status_code != 200: raise SpotifyException(r.status_code, -1, u'the requested resource could not be found: ' + r.url) - return r.json() + results = r.json() + if self.trace: + print('RESP', results) + print() + return results def get(self, method, args=None, **kwargs): if args: @@ -72,11 +94,6 @@ class Spotify(object): trid = self._get_id('artist', artist_id) return self.get('artists/' + trid) - def artist_albums(self, artist_id, album_type=None, limit=20, offset=0): - ''' Get Spotify catalog information about an artist’s albums - ''' - trid = self._get_id('artist', artist_id) - return self.get('artists/' + trid + '/albums', album_type=album_type, limit=limit, offset=offset) def artists(self, artists): ''' returns a list of artists given the artist IDs, URNs, or URLs @@ -85,6 +102,20 @@ class Spotify(object): tlist = [self._get_id('artist', a) for a in artists] return self.get('artists/?ids=' + ','.join(tlist)) + def artist_albums(self, artist_id, album_type=None, limit=20, offset=0): + ''' Get Spotify catalog information about an artist’s albums + ''' + + trid = self._get_id('artist', artist_id) + return self.get('artists/' + trid + '/albums', album_type=album_type, limit=limit, offset=offset) + + def artist_top_tracks(self, artist_id, country='US'): + ''' Get Spotify catalog information about an artist’s top 10 tracks by country. + ''' + + trid = self._get_id('artist', artist_id) + return self.get('artists/' + trid + '/top-tracks', country=country) + def album(self, album_id): ''' returns a single album given the album's ID, URN or URL ''' @@ -92,6 +123,13 @@ class Spotify(object): trid = self._get_id('album', album_id) return self.get('albums/' + trid) + def album_tracks(self, album_id): + ''' Get Spotify catalog information about an album’s tracks + ''' + + trid = self._get_id('album', album_id) + return self.get('albums/' + trid + '/tracks/') + def albums(self, albums): ''' returns a list of albums given the album IDs, URNs, or URLs ''' diff --git a/tests/tests.py b/tests/tests.py index 0bc4e8e..1c6ba11 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -1,6 +1,7 @@ # -*- coding: latin-1 -*- import spotipy import unittest +import pprint class TestSpotipy(unittest.TestCase): @@ -32,6 +33,10 @@ class TestSpotipy(unittest.TestCase): album = self.spotify.album(self.pinkerton_urn) self.assertTrue(album['name'] == u'Pinkerton') + def test_album_tracks(self): + results = self.spotify.album_tracks(self.pinkerton_urn) + self.assertTrue(len(results['items']) == 10) + def test_albums(self): results = self.spotify.albums([self.pinkerton_urn, self.pablo_honey_urn]) self.assertTrue('albums' in results) @@ -54,6 +59,11 @@ class TestSpotipy(unittest.TestCase): self.assertTrue('tracks' in results) self.assertTrue(len(results['tracks']) == 2) + def test_artist_top_tracks(self): + results = self.spotify.artist_top_tracks(self.weezer_urn) + self.assertTrue('tracks' in results) + self.assertTrue(len(results['tracks']) == 10) + def test_artist_search(self): results = self.spotify.search(q='weezer', type='artist') self.assertTrue('artists' in results) @@ -62,9 +72,15 @@ class TestSpotipy(unittest.TestCase): def test_artist_albums(self): results = self.spotify.artist_albums(self.weezer_urn) - self.assertTrue('albums' in results) - self.assertTrue(len(results['albums']) > 0) - self.assertTrue(results['albums'][0]['artists'][0]['name'] == 'Weezer') + self.assertTrue('items' in results) + self.assertTrue(len(results['items']) > 0) + + found = False + for album in results['items']: + if album['name'] == 'Hurley': + found = True + + self.assertTrue(found) def test_album_search(self): results = self.spotify.search(q='weezer pinkerton', type='album') @@ -80,7 +96,7 @@ class TestSpotipy(unittest.TestCase): def test_user(self): user = self.spotify.user(user_id='plamere') - self.assertTrue(user['username'] == 'plamere') + self.assertTrue(user['uri'] == 'spotify:user:plamere') def test_track_bad_id(self): try: