Added examples, improved docs

This commit is contained in:
Paul Lamere 2014-05-16 07:56:59 -04:00
parent 981f69047a
commit 75df13d894
6 changed files with 110 additions and 20 deletions

View File

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

View File

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

17
examples/show_user.py Normal file
View File

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

View File

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

View File

@ -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 artists 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 artists 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 artists 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 albums 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
'''

View File

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