Repackaged for saner imports

This commit is contained in:
Paul Lamere
2014-05-18 07:08:51 -04:00
parent a48feaab66
commit 8dcaf7dba6
5 changed files with 21 additions and 15 deletions

180
spotipy/__init__.py Normal file
View File

@@ -0,0 +1,180 @@
# coding: utf-8
from __future__ import print_function
import base64
import requests
__all__ = ['oauth2']
''' 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
self.code = code
self.msg = msg
def __str__(self):
return u'http status: {0}, code:{1} - {2}'.format(
self.http_status, self.code, self.msg)
class Spotify(object):
'''
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
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()
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)
results = r.json()
if self.trace:
print('RESP', results)
print()
return results
def get(self, method, args=None, **kwargs):
if args:
kwargs.update(args)
return self._internal_call('GET', method, kwargs)
def _warn(self, msg):
print('warning:' + msg, file=sys.stderr)
def track(self, track_id):
''' returns a single track given the track's ID, URN or URL
'''
trid = self._get_id('track', track_id)
return self.get('tracks/' + trid)
def tracks(self, tracks):
''' returns a list of tracks given the track IDs, URNs, or URLs
'''
tlist = [self._get_id('track', t) for t in tracks]
return self.get('tracks/?ids=' + ','.join(tlist))
def artist(self, artist_id):
''' returns a single artist given the artist's ID, URN or URL
'''
trid = self._get_id('artist', artist_id)
return self.get('artists/' + trid)
def artists(self, artists):
''' returns a list of artists given the artist IDs, URNs, or URLs
'''
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
'''
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
'''
tlist = [self._get_id('album', a) for a in albums]
return self.get('albums/?ids=' + ','.join(tlist))
def search(self, q, limit=10, offset=0, type='track'):
''' searches for an item
'''
return self.get('search', q=q, limit=limit, offset=offset, type=type)
def user(self, user_id):
''' Gets basic profile information about a Spotify User
'''
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):
fields = id.split(':')
if len(fields) == 3:
if type != fields[1]:
self._warn('expected id of type ' + type + ' but found type ' + fields[2] + " " + id)
return fields[2]
fields = id.split('/')
if len(fields) >= 3:
itype = fields[-2]
if type != itype:
self._warn('expected id of type ' + type + ' but found type ' + itype + " " + id)
return fields[-1]
return id

57
spotipy/oauth2.py Normal file
View File

@@ -0,0 +1,57 @@
import base64
import urllib
import requests
print ('oauth2 is here')
class SpotifyOauthError(Exception):
pass
class SpotifyOAuth(object):
'''
Implements Authorization Code Flow for Spotify's OAuth implementation.
Docs: https://developer.spotify.com/spotify-web-api/authorization-guide/#authorization_code_flow
'''
OAUTH_AUTHORIZE_URL = 'https://accounts.spotify.com/authorize'
OAUTH_TOKEN_URL = 'https://accounts.spotify.com/api/token'
def __init__(self, client_id, client_secret, redirect_uri, state=None, scope=None):
self.client_id = client_id
self.client_secret = client_secret
self.redirect_uri = redirect_uri
self.state=state
self.scope=scope
def get_authorize_url(self):
payload = {'client_id': self.client_id,
'response_type': 'code',
'redirect_uri': self.redirect_uri}
if self.scope:
payload['scope'] = self.scope
if self.state:
payload['state'] = self.state
urlparams = urllib.urlencode(payload)
return "%s?%s" % (self.OAUTH_AUTHORIZE_URL, urlparams)
def parse_response_code(self, response):
return response.split("?code=")[1].split("&")[0]
def get_access_token(self, code):
payload = {'redirect_uri': self.redirect_uri,
'code': code,
'grant_type': 'authorization_code'}
if self.scope:
payload['scope'] = self.scope
if self.state:
payload['state'] = self.state
auth_header = base64.b64encode(self.client_id + ':' + self.client_secret)
headers = {'Authorization': 'Basic %s' % auth_header}
response = requests.post(self.OAUTH_TOKEN_URL, data=payload, headers=headers, verify=True)
if response.status_code is not 200:
raise SpotifyOauthError(response.reason)
return response.json()['access_token']