From de9b99afc16addb8575ae0463292e1d64a963f18 Mon Sep 17 00:00:00 2001 From: Ryan Choi Date: Fri, 9 May 2014 00:34:08 -0700 Subject: [PATCH 1/2] spotipy --- examples/search.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 examples/search.py diff --git a/examples/search.py b/examples/search.py new file mode 100644 index 0000000..cd10aa8 --- /dev/null +++ b/examples/search.py @@ -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) From e98ff92425d83b17c54bfd7fab3c976120b0f73c Mon Sep 17 00:00:00 2001 From: Ryan Choi Date: Thu, 15 May 2014 19:51:42 -0700 Subject: [PATCH 2/2] auth-based actions --- spotipy.py | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/spotipy.py b/spotipy.py index 168dcc8..d1127f0 100644 --- a/spotipy.py +++ b/spotipy.py @@ -1,4 +1,7 @@ +# coding: utf-8 + from __future__ import print_function +import base64 import requests ''' A simple and thin Python library for the Spotify Web API @@ -17,13 +20,25 @@ class SpotifyException(Exception): class Spotify(object): - def __init__(self): + + 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) - 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: raise SpotifyException(r.status_code, -1, u'the requested resource could not be found: ' + r.url) return r.json() @@ -93,6 +108,21 @@ class Spotify(object): ''' 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(':')