Merge pull request #2 from ryankicks/master

spotipy
This commit is contained in:
Paul Lamere 2014-05-16 05:46:16 -04:00
commit 981f69047a
2 changed files with 46 additions and 2 deletions

14
examples/search.py Normal file
View File

@ -0,0 +1,14 @@
# shows artist info for a URN or URL
import spotipy
import sys
import pprint
if len(sys.argv) > 1:
str = sys.argv[1]
else:
str = 'Radiohead'
sp = spotipy.Spotify()
result = sp.search(str)
pprint.pprint(result)

View File

@ -1,4 +1,7 @@
# coding: utf-8
from __future__ import print_function from __future__ import print_function
import base64
import requests import requests
''' A simple and thin Python library for the Spotify Web API ''' A simple and thin Python library for the Spotify Web API
@ -17,13 +20,25 @@ class SpotifyException(Exception):
class Spotify(object): class Spotify(object):
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
def auth_headers(self):
if self.auth:
return {'Authorization': 'Bearer {0}'.format(self.auth)}
else:
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)
r = requests.request(verb, url, **args) headers = self.auth_headers()
print(headers)
r = requests.request(verb, url, headers=headers, **args)
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() return r.json()
@ -94,6 +109,21 @@ class Spotify(object):
''' '''
return self.get('users/' + user_id) return self.get('users/' + user_id)
def user_playlists(self, user):
''' Gets playlists of a user
'''
return self.get("users/%s/playlists" % user)
def user_playlist(self, user, playlist_id):
''' Gets playlist of a user
'''
return self.get("users/%s/playlists/%s" % (user, playlist_id))
def me(self):
''' returns info about me
'''
return self.get('me/')
def _get_id(self, type, id): def _get_id(self, type, id):
fields = id.split(':') fields = id.split(':')
if len(fields) == 3: if len(fields) == 3: