Documentation improvements

This commit is contained in:
Paul Lamere
2014-08-22 11:00:29 -04:00
parent 58e5206076
commit 20870ea3ed
7 changed files with 358 additions and 59 deletions

View File

@@ -22,7 +22,7 @@ class SpotifyException(Exception):
class Spotify(object):
'''
Example usage:
Example usage::
import spotipy
@@ -324,8 +324,7 @@ class Spotify(object):
Parameters:
- user - the id of the user
- playlist_id - the id of the playlist
- tracks - the list of track URIs, URLs or IDs to add
to the playlist
- tracks - the list of track ids to add to the playlist
'''
plid = self._get_id('playlist', playlist_id)
ftracks = [ self._get_uri('track', tid) for tid in tracks]
@@ -340,9 +339,10 @@ class Spotify(object):
Parameters:
- user - the id of the user
- playlist_id - the id of the playlist
- tracks - the list of track URIs, URLs or IDs to add
to the playlist
- tracks - the list of track ids to add to the playlist
'''
plid = self._get_id('playlist', playlist_id)
ftracks = [ self._get_uri('track', tid) for tid in tracks]
payload = { "tracks": [ {"uri": track} for track in ftracks] }
@@ -356,14 +356,11 @@ class Spotify(object):
Parameters:
- user - the id of the user
- playlist_id - the id of the playlist
- tracks - an array of objects containing Spotify URIs of the
tracks to remove with their current positions in
the playlist. For example:
[
{"uri": "spotify:track:4iV5W9uYEdYUVa79Axb7Rh", "positions": [2] },
{"uri": "spotify:track:1301WleyT98MSxVHPZCA6M", "positions": [7] }
]
- tracks - an array of objects containing Spotify URIs of the tracks to remove with their current positions in the playlist. For example:
[ { "uri":"4iV5W9uYEdYUVa79Axb7Rh", "positions":[2] },
{ "uri":"1301WleyT98MSxVHPZCA6M", "positions":[7] } ]
'''
plid = self._get_id('playlist', playlist_id)
ftracks = [ self._get_uri('track', tid) for tid in tracks]
payload = { "tracks": ftracks }

View File

@@ -37,7 +37,7 @@ class SpotifyOAuth(object):
self.redirect_uri = redirect_uri
self.state=state
self.cache_path = cache_path
self.scope=self.normalize_scope(scope)
self.scope=self._normalize_scope(scope)
def get_cached_token(self):
token_info = None
@@ -52,14 +52,14 @@ class SpotifyOAuth(object):
if 'scope' not in token_info or self.scope != token_info['scope']:
return None
if self.is_token_expired(token_info):
token_info = self.refresh_access_token(token_info['refresh_token'])
if self._is_token_expired(token_info):
token_info = self._refresh_access_token(token_info['refresh_token'])
except IOError:
pass
return token_info
def save_token_info(self, token_info):
def _save_token_info(self, token_info):
if self.cache_path:
try:
f = open(self.cache_path, 'w')
@@ -70,7 +70,7 @@ class SpotifyOAuth(object):
pass
def is_token_expired(self, token_info):
def _is_token_expired(self, token_info):
now = int(time.time())
return token_info['expires_at'] < now
@@ -112,10 +112,10 @@ class SpotifyOAuth(object):
raise SpotifyOauthError(response.reason)
token_info = response.json()
token_info = self._add_custom_values_to_token_info(token_info)
self.save_token_info(token_info)
self._save_token_info(token_info)
return token_info
def normalize_scope(self, scope):
def _normalize_scope(self, scope):
if scope:
scopes = scope.split()
scopes.sort()
@@ -123,7 +123,7 @@ class SpotifyOAuth(object):
else:
return None
def refresh_access_token(self, refresh_token):
def _refresh_access_token(self, refresh_token):
payload = { 'refresh_token': refresh_token,
'grant_type': 'refresh_token'}
@@ -143,7 +143,7 @@ class SpotifyOAuth(object):
token_info = self._add_custom_values_to_token_info(token_info)
if not 'refresh_token' in token_info:
token_info['refresh_token'] = refresh_token
self.save_token_info(token_info)
self._save_token_info(token_info)
return token_info
def _add_custom_values_to_token_info(self, token_info):

View File

@@ -23,22 +23,22 @@ def prompt_for_user_token(username, scope=None, client_id = None,
'''
if not client_id:
client_id = os.getenv('CLIENT_ID')
client_id = os.getenv('SPOTIPY_CLIENT_ID')
if not client_secret:
client_secret = os.getenv('CLIENT_SECRET')
client_secret = os.getenv('SPOTIPY_CLIENT_SECRET')
if not redirect_uri:
redirect_uri = os.getenv('REDIRECT_URI')
redirect_uri = os.getenv('SPOTIPY_REDIRECT_URI')
if not client_id:
print '''
You need to set your Spotify API credentials. You can do this by
setting environment variables like so:
export CLIENT_ID='your-spotify-client-id'
export CLIENT_SECRET='your-spotify-client-secret'
export REDIRECT_URI='your-app-redirect-url'
export SPOTIPY_CLIENT_ID='your-spotify-client-id'
export SPOTIPY_CLIENT_SECRET='your-spotify-client-secret'
export SPOTIPY_REDIRECT_URI='your-app-redirect-url'
Get your credentials at
https://developer.spotify.com/my-applications