mirror of
https://github.com/c0de-archive/spotipy.git
synced 2024-11-05 07:27:47 +00:00
Added examples, improved docs
This commit is contained in:
parent
981f69047a
commit
75df13d894
@ -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
|
- albums - gets info for a set of albums
|
||||||
- artist - gets info for an artist
|
- artist - gets info for an artist
|
||||||
- artists - gets info for a set of artists
|
- 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
|
- user - gets profile info for a user
|
||||||
- search - searches for artists, albums or tracks
|
- 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
|
## Version
|
||||||
|
|
||||||
- 1.0 - 04/05/2014 - Initial release
|
- 1.0 - 04/05/2014 - Initial release
|
||||||
|
- 1.1 - 05/16/2014 - Adapt to web API changes. Early auth support.
|
||||||
|
|
||||||
|
16
examples/show_artist_top_tracks.py
Normal file
16
examples/show_artist_top_tracks.py
Normal 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
17
examples/show_user.py
Normal 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)
|
||||||
|
|
2
setup.py
2
setup.py
@ -3,7 +3,7 @@ from setuptools import setup
|
|||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='spotipy',
|
name='spotipy',
|
||||||
version='0.931',
|
version='1.100',
|
||||||
description='simple client for the Spotify Web API',
|
description='simple client for the Spotify Web API',
|
||||||
author="@plamere",
|
author="@plamere",
|
||||||
author_email="paul@echonest.com",
|
author_email="paul@echonest.com",
|
||||||
|
66
spotipy.py
66
spotipy.py
@ -7,7 +7,6 @@ import requests
|
|||||||
''' A simple and thin Python library for the Spotify Web API
|
''' A simple and thin Python library for the Spotify Web API
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
class SpotifyException(Exception):
|
class SpotifyException(Exception):
|
||||||
def __init__(self, http_status, code, msg):
|
def __init__(self, http_status, code, msg):
|
||||||
self.http_status = http_status
|
self.http_status = http_status
|
||||||
@ -20,28 +19,51 @@ class SpotifyException(Exception):
|
|||||||
|
|
||||||
|
|
||||||
class Spotify(object):
|
class Spotify(object):
|
||||||
|
'''
|
||||||
|
Example usage:
|
||||||
|
|
||||||
auth = None
|
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):
|
def __init__(self, auth=None):
|
||||||
self.prefix = 'https://api.spotify.com/v1/'
|
self.prefix = 'https://api.spotify.com/v1/'
|
||||||
self.auth = auth
|
self._auth = auth
|
||||||
|
|
||||||
def auth_headers(self):
|
def _auth_headers(self):
|
||||||
if self.auth:
|
if self._auth:
|
||||||
return {'Authorization': 'Bearer {0}'.format(self.auth)}
|
return {'Authorization': 'Bearer {0}'.format(self._auth)}
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def _internal_call(self, verb, method, params):
|
def _internal_call(self, verb, method, params):
|
||||||
url = self.prefix + method
|
url = self.prefix + method
|
||||||
args = dict(params=params)
|
args = dict(params=params)
|
||||||
headers = self.auth_headers()
|
headers = self._auth_headers()
|
||||||
print(headers)
|
|
||||||
r = requests.request(verb, url, headers=headers, **args)
|
r = requests.request(verb, url, headers=headers, **args)
|
||||||
|
if self.trace:
|
||||||
|
print()
|
||||||
|
print(verb, r.url)
|
||||||
if r.status_code != 200:
|
if r.status_code != 200:
|
||||||
raise SpotifyException(r.status_code, -1, u'the requested resource could not be found: ' + r.url)
|
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):
|
def get(self, method, args=None, **kwargs):
|
||||||
if args:
|
if args:
|
||||||
@ -72,11 +94,6 @@ class Spotify(object):
|
|||||||
trid = self._get_id('artist', artist_id)
|
trid = self._get_id('artist', artist_id)
|
||||||
return self.get('artists/' + trid)
|
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):
|
def artists(self, artists):
|
||||||
''' returns a list of artists given the artist IDs, URNs, or URLs
|
''' 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]
|
tlist = [self._get_id('artist', a) for a in artists]
|
||||||
return self.get('artists/?ids=' + ','.join(tlist))
|
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):
|
def album(self, album_id):
|
||||||
''' returns a single album given the album's ID, URN or URL
|
''' 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)
|
trid = self._get_id('album', album_id)
|
||||||
return self.get('albums/' + trid)
|
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):
|
def albums(self, albums):
|
||||||
''' returns a list of albums given the album IDs, URNs, or URLs
|
''' returns a list of albums given the album IDs, URNs, or URLs
|
||||||
'''
|
'''
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
# -*- coding: latin-1 -*-
|
# -*- coding: latin-1 -*-
|
||||||
import spotipy
|
import spotipy
|
||||||
import unittest
|
import unittest
|
||||||
|
import pprint
|
||||||
|
|
||||||
|
|
||||||
class TestSpotipy(unittest.TestCase):
|
class TestSpotipy(unittest.TestCase):
|
||||||
@ -32,6 +33,10 @@ class TestSpotipy(unittest.TestCase):
|
|||||||
album = self.spotify.album(self.pinkerton_urn)
|
album = self.spotify.album(self.pinkerton_urn)
|
||||||
self.assertTrue(album['name'] == u'Pinkerton')
|
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):
|
def test_albums(self):
|
||||||
results = self.spotify.albums([self.pinkerton_urn, self.pablo_honey_urn])
|
results = self.spotify.albums([self.pinkerton_urn, self.pablo_honey_urn])
|
||||||
self.assertTrue('albums' in results)
|
self.assertTrue('albums' in results)
|
||||||
@ -54,6 +59,11 @@ class TestSpotipy(unittest.TestCase):
|
|||||||
self.assertTrue('tracks' in results)
|
self.assertTrue('tracks' in results)
|
||||||
self.assertTrue(len(results['tracks']) == 2)
|
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):
|
def test_artist_search(self):
|
||||||
results = self.spotify.search(q='weezer', type='artist')
|
results = self.spotify.search(q='weezer', type='artist')
|
||||||
self.assertTrue('artists' in results)
|
self.assertTrue('artists' in results)
|
||||||
@ -62,9 +72,15 @@ class TestSpotipy(unittest.TestCase):
|
|||||||
|
|
||||||
def test_artist_albums(self):
|
def test_artist_albums(self):
|
||||||
results = self.spotify.artist_albums(self.weezer_urn)
|
results = self.spotify.artist_albums(self.weezer_urn)
|
||||||
self.assertTrue('albums' in results)
|
self.assertTrue('items' in results)
|
||||||
self.assertTrue(len(results['albums']) > 0)
|
self.assertTrue(len(results['items']) > 0)
|
||||||
self.assertTrue(results['albums'][0]['artists'][0]['name'] == 'Weezer')
|
|
||||||
|
found = False
|
||||||
|
for album in results['items']:
|
||||||
|
if album['name'] == 'Hurley':
|
||||||
|
found = True
|
||||||
|
|
||||||
|
self.assertTrue(found)
|
||||||
|
|
||||||
def test_album_search(self):
|
def test_album_search(self):
|
||||||
results = self.spotify.search(q='weezer pinkerton', type='album')
|
results = self.spotify.search(q='weezer pinkerton', type='album')
|
||||||
@ -80,7 +96,7 @@ class TestSpotipy(unittest.TestCase):
|
|||||||
|
|
||||||
def test_user(self):
|
def test_user(self):
|
||||||
user = self.spotify.user(user_id='plamere')
|
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):
|
def test_track_bad_id(self):
|
||||||
try:
|
try:
|
||||||
|
Loading…
Reference in New Issue
Block a user