2014-08-20 18:04:29 +00:00
|
|
|
|
# coding: utf-8
|
|
|
|
|
|
2015-06-05 09:07:28 +00:00
|
|
|
|
|
2015-06-05 09:14:13 +00:00
|
|
|
|
from __future__ import print_function
|
2014-08-20 18:04:29 +00:00
|
|
|
|
import sys
|
|
|
|
|
import requests
|
|
|
|
|
import json
|
2015-04-01 14:16:02 +00:00
|
|
|
|
import time
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
2017-01-07 15:18:46 +00:00
|
|
|
|
import six
|
|
|
|
|
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" A simple and thin Python library for the Spotify Web API
|
|
|
|
|
"""
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
2016-06-01 00:02:25 +00:00
|
|
|
|
|
2014-08-20 18:04:29 +00:00
|
|
|
|
class SpotifyException(Exception):
|
2015-09-24 17:30:07 +00:00
|
|
|
|
def __init__(self, http_status, code, msg, headers=None):
|
2014-08-20 18:04:29 +00:00
|
|
|
|
self.http_status = http_status
|
|
|
|
|
self.code = code
|
|
|
|
|
self.msg = msg
|
2015-09-24 17:30:07 +00:00
|
|
|
|
# `headers` is used to support `Retry-After` in the event of a
|
|
|
|
|
# 429 status code.
|
|
|
|
|
if headers is None:
|
|
|
|
|
headers = {}
|
|
|
|
|
self.headers = headers
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
def __str__(self):
|
2015-06-05 09:07:28 +00:00
|
|
|
|
return 'http status: {0}, code:{1} - {2}'.format(
|
2014-08-20 18:04:29 +00:00
|
|
|
|
self.http_status, self.code, self.msg)
|
|
|
|
|
|
2016-10-18 03:14:54 +00:00
|
|
|
|
|
2014-08-20 18:04:29 +00:00
|
|
|
|
class Spotify(object):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2014-08-22 15:00:29 +00:00
|
|
|
|
Example usage::
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
import spotipy
|
|
|
|
|
|
|
|
|
|
urn = 'spotify:artist:3jOstUTkEu2JkjvRdBA5Gu'
|
|
|
|
|
sp = spotipy.Spotify()
|
|
|
|
|
|
|
|
|
|
sp.trace = True # turn on tracing
|
2015-12-30 21:11:27 +00:00
|
|
|
|
sp.trace_out = True # turn on trace out
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
artist = sp.artist(urn)
|
|
|
|
|
print(artist)
|
|
|
|
|
|
|
|
|
|
user = sp.user('plamere')
|
|
|
|
|
print(user)
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
2015-01-03 18:09:46 +00:00
|
|
|
|
trace = False # Enable tracing?
|
2015-12-30 21:11:27 +00:00
|
|
|
|
trace_out = False
|
2015-04-28 10:25:06 +00:00
|
|
|
|
max_get_retries = 10
|
2014-08-21 14:58:40 +00:00
|
|
|
|
|
2015-04-01 14:03:29 +00:00
|
|
|
|
def __init__(self, auth=None, requests_session=True,
|
2017-01-02 13:43:40 +00:00
|
|
|
|
client_credentials_manager=None, proxies=None, requests_timeout=None):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2015-01-03 18:09:46 +00:00
|
|
|
|
Create a Spotify API object.
|
|
|
|
|
|
|
|
|
|
:param auth: An authorization token (optional)
|
|
|
|
|
:param requests_session:
|
|
|
|
|
A Requests session object or a truthy value to create one.
|
|
|
|
|
A falsy value disables sessions.
|
|
|
|
|
It should generally be a good idea to keep sessions enabled
|
|
|
|
|
for performance reasons (connection pooling).
|
2015-04-01 14:03:29 +00:00
|
|
|
|
:param client_credentials_manager:
|
|
|
|
|
SpotifyClientCredentials object
|
2016-08-23 14:48:49 +00:00
|
|
|
|
:param proxies:
|
|
|
|
|
Definition of proxies (optional)
|
2016-10-29 14:02:25 +00:00
|
|
|
|
:param requests_timeout:
|
|
|
|
|
Tell Requests to stop waiting for a response after a given number of seconds
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2014-08-20 18:04:29 +00:00
|
|
|
|
self.prefix = 'https://api.spotify.com/v1/'
|
|
|
|
|
self._auth = auth
|
2015-04-01 14:03:29 +00:00
|
|
|
|
self.client_credentials_manager = client_credentials_manager
|
2016-08-23 14:48:49 +00:00
|
|
|
|
self.proxies = proxies
|
2016-10-29 14:02:25 +00:00
|
|
|
|
self.requests_timeout = requests_timeout
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
2015-01-03 18:09:46 +00:00
|
|
|
|
if isinstance(requests_session, requests.Session):
|
|
|
|
|
self._session = requests_session
|
|
|
|
|
else:
|
|
|
|
|
if requests_session: # Build a new session.
|
|
|
|
|
self._session = requests.Session()
|
|
|
|
|
else: # Use the Requests API module as a "session".
|
|
|
|
|
from requests import api
|
|
|
|
|
self._session = api
|
|
|
|
|
|
2014-08-20 18:04:29 +00:00
|
|
|
|
def _auth_headers(self):
|
|
|
|
|
if self._auth:
|
|
|
|
|
return {'Authorization': 'Bearer {0}'.format(self._auth)}
|
2015-04-01 14:03:29 +00:00
|
|
|
|
elif self.client_credentials_manager:
|
|
|
|
|
token = self.client_credentials_manager.get_access_token()
|
|
|
|
|
return {'Authorization': 'Bearer {0}'.format(token)}
|
2014-08-20 18:04:29 +00:00
|
|
|
|
else:
|
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
def _internal_call(self, method, url, payload, params):
|
|
|
|
|
args = dict(params=params)
|
2016-10-29 14:02:25 +00:00
|
|
|
|
args["timeout"] = self.requests_timeout
|
2014-08-20 18:04:29 +00:00
|
|
|
|
if not url.startswith('http'):
|
|
|
|
|
url = self.prefix + url
|
|
|
|
|
headers = self._auth_headers()
|
|
|
|
|
headers['Content-Type'] = 'application/json'
|
|
|
|
|
|
|
|
|
|
if payload:
|
2015-01-03 18:09:46 +00:00
|
|
|
|
args["data"] = json.dumps(payload)
|
|
|
|
|
|
2015-12-30 21:11:27 +00:00
|
|
|
|
if self.trace_out:
|
|
|
|
|
print(url)
|
2016-08-23 14:48:49 +00:00
|
|
|
|
r = self._session.request(method, url, headers=headers, proxies=self.proxies, **args)
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
2015-01-03 18:26:35 +00:00
|
|
|
|
if self.trace: # pragma: no cover
|
2014-08-20 18:04:29 +00:00
|
|
|
|
print()
|
2016-03-12 14:59:16 +00:00
|
|
|
|
print ('headers', headers)
|
2016-02-20 13:54:11 +00:00
|
|
|
|
print ('http status', r.status_code)
|
2014-08-20 18:04:29 +00:00
|
|
|
|
print(method, r.url)
|
|
|
|
|
if payload:
|
|
|
|
|
print("DATA", json.dumps(payload))
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
r.raise_for_status()
|
|
|
|
|
except:
|
2016-02-12 12:24:28 +00:00
|
|
|
|
if r.text and len(r.text) > 0 and r.text != 'null':
|
2016-02-12 11:30:45 +00:00
|
|
|
|
raise SpotifyException(r.status_code,
|
2015-09-24 17:30:07 +00:00
|
|
|
|
-1, '%s:\n %s' % (r.url, r.json()['error']['message']),
|
|
|
|
|
headers=r.headers)
|
2016-02-12 11:30:45 +00:00
|
|
|
|
else:
|
|
|
|
|
raise SpotifyException(r.status_code,
|
2015-09-24 17:30:07 +00:00
|
|
|
|
-1, '%s:\n %s' % (r.url, 'error'), headers=r.headers)
|
2015-06-05 10:49:01 +00:00
|
|
|
|
finally:
|
|
|
|
|
r.connection.close()
|
2016-02-12 12:24:28 +00:00
|
|
|
|
if r.text and len(r.text) > 0 and r.text != 'null':
|
2014-08-20 18:04:29 +00:00
|
|
|
|
results = r.json()
|
2015-01-03 18:26:35 +00:00
|
|
|
|
if self.trace: # pragma: no cover
|
2014-08-20 18:04:29 +00:00
|
|
|
|
print('RESP', results)
|
|
|
|
|
print()
|
|
|
|
|
return results
|
|
|
|
|
else:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def _get(self, url, args=None, payload=None, **kwargs):
|
|
|
|
|
if args:
|
|
|
|
|
kwargs.update(args)
|
2015-04-01 14:16:02 +00:00
|
|
|
|
retries = self.max_get_retries
|
2015-04-28 10:25:06 +00:00
|
|
|
|
delay = 1
|
2015-04-01 14:16:02 +00:00
|
|
|
|
while retries > 0:
|
|
|
|
|
try:
|
|
|
|
|
return self._internal_call('GET', url, payload, kwargs)
|
|
|
|
|
except SpotifyException as e:
|
2015-04-28 10:25:06 +00:00
|
|
|
|
retries -= 1
|
|
|
|
|
status = e.http_status
|
|
|
|
|
# 429 means we hit a rate limit, backoff
|
2015-04-28 10:38:35 +00:00
|
|
|
|
if status == 429 or (status >= 500 and status < 600):
|
2015-04-01 14:16:02 +00:00
|
|
|
|
if retries < 0:
|
|
|
|
|
raise
|
|
|
|
|
else:
|
2015-09-24 17:30:07 +00:00
|
|
|
|
sleep_seconds = int(e.headers.get('Retry-After', delay))
|
|
|
|
|
print ('retrying ...' + str(sleep_seconds) + 'secs')
|
2017-01-22 13:04:51 +00:00
|
|
|
|
time.sleep(sleep_seconds + 1)
|
2015-04-28 10:25:06 +00:00
|
|
|
|
delay += 1
|
2015-04-01 14:16:02 +00:00
|
|
|
|
else:
|
|
|
|
|
raise
|
2016-10-24 16:19:08 +00:00
|
|
|
|
except Exception as e:
|
2016-02-12 11:30:45 +00:00
|
|
|
|
raise
|
|
|
|
|
print ('exception', str(e))
|
2015-06-08 12:37:19 +00:00
|
|
|
|
# some other exception. Requests have
|
|
|
|
|
# been know to throw a BadStatusLine exception
|
|
|
|
|
retries -= 1
|
|
|
|
|
if retries >= 0:
|
2015-09-24 17:30:07 +00:00
|
|
|
|
sleep_seconds = int(e.headers.get('Retry-After', delay))
|
2015-06-08 12:37:19 +00:00
|
|
|
|
print ('retrying ...' + str(delay) + 'secs')
|
2017-01-22 13:04:51 +00:00
|
|
|
|
time.sleep(sleep_seconds + 1)
|
2015-06-08 12:37:19 +00:00
|
|
|
|
delay += 1
|
|
|
|
|
else:
|
|
|
|
|
raise
|
|
|
|
|
|
2014-08-20 18:04:29 +00:00
|
|
|
|
def _post(self, url, args=None, payload=None, **kwargs):
|
|
|
|
|
if args:
|
|
|
|
|
kwargs.update(args)
|
|
|
|
|
return self._internal_call('POST', url, payload, kwargs)
|
|
|
|
|
|
|
|
|
|
def _delete(self, url, args=None, payload=None, **kwargs):
|
|
|
|
|
if args:
|
|
|
|
|
kwargs.update(args)
|
|
|
|
|
return self._internal_call('DELETE', url, payload, kwargs)
|
|
|
|
|
|
|
|
|
|
def _put(self, url, args=None, payload=None, **kwargs):
|
|
|
|
|
if args:
|
|
|
|
|
kwargs.update(args)
|
|
|
|
|
return self._internal_call('PUT', url, payload, kwargs)
|
|
|
|
|
|
|
|
|
|
def next(self, result):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" returns the next result given a paged result
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
2015-06-04 03:54:49 +00:00
|
|
|
|
- result - a previously returned paged result
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2014-08-20 18:04:29 +00:00
|
|
|
|
if result['next']:
|
|
|
|
|
return self._get(result['next'])
|
|
|
|
|
else:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def previous(self, result):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" returns the previous result given a paged result
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
2015-06-04 03:54:49 +00:00
|
|
|
|
- result - a previously returned paged result
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2014-08-20 18:04:29 +00:00
|
|
|
|
if result['previous']:
|
|
|
|
|
return self._get(result['previous'])
|
|
|
|
|
else:
|
|
|
|
|
return None
|
2015-06-04 03:54:49 +00:00
|
|
|
|
|
2017-01-22 12:52:00 +00:00
|
|
|
|
def _warn_old(self, msg):
|
2014-08-20 18:04:29 +00:00
|
|
|
|
print('warning:' + msg, file=sys.stderr)
|
|
|
|
|
|
2017-01-22 12:52:00 +00:00
|
|
|
|
def _warn(self, msg, *args):
|
|
|
|
|
print('warning:' + msg.format(*args), file=sys.stderr)
|
|
|
|
|
|
2014-08-20 18:04:29 +00:00
|
|
|
|
def track(self, track_id):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" returns a single track given the track's ID, URI or URL
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- track_id - a spotify URI, URL or ID
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
trid = self._get_id('track', track_id)
|
|
|
|
|
return self._get('tracks/' + trid)
|
|
|
|
|
|
2016-10-18 15:51:08 +00:00
|
|
|
|
def tracks(self, tracks, market = None):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" returns a list of tracks given a list of track IDs, URIs, or URLs
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- tracks - a list of spotify URIs, URLs or IDs
|
2016-10-18 15:51:08 +00:00
|
|
|
|
- market - an ISO 3166-1 alpha-2 country code.
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
tlist = [self._get_id('track', t) for t in tracks]
|
2016-10-18 15:51:08 +00:00
|
|
|
|
return self._get('tracks/?ids=' + ','.join(tlist), market = market)
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
def artist(self, artist_id):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" returns a single artist given the artist's ID, URI or URL
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- artist_id - an artist ID, URI or URL
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
trid = self._get_id('artist', artist_id)
|
|
|
|
|
return self._get('artists/' + trid)
|
|
|
|
|
|
|
|
|
|
def artists(self, artists):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" returns a list of artists given the artist IDs, URIs, or URLs
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- artists - a list of artist IDs, URIs or URLs
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
tlist = [self._get_id('artist', a) for a in artists]
|
|
|
|
|
return self._get('artists/?ids=' + ','.join(tlist))
|
|
|
|
|
|
2016-06-01 00:02:25 +00:00
|
|
|
|
def artist_albums(self, artist_id, album_type=None, country=None, limit=20,
|
|
|
|
|
offset=0):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Get Spotify catalog information about an artist's albums
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- artist_id - the artist ID, URI or URL
|
|
|
|
|
- album_type - 'album', 'single', 'appears_on', 'compilation'
|
|
|
|
|
- country - limit the response to one particular country.
|
|
|
|
|
- limit - the number of albums to return
|
|
|
|
|
- offset - the index of the first album to return
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
trid = self._get_id('artist', artist_id)
|
2015-06-04 03:54:49 +00:00
|
|
|
|
return self._get('artists/' + trid + '/albums', album_type=album_type,
|
2016-06-01 00:02:25 +00:00
|
|
|
|
country=country, limit=limit, offset=offset)
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
def artist_top_tracks(self, artist_id, country='US'):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Get Spotify catalog information about an artist's top 10 tracks
|
2014-08-20 18:04:29 +00:00
|
|
|
|
by country.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- artist_id - the artist ID, URI or URL
|
|
|
|
|
- country - limit the response to one particular country.
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
trid = self._get_id('artist', artist_id)
|
|
|
|
|
return self._get('artists/' + trid + '/top-tracks', country=country)
|
|
|
|
|
|
|
|
|
|
def artist_related_artists(self, artist_id):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Get Spotify catalog information about artists similar to an
|
2015-06-04 03:54:49 +00:00
|
|
|
|
identified artist. Similarity is based on analysis of the
|
2014-08-20 18:04:29 +00:00
|
|
|
|
Spotify community's listening history.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- artist_id - the artist ID, URI or URL
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2014-08-20 18:04:29 +00:00
|
|
|
|
trid = self._get_id('artist', artist_id)
|
|
|
|
|
return self._get('artists/' + trid + '/related-artists')
|
|
|
|
|
|
2015-06-04 04:16:12 +00:00
|
|
|
|
def album(self, album_id):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" returns a single album given the album's ID, URIs or URL
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- album_id - the album ID, URI or URL
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
trid = self._get_id('album', album_id)
|
2015-06-04 04:16:12 +00:00
|
|
|
|
return self._get('albums/' + trid)
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
2015-06-04 03:54:49 +00:00
|
|
|
|
def album_tracks(self, album_id, limit=50, offset=0):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Get Spotify catalog information about an album's tracks
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- album_id - the album ID, URI or URL
|
2015-06-04 03:54:49 +00:00
|
|
|
|
- limit - the number of items to return
|
|
|
|
|
- offset - the index of the first item to return
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
trid = self._get_id('album', album_id)
|
2016-06-01 00:02:25 +00:00
|
|
|
|
return self._get('albums/' + trid + '/tracks/', limit=limit,
|
|
|
|
|
offset=offset)
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
def albums(self, albums):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" returns a list of albums given the album IDs, URIs, or URLs
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- albums - a list of album IDs, URIs or URLs
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
tlist = [self._get_id('album', a) for a in albums]
|
|
|
|
|
return self._get('albums/?ids=' + ','.join(tlist))
|
|
|
|
|
|
2015-08-05 17:52:38 +00:00
|
|
|
|
def search(self, q, limit=10, offset=0, type='track', market=None):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" searches for an item
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- q - the search query
|
|
|
|
|
- limit - the number of items to return
|
|
|
|
|
- offset - the index of the first item to return
|
2015-02-19 14:01:44 +00:00
|
|
|
|
- type - the type of item to return. One of 'artist', 'album',
|
|
|
|
|
'track' or 'playlist'
|
2015-08-05 17:52:38 +00:00
|
|
|
|
- market - An ISO 3166-1 alpha-2 country code or the string from_token.
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2015-08-05 17:52:38 +00:00
|
|
|
|
return self._get('search', q=q, limit=limit, offset=offset, type=type, market=market)
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
def user(self, user):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Gets basic profile information about a Spotify User
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- user - the id of the usr
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2014-08-20 18:04:29 +00:00
|
|
|
|
return self._get('users/' + user)
|
2015-06-04 03:54:49 +00:00
|
|
|
|
|
2016-12-31 15:10:22 +00:00
|
|
|
|
def current_user_playlists(self, limit=50, offset=0):
|
2016-10-18 03:14:54 +00:00
|
|
|
|
""" Get current user playlists without required getting his profile
|
|
|
|
|
Parameters:
|
|
|
|
|
- limit - the number of items to return
|
|
|
|
|
- offset - the index of the first item to return
|
|
|
|
|
"""
|
|
|
|
|
return self._get("me/playlists", limit=limit, offset=offset)
|
|
|
|
|
|
2014-08-20 19:50:32 +00:00
|
|
|
|
def user_playlists(self, user, limit=50, offset=0):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Gets playlists of a user
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- user - the id of the usr
|
2014-08-20 19:50:32 +00:00
|
|
|
|
- limit - the number of items to return
|
|
|
|
|
- offset - the index of the first item to return
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2016-06-01 00:02:25 +00:00
|
|
|
|
return self._get("users/%s/playlists" % user, limit=limit,
|
|
|
|
|
offset=offset)
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
2016-06-01 00:02:25 +00:00
|
|
|
|
def user_playlist(self, user, playlist_id=None, fields=None):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Gets playlist of a user
|
2014-08-20 18:04:29 +00:00
|
|
|
|
Parameters:
|
|
|
|
|
- user - the id of the user
|
|
|
|
|
- playlist_id - the id of the playlist
|
|
|
|
|
- fields - which fields to return
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2016-06-01 00:02:25 +00:00
|
|
|
|
if playlist_id is None:
|
2014-08-20 18:04:29 +00:00
|
|
|
|
return self._get("users/%s/starred" % (user), fields=fields)
|
2014-08-20 19:50:32 +00:00
|
|
|
|
plid = self._get_id('playlist', playlist_id)
|
2014-11-15 12:20:11 +00:00
|
|
|
|
return self._get("users/%s/playlists/%s" % (user, plid), fields=fields)
|
|
|
|
|
|
2016-06-01 00:02:25 +00:00
|
|
|
|
def user_playlist_tracks(self, user, playlist_id=None, fields=None,
|
2016-08-23 02:29:52 +00:00
|
|
|
|
limit=100, offset=0, market=None):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Get full details of the tracks of a playlist owned by a user.
|
2014-11-15 12:20:11 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- user - the id of the user
|
|
|
|
|
- playlist_id - the id of the playlist
|
|
|
|
|
- fields - which fields to return
|
|
|
|
|
- limit - the maximum number of tracks to return
|
|
|
|
|
- offset - the index of the first track to return
|
2016-06-09 07:42:51 +00:00
|
|
|
|
- market - an ISO 3166-1 alpha-2 country code.
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2014-11-15 12:20:11 +00:00
|
|
|
|
plid = self._get_id('playlist', playlist_id)
|
2015-06-04 03:54:49 +00:00
|
|
|
|
return self._get("users/%s/playlists/%s/tracks" % (user, plid),
|
2016-08-23 02:29:52 +00:00
|
|
|
|
limit=limit, offset=offset, fields=fields,
|
|
|
|
|
market=market)
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
2017-09-17 16:03:49 +00:00
|
|
|
|
|
2017-06-02 23:51:38 +00:00
|
|
|
|
def user_playlist_create(self, user, name, public=True, description=''):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Creates a playlist for a user
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- user - the id of the user
|
|
|
|
|
- name - the name of the playlist
|
|
|
|
|
- public - is the created playlist public
|
2017-06-02 23:51:38 +00:00
|
|
|
|
- description - the description of the playlist
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2017-06-02 23:51:38 +00:00
|
|
|
|
data = {'name': name, 'public': public, 'description': description}
|
2017-09-17 16:03:49 +00:00
|
|
|
|
|
|
|
|
|
|
2016-06-01 00:02:25 +00:00
|
|
|
|
return self._post("users/%s/playlists" % (user,), payload=data)
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
2016-06-01 00:02:06 +00:00
|
|
|
|
def user_playlist_change_details(
|
2016-12-26 17:32:28 +00:00
|
|
|
|
self, user, playlist_id, name=None, public=None,
|
2017-06-02 23:51:38 +00:00
|
|
|
|
collaborative=None, description=None):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Changes a playlist's name and/or public/private state
|
2016-06-01 00:02:06 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- user - the id of the user
|
|
|
|
|
- playlist_id - the id of the playlist
|
|
|
|
|
- name - optional name of the playlist
|
|
|
|
|
- public - optional is the playlist public
|
2016-12-26 17:32:28 +00:00
|
|
|
|
- collaborative - optional is the playlist collaborative
|
2017-06-02 23:51:38 +00:00
|
|
|
|
- description - optional description of the playlist
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2017-09-17 16:03:49 +00:00
|
|
|
|
|
2016-06-01 00:02:06 +00:00
|
|
|
|
data = {}
|
2017-01-07 15:18:46 +00:00
|
|
|
|
if isinstance(name, six.string_types):
|
2016-06-01 00:02:06 +00:00
|
|
|
|
data['name'] = name
|
2016-12-26 17:32:28 +00:00
|
|
|
|
if isinstance(public, bool):
|
2016-06-01 00:02:06 +00:00
|
|
|
|
data['public'] = public
|
2016-12-26 17:32:28 +00:00
|
|
|
|
if isinstance(collaborative, bool):
|
|
|
|
|
data['collaborative'] = collaborative
|
2017-06-02 23:51:38 +00:00
|
|
|
|
if isinstance(description, six.string_types):
|
|
|
|
|
data['description'] = description
|
2016-06-01 00:02:06 +00:00
|
|
|
|
return self._put("users/%s/playlists/%s" % (user, playlist_id),
|
|
|
|
|
payload=data)
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
2016-05-22 22:57:16 +00:00
|
|
|
|
def user_playlist_unfollow(self, user, playlist_id):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Unfollows (deletes) a playlist for a user
|
2016-05-22 22:57:16 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- user - the id of the user
|
|
|
|
|
- name - the name of the playlist
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2016-05-22 22:57:16 +00:00
|
|
|
|
return self._delete("users/%s/playlists/%s/followers" % (user, playlist_id))
|
|
|
|
|
|
2015-06-04 03:54:49 +00:00
|
|
|
|
def user_playlist_add_tracks(self, user, playlist_id, tracks,
|
2016-06-01 00:02:25 +00:00
|
|
|
|
position=None):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Adds tracks to a playlist
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- user - the id of the user
|
|
|
|
|
- playlist_id - the id of the playlist
|
|
|
|
|
- tracks - a list of track URIs, URLs or IDs
|
|
|
|
|
- position - the position to add the tracks
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2014-08-20 18:04:29 +00:00
|
|
|
|
plid = self._get_id('playlist', playlist_id)
|
2016-06-01 00:02:25 +00:00
|
|
|
|
ftracks = [self._get_uri('track', tid) for tid in tracks]
|
|
|
|
|
return self._post("users/%s/playlists/%s/tracks" % (user, plid),
|
|
|
|
|
payload=ftracks, position=position)
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
def user_playlist_replace_tracks(self, user, playlist_id, tracks):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Replace all tracks in a playlist
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- user - the id of the user
|
|
|
|
|
- playlist_id - the id of the playlist
|
2014-08-22 15:00:29 +00:00
|
|
|
|
- tracks - the list of track ids to add to the playlist
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2014-08-20 18:04:29 +00:00
|
|
|
|
plid = self._get_id('playlist', playlist_id)
|
2016-06-01 00:02:25 +00:00
|
|
|
|
ftracks = [self._get_uri('track', tid) for tid in tracks]
|
|
|
|
|
payload = {"uris": ftracks}
|
|
|
|
|
return self._put("users/%s/playlists/%s/tracks" % (user, plid),
|
|
|
|
|
payload=payload)
|
|
|
|
|
|
|
|
|
|
def user_playlist_reorder_tracks(
|
|
|
|
|
self, user, playlist_id, range_start, insert_before,
|
|
|
|
|
range_length=1, snapshot_id=None):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Reorder tracks in a playlist
|
2015-03-30 10:25:16 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- user - the id of the user
|
|
|
|
|
- playlist_id - the id of the playlist
|
|
|
|
|
- range_start - the position of the first track to be reordered
|
|
|
|
|
- range_length - optional the number of tracks to be reordered (default: 1)
|
|
|
|
|
- insert_before - the position where the tracks should be inserted
|
|
|
|
|
- snapshot_id - optional playlist's snapshot ID
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2015-03-30 10:25:16 +00:00
|
|
|
|
plid = self._get_id('playlist', playlist_id)
|
2016-06-01 00:02:25 +00:00
|
|
|
|
payload = {"range_start": range_start,
|
|
|
|
|
"range_length": range_length,
|
|
|
|
|
"insert_before": insert_before}
|
2015-03-30 10:25:16 +00:00
|
|
|
|
if snapshot_id:
|
|
|
|
|
payload["snapshot_id"] = snapshot_id
|
2016-06-01 00:02:25 +00:00
|
|
|
|
return self._put("users/%s/playlists/%s/tracks" % (user, plid),
|
|
|
|
|
payload=payload)
|
2015-03-30 10:25:16 +00:00
|
|
|
|
|
2016-06-01 00:02:25 +00:00
|
|
|
|
def user_playlist_remove_all_occurrences_of_tracks(
|
|
|
|
|
self, user, playlist_id, tracks, snapshot_id=None):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Removes all occurrences of the given tracks from the given playlist
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- user - the id of the user
|
|
|
|
|
- playlist_id - the id of the playlist
|
2014-08-22 15:00:29 +00:00
|
|
|
|
- tracks - the list of track ids to add to the playlist
|
2014-12-02 00:18:30 +00:00
|
|
|
|
- snapshot_id - optional id of the playlist snapshot
|
2014-08-22 15:00:29 +00:00
|
|
|
|
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2014-08-22 15:00:29 +00:00
|
|
|
|
|
2014-08-20 18:04:29 +00:00
|
|
|
|
plid = self._get_id('playlist', playlist_id)
|
2016-06-01 00:02:25 +00:00
|
|
|
|
ftracks = [self._get_uri('track', tid) for tid in tracks]
|
|
|
|
|
payload = {"tracks": [{"uri": track} for track in ftracks]}
|
2014-12-02 00:18:30 +00:00
|
|
|
|
if snapshot_id:
|
|
|
|
|
payload["snapshot_id"] = snapshot_id
|
2015-06-04 03:54:49 +00:00
|
|
|
|
return self._delete("users/%s/playlists/%s/tracks" % (user, plid),
|
2016-06-01 00:02:25 +00:00
|
|
|
|
payload=payload)
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
2016-06-01 00:02:25 +00:00
|
|
|
|
def user_playlist_remove_specific_occurrences_of_tracks(
|
|
|
|
|
self, user, playlist_id, tracks, snapshot_id=None):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Removes all occurrences of the given tracks from the given playlist
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- user - the id of the user
|
|
|
|
|
- playlist_id - the id of the playlist
|
2015-06-04 03:54:49 +00:00
|
|
|
|
- tracks - an array of objects containing Spotify URIs of the tracks to remove with their current positions in the playlist. For example:
|
2014-08-22 15:00:29 +00:00
|
|
|
|
[ { "uri":"4iV5W9uYEdYUVa79Axb7Rh", "positions":[2] },
|
2015-06-04 03:54:49 +00:00
|
|
|
|
{ "uri":"1301WleyT98MSxVHPZCA6M", "positions":[7] } ]
|
2014-12-02 00:18:30 +00:00
|
|
|
|
- snapshot_id - optional id of the playlist snapshot
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2014-08-22 15:00:29 +00:00
|
|
|
|
|
2014-08-20 18:04:29 +00:00
|
|
|
|
plid = self._get_id('playlist', playlist_id)
|
2014-12-01 23:21:05 +00:00
|
|
|
|
ftracks = []
|
|
|
|
|
for tr in tracks:
|
|
|
|
|
ftracks.append({
|
|
|
|
|
"uri": self._get_uri("track", tr["uri"]),
|
|
|
|
|
"positions": tr["positions"],
|
|
|
|
|
})
|
2016-06-01 00:02:25 +00:00
|
|
|
|
payload = {"tracks": ftracks}
|
2014-12-02 00:18:30 +00:00
|
|
|
|
if snapshot_id:
|
|
|
|
|
payload["snapshot_id"] = snapshot_id
|
2015-06-04 03:54:49 +00:00
|
|
|
|
return self._delete("users/%s/playlists/%s/tracks" % (user, plid),
|
2016-06-01 00:02:25 +00:00
|
|
|
|
payload=payload)
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
2016-12-08 19:27:43 +00:00
|
|
|
|
def user_playlist_follow_playlist(self, playlist_owner_id, playlist_id):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2016-12-08 19:27:43 +00:00
|
|
|
|
Add the current authenticated user as a follower of a playlist.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- playlist_owner_id - the user id of the playlist owner
|
|
|
|
|
- playlist_id - the id of the playlist
|
|
|
|
|
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2016-12-08 19:27:43 +00:00
|
|
|
|
return self._put("users/{}/playlists/{}/followers".format(playlist_owner_id, playlist_id))
|
|
|
|
|
|
2016-12-31 15:58:01 +00:00
|
|
|
|
def user_playlist_is_following(self, playlist_owner_id, playlist_id, user_ids):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2016-12-31 15:58:01 +00:00
|
|
|
|
Check to see if the given users are following the given playlist
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- playlist_owner_id - the user id of the playlist owner
|
|
|
|
|
- playlist_id - the id of the playlist
|
|
|
|
|
- user_ids - the ids of the users that you want to check to see if they follow the playlist. Maximum: 5 ids.
|
|
|
|
|
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2016-12-31 15:58:01 +00:00
|
|
|
|
return self._get("users/{}/playlists/{}/followers/contains?ids={}".format(playlist_owner_id, playlist_id, ','.join(user_ids)))
|
|
|
|
|
|
2014-08-20 18:04:29 +00:00
|
|
|
|
def me(self):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Get detailed profile information about the current user.
|
2014-08-20 18:04:29 +00:00
|
|
|
|
An alias for the 'current_user' method.
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2014-08-20 18:04:29 +00:00
|
|
|
|
return self._get('me/')
|
|
|
|
|
|
|
|
|
|
def current_user(self):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Get detailed profile information about the current user.
|
2014-08-20 18:04:29 +00:00
|
|
|
|
An alias for the 'me' method.
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2014-08-20 18:04:29 +00:00
|
|
|
|
return self.me()
|
|
|
|
|
|
2017-05-13 08:35:48 +00:00
|
|
|
|
def current_user_playing_track(self):
|
|
|
|
|
''' Get information about the current users currently playing track.
|
|
|
|
|
'''
|
|
|
|
|
return self._get('me/player/currently-playing')
|
|
|
|
|
|
2015-12-30 21:11:27 +00:00
|
|
|
|
def current_user_saved_albums(self, limit=20, offset=0):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Gets a list of the albums saved in the current authorized user's
|
2015-12-30 21:11:27 +00:00
|
|
|
|
"Your Music" library
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- limit - the number of albums to return
|
|
|
|
|
- offset - the index of the first album to return
|
|
|
|
|
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2015-12-30 21:11:27 +00:00
|
|
|
|
return self._get('me/albums', limit=limit, offset=offset)
|
|
|
|
|
|
2014-08-20 18:04:29 +00:00
|
|
|
|
def current_user_saved_tracks(self, limit=20, offset=0):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Gets a list of the tracks saved in the current authorized user's
|
2014-08-20 18:04:29 +00:00
|
|
|
|
"Your Music" library
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- limit - the number of tracks to return
|
|
|
|
|
- offset - the index of the first track to return
|
|
|
|
|
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2014-08-20 18:04:29 +00:00
|
|
|
|
return self._get('me/tracks', limit=limit, offset=offset)
|
2016-10-24 16:19:08 +00:00
|
|
|
|
|
2015-08-10 13:17:14 +00:00
|
|
|
|
def current_user_followed_artists(self, limit=20, after=None):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Gets a list of the artists followed by the current authorized user
|
2015-08-10 13:17:14 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- limit - the number of tracks to return
|
|
|
|
|
- after - ghe last artist ID retrieved from the previous request
|
|
|
|
|
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2016-06-01 00:02:25 +00:00
|
|
|
|
return self._get('me/following', type='artist', limit=limit,
|
|
|
|
|
after=after)
|
2014-08-20 18:04:29 +00:00
|
|
|
|
|
2016-06-01 00:02:25 +00:00
|
|
|
|
def current_user_saved_tracks_delete(self, tracks=None):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Remove one or more tracks from the current user's
|
2014-08-20 18:04:29 +00:00
|
|
|
|
"Your Music" library.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- tracks - a list of track URIs, URLs or IDs
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2016-06-01 00:02:25 +00:00
|
|
|
|
tlist = []
|
|
|
|
|
if tracks is not None:
|
|
|
|
|
tlist = [self._get_id('track', t) for t in tracks]
|
2014-08-20 18:04:29 +00:00
|
|
|
|
return self._delete('me/tracks/?ids=' + ','.join(tlist))
|
|
|
|
|
|
2016-06-01 00:02:25 +00:00
|
|
|
|
def current_user_saved_tracks_contains(self, tracks=None):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Check if one or more tracks is already saved in
|
2014-08-22 17:40:15 +00:00
|
|
|
|
the current Spotify user’s “Your Music” library.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- tracks - a list of track URIs, URLs or IDs
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2016-06-01 00:02:25 +00:00
|
|
|
|
tlist = []
|
|
|
|
|
if tracks is not None:
|
|
|
|
|
tlist = [self._get_id('track', t) for t in tracks]
|
2014-08-22 17:40:15 +00:00
|
|
|
|
return self._get('me/tracks/contains?ids=' + ','.join(tlist))
|
|
|
|
|
|
2016-06-01 00:02:25 +00:00
|
|
|
|
def current_user_saved_tracks_add(self, tracks=None):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Add one or more tracks to the current user's
|
2014-08-20 18:04:29 +00:00
|
|
|
|
"Your Music" library.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- tracks - a list of track URIs, URLs or IDs
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2016-06-01 00:02:25 +00:00
|
|
|
|
tlist = []
|
|
|
|
|
if tracks is not None:
|
|
|
|
|
tlist = [self._get_id('track', t) for t in tracks]
|
2014-08-20 18:04:29 +00:00
|
|
|
|
return self._put('me/tracks/?ids=' + ','.join(tlist))
|
|
|
|
|
|
2016-06-01 00:02:25 +00:00
|
|
|
|
def current_user_top_artists(self, limit=20, offset=0,
|
|
|
|
|
time_range='medium_term'):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Get the current user's top artists
|
2016-02-20 13:54:11 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- limit - the number of entities to return
|
|
|
|
|
- offset - the index of the first entity to return
|
2016-06-01 00:02:25 +00:00
|
|
|
|
- time_range - Over what time frame are the affinities computed
|
2016-03-12 14:59:16 +00:00
|
|
|
|
Valid-values: short_term, medium_term, long_term
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2016-06-01 00:02:25 +00:00
|
|
|
|
return self._get('me/top/artists', time_range=time_range, limit=limit,
|
|
|
|
|
offset=offset)
|
2016-02-20 13:54:11 +00:00
|
|
|
|
|
2016-06-01 00:02:25 +00:00
|
|
|
|
def current_user_top_tracks(self, limit=20, offset=0,
|
|
|
|
|
time_range='medium_term'):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Get the current user's top tracks
|
2016-02-20 13:54:11 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- limit - the number of entities to return
|
|
|
|
|
- offset - the index of the first entity to return
|
2016-06-01 00:02:25 +00:00
|
|
|
|
- time_range - Over what time frame are the affinities computed
|
2016-03-12 14:59:16 +00:00
|
|
|
|
Valid-values: short_term, medium_term, long_term
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2016-06-01 00:02:25 +00:00
|
|
|
|
return self._get('me/top/tracks', time_range=time_range, limit=limit,
|
|
|
|
|
offset=offset)
|
2014-10-25 10:40:51 +00:00
|
|
|
|
|
2017-07-10 21:52:40 +00:00
|
|
|
|
def current_user_recently_played(self, limit=50):
|
|
|
|
|
''' Get the current user's recently played tracks
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- limit - the number of entities to return
|
|
|
|
|
'''
|
|
|
|
|
return self._get('me/player/recently-played', limit=limit)
|
|
|
|
|
|
2016-12-31 23:51:37 +00:00
|
|
|
|
def current_user_saved_albums_add(self, albums=[]):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Add one or more albums to the current user's
|
2016-12-31 23:51:37 +00:00
|
|
|
|
"Your Music" library.
|
|
|
|
|
Parameters:
|
|
|
|
|
- albums - a list of album URIs, URLs or IDs
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2016-12-31 23:51:37 +00:00
|
|
|
|
alist = [self._get_id('album', a) for a in albums]
|
|
|
|
|
r = self._put('me/albums?ids=' + ','.join(alist))
|
|
|
|
|
return r
|
|
|
|
|
|
2017-04-11 11:04:12 +00:00
|
|
|
|
def user_follow_artists(self, ids=[]):
|
|
|
|
|
''' Follow one or more artists
|
|
|
|
|
Parameters:
|
|
|
|
|
- ids - a list of artist IDs
|
|
|
|
|
'''
|
|
|
|
|
return self._put('me/following?type=artist&ids=' + ','.join(ids))
|
|
|
|
|
|
|
|
|
|
def user_follow_users(self, ids=[]):
|
|
|
|
|
''' Follow one or more users
|
|
|
|
|
Parameters:
|
|
|
|
|
- ids - a list of user IDs
|
|
|
|
|
'''
|
|
|
|
|
return self._put('me/following?type=user&ids=' + ','.join(ids))
|
|
|
|
|
|
2016-06-01 00:02:25 +00:00
|
|
|
|
def featured_playlists(self, locale=None, country=None, timestamp=None,
|
|
|
|
|
limit=20, offset=0):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Get a list of Spotify featured playlists
|
2015-06-04 03:54:49 +00:00
|
|
|
|
|
2014-10-25 10:40:51 +00:00
|
|
|
|
Parameters:
|
|
|
|
|
- locale - The desired language, consisting of a lowercase ISO
|
|
|
|
|
639 language code and an uppercase ISO 3166-1 alpha-2 country
|
2015-06-04 03:54:49 +00:00
|
|
|
|
code, joined by an underscore.
|
2014-10-25 10:40:51 +00:00
|
|
|
|
|
2015-06-04 03:54:49 +00:00
|
|
|
|
- country - An ISO 3166-1 alpha-2 country code.
|
2014-10-25 10:40:51 +00:00
|
|
|
|
|
|
|
|
|
- timestamp - A timestamp in ISO 8601 format:
|
|
|
|
|
yyyy-MM-ddTHH:mm:ss. Use this parameter to specify the user's
|
|
|
|
|
local time to get results tailored for that specific date and
|
|
|
|
|
time in the day
|
|
|
|
|
|
|
|
|
|
- limit - The maximum number of items to return. Default: 20.
|
|
|
|
|
Minimum: 1. Maximum: 50
|
|
|
|
|
|
|
|
|
|
- offset - The index of the first item to return. Default: 0
|
|
|
|
|
(the first object). Use with limit to get the next set of
|
2015-06-04 03:54:49 +00:00
|
|
|
|
items.
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2014-10-25 10:40:51 +00:00
|
|
|
|
return self._get('browse/featured-playlists', locale=locale,
|
2016-06-01 00:02:25 +00:00
|
|
|
|
country=country, timestamp=timestamp, limit=limit,
|
|
|
|
|
offset=offset)
|
2014-10-25 10:40:51 +00:00
|
|
|
|
|
2016-06-01 00:02:25 +00:00
|
|
|
|
def new_releases(self, country=None, limit=20, offset=0):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Get a list of new album releases featured in Spotify
|
2015-06-04 03:54:49 +00:00
|
|
|
|
|
2014-10-25 10:40:51 +00:00
|
|
|
|
Parameters:
|
2015-06-04 03:54:49 +00:00
|
|
|
|
- country - An ISO 3166-1 alpha-2 country code.
|
2014-10-25 10:40:51 +00:00
|
|
|
|
|
|
|
|
|
- limit - The maximum number of items to return. Default: 20.
|
|
|
|
|
Minimum: 1. Maximum: 50
|
|
|
|
|
|
|
|
|
|
- offset - The index of the first item to return. Default: 0
|
|
|
|
|
(the first object). Use with limit to get the next set of
|
2015-06-04 03:54:49 +00:00
|
|
|
|
items.
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2016-06-01 00:02:25 +00:00
|
|
|
|
return self._get('browse/new-releases', country=country, limit=limit,
|
|
|
|
|
offset=offset)
|
2014-10-25 10:40:51 +00:00
|
|
|
|
|
2016-06-01 00:02:25 +00:00
|
|
|
|
def categories(self, country=None, locale=None, limit=20, offset=0):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Get a list of new album releases featured in Spotify
|
2016-02-02 12:25:33 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- country - An ISO 3166-1 alpha-2 country code.
|
|
|
|
|
- locale - The desired language, consisting of an ISO 639
|
|
|
|
|
language code and an ISO 3166-1 alpha-2 country code, joined
|
|
|
|
|
by an underscore.
|
|
|
|
|
|
|
|
|
|
- limit - The maximum number of items to return. Default: 20.
|
|
|
|
|
Minimum: 1. Maximum: 50
|
|
|
|
|
|
|
|
|
|
- offset - The index of the first item to return. Default: 0
|
|
|
|
|
(the first object). Use with limit to get the next set of
|
|
|
|
|
items.
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2016-02-02 12:25:33 +00:00
|
|
|
|
return self._get('browse/categories', country=country, locale=locale,
|
2016-06-01 00:02:25 +00:00
|
|
|
|
limit=limit, offset=offset)
|
2016-02-02 12:25:33 +00:00
|
|
|
|
|
2016-06-01 00:02:25 +00:00
|
|
|
|
def category_playlists(self, category_id=None, country=None, limit=20,
|
|
|
|
|
offset=0):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Get a list of new album releases featured in Spotify
|
2016-02-02 12:25:33 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- category_id - The Spotify category ID for the category.
|
|
|
|
|
|
|
|
|
|
- country - An ISO 3166-1 alpha-2 country code.
|
|
|
|
|
|
|
|
|
|
- limit - The maximum number of items to return. Default: 20.
|
|
|
|
|
Minimum: 1. Maximum: 50
|
|
|
|
|
|
|
|
|
|
- offset - The index of the first item to return. Default: 0
|
|
|
|
|
(the first object). Use with limit to get the next set of
|
|
|
|
|
items.
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2016-06-01 00:02:25 +00:00
|
|
|
|
return self._get('browse/categories/' + category_id + '/playlists',
|
|
|
|
|
country=country, limit=limit, offset=offset)
|
2016-02-02 12:25:33 +00:00
|
|
|
|
|
2016-06-01 00:02:25 +00:00
|
|
|
|
def recommendations(self, seed_artists=None, seed_genres=None,
|
|
|
|
|
seed_tracks=None, limit=20, country=None, **kwargs):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Get a list of recommended tracks for one to five seeds.
|
2016-03-30 18:31:58 +00:00
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- seed_artists - a list of artist IDs, URIs or URLs
|
|
|
|
|
|
|
|
|
|
- seed_tracks - a list of artist IDs, URIs or URLs
|
|
|
|
|
|
2016-10-24 16:19:08 +00:00
|
|
|
|
- seed_genres - a list of genre names. Available genres for
|
2016-03-30 18:31:58 +00:00
|
|
|
|
recommendations can be found by calling recommendation_genre_seeds
|
|
|
|
|
|
2016-10-24 16:19:08 +00:00
|
|
|
|
- country - An ISO 3166-1 alpha-2 country code. If provided, all
|
2016-03-30 18:31:58 +00:00
|
|
|
|
results will be playable in this country.
|
|
|
|
|
|
|
|
|
|
- limit - The maximum number of items to return. Default: 20.
|
|
|
|
|
Minimum: 1. Maximum: 100
|
|
|
|
|
|
2016-10-24 16:19:08 +00:00
|
|
|
|
- min/max/target_<attribute> - For the tuneable track attributes listed
|
2016-03-30 18:31:58 +00:00
|
|
|
|
in the documentation, these values provide filters and targeting on
|
|
|
|
|
results.
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2016-03-30 18:31:58 +00:00
|
|
|
|
params = dict(limit=limit)
|
|
|
|
|
if seed_artists:
|
2016-10-24 16:19:08 +00:00
|
|
|
|
params['seed_artists'] = ','.join(
|
|
|
|
|
[self._get_id('artist', a) for a in seed_artists])
|
2016-03-30 18:31:58 +00:00
|
|
|
|
if seed_genres:
|
2016-10-24 16:19:08 +00:00
|
|
|
|
params['seed_genres'] = ','.join(seed_genres)
|
2016-03-30 18:31:58 +00:00
|
|
|
|
if seed_tracks:
|
2016-10-24 16:19:08 +00:00
|
|
|
|
params['seed_tracks'] = ','.join(
|
|
|
|
|
[self._get_id('track', t) for t in seed_tracks])
|
2016-03-30 18:31:58 +00:00
|
|
|
|
if country:
|
|
|
|
|
params['market'] = country
|
|
|
|
|
|
2016-06-01 00:02:25 +00:00
|
|
|
|
for attribute in ["acousticness", "danceability", "duration_ms",
|
|
|
|
|
"energy", "instrumentalness", "key", "liveness",
|
|
|
|
|
"loudness", "mode", "popularity", "speechiness",
|
|
|
|
|
"tempo", "time_signature", "valence"]:
|
2016-03-30 18:31:58 +00:00
|
|
|
|
for prefix in ["min_", "max_", "target_"]:
|
|
|
|
|
param = prefix + attribute
|
|
|
|
|
if param in kwargs:
|
|
|
|
|
params[param] = kwargs[param]
|
2016-10-24 16:19:08 +00:00
|
|
|
|
return self._get('recommendations', **params)
|
2016-03-30 18:31:58 +00:00
|
|
|
|
|
|
|
|
|
def recommendation_genre_seeds(self):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Get a list of genres available for the recommendations function.
|
|
|
|
|
"""
|
2016-03-30 18:31:58 +00:00
|
|
|
|
return self._get('recommendations/available-genre-seeds')
|
|
|
|
|
|
2016-12-15 21:21:05 +00:00
|
|
|
|
def audio_analysis(self, track_id):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Get audio analysis for a track based upon its Spotify ID
|
2016-12-15 21:21:05 +00:00
|
|
|
|
Parameters:
|
|
|
|
|
- track_id - a track URI, URL or ID
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2016-12-15 21:21:05 +00:00
|
|
|
|
trid = self._get_id('track', track_id)
|
|
|
|
|
return self._get('audio-analysis/' + trid)
|
|
|
|
|
|
2015-12-30 21:11:27 +00:00
|
|
|
|
def audio_features(self, tracks=[]):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Get audio features for one or multiple tracks based upon their Spotify IDs
|
2015-12-30 21:11:27 +00:00
|
|
|
|
Parameters:
|
|
|
|
|
- tracks - a list of track URIs, URLs or IDs, maximum: 50 ids
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2017-01-02 13:57:48 +00:00
|
|
|
|
if isinstance(tracks, str):
|
|
|
|
|
trackid = self._get_id('track', tracks)
|
|
|
|
|
results = self._get('audio-features/?ids=' + trackid)
|
|
|
|
|
else:
|
|
|
|
|
tlist = [self._get_id('track', t) for t in tracks]
|
|
|
|
|
results = self._get('audio-features/?ids=' + ','.join(tlist))
|
2016-02-17 14:18:06 +00:00
|
|
|
|
# the response has changed, look for the new style first, and if
|
|
|
|
|
# its not there, fallback on the old style
|
2016-02-20 13:54:11 +00:00
|
|
|
|
if 'audio_features' in results:
|
|
|
|
|
return results['audio_features']
|
2016-02-17 14:18:06 +00:00
|
|
|
|
else:
|
|
|
|
|
return results
|
2015-12-30 21:11:27 +00:00
|
|
|
|
|
2016-10-15 12:41:48 +00:00
|
|
|
|
def audio_analysis(self, id):
|
2017-04-11 13:27:51 +00:00
|
|
|
|
""" Get audio analysis for a track based upon its Spotify ID
|
2016-10-15 12:41:48 +00:00
|
|
|
|
Parameters:
|
|
|
|
|
- id - a track URIs, URLs or IDs
|
2017-04-11 13:27:51 +00:00
|
|
|
|
"""
|
2016-10-15 12:41:48 +00:00
|
|
|
|
id = self._get_id('track', id)
|
|
|
|
|
return self._get('audio-analysis/'+id)
|
|
|
|
|
|
2017-04-07 20:40:50 +00:00
|
|
|
|
def devices(self):
|
|
|
|
|
''' Get a list of user's available devices.
|
|
|
|
|
'''
|
|
|
|
|
return self._get("me/player/devices")
|
|
|
|
|
|
|
|
|
|
def current_playback(self, market = None):
|
|
|
|
|
''' Get information about user's current playback.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- market - an ISO 3166-1 alpha-2 country code.
|
|
|
|
|
'''
|
|
|
|
|
return self._get("me/player", market = market)
|
|
|
|
|
|
|
|
|
|
def currently_playing(self, market = None):
|
|
|
|
|
''' Get user's currently playing track.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- market - an ISO 3166-1 alpha-2 country code.
|
|
|
|
|
'''
|
|
|
|
|
return self._get("me/player/currently-playing", market = market)
|
|
|
|
|
|
|
|
|
|
def transfer_playback(self, device_id, force_play = True):
|
|
|
|
|
''' Transfer playback to another device.
|
|
|
|
|
Note that the API accepts a list of device ids, but only
|
|
|
|
|
actually supports one.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- device_id - transfer playback to this device
|
|
|
|
|
- force_play - true: after transfer, play. false:
|
|
|
|
|
keep current state.
|
|
|
|
|
'''
|
|
|
|
|
data = {
|
|
|
|
|
'device_ids': [device_id],
|
|
|
|
|
'play': force_play
|
|
|
|
|
}
|
|
|
|
|
return self._put("me/player", payload=data)
|
|
|
|
|
|
|
|
|
|
def start_playback(self, device_id = None, context_uri = None, uris = None, offset = None):
|
|
|
|
|
''' Start or resume user's playback.
|
|
|
|
|
|
|
|
|
|
Provide a `context_uri` to start playback or a album,
|
|
|
|
|
artist, or playlist.
|
|
|
|
|
|
|
|
|
|
Provide a `uris` list to start playback of one or more
|
|
|
|
|
tracks.
|
|
|
|
|
|
|
|
|
|
Provide `offset` as {"position": <int>} or {"uri": "<track uri>"}
|
|
|
|
|
to start playback at a particular offset.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- device_id - device target for playback
|
|
|
|
|
- context_uri - spotify context uri to play
|
|
|
|
|
- uris - spotify track uris
|
|
|
|
|
- offset - offset into context by index or track
|
|
|
|
|
'''
|
|
|
|
|
if context_uri is not None and uris is not None:
|
|
|
|
|
self._warn('specify either context uri or uris, not both')
|
|
|
|
|
return
|
|
|
|
|
if uris is not None and not isinstance(uris, list):
|
|
|
|
|
self._warn('uris must be a list')
|
|
|
|
|
return
|
|
|
|
|
data = {}
|
|
|
|
|
if context_uri is not None:
|
|
|
|
|
data['context_uri'] = context_uri
|
|
|
|
|
if uris is not None:
|
|
|
|
|
data['uris'] = uris
|
|
|
|
|
if offset is not None:
|
|
|
|
|
data['offset'] = offset
|
|
|
|
|
return self._put(self._append_device_id("me/player/play", device_id), payload=data)
|
|
|
|
|
|
|
|
|
|
def pause_playback(self, device_id = None):
|
|
|
|
|
''' Pause user's playback.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- device_id - device target for playback
|
|
|
|
|
'''
|
|
|
|
|
return self._put(self._append_device_id("me/player/pause", device_id))
|
|
|
|
|
|
|
|
|
|
def next_track(self, device_id = None):
|
|
|
|
|
''' Skip user's playback to next track.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- device_id - device target for playback
|
|
|
|
|
'''
|
|
|
|
|
return self._post(self._append_device_id("me/player/next", device_id))
|
|
|
|
|
|
|
|
|
|
def previous_track(self, device_id = None):
|
|
|
|
|
''' Skip user's playback to previous track.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- device_id - device target for playback
|
|
|
|
|
'''
|
|
|
|
|
return self._post(self._append_device_id("me/player/previous", device_id))
|
|
|
|
|
|
|
|
|
|
def seek_track(self, position_ms, device_id = None):
|
|
|
|
|
''' Seek to position in current track.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- position_ms - position in milliseconds to seek to
|
|
|
|
|
- device_id - device target for playback
|
|
|
|
|
'''
|
|
|
|
|
if not isinstance(position_ms, int):
|
|
|
|
|
self._warn('position_ms must be an integer')
|
|
|
|
|
return
|
|
|
|
|
return self._put(self._append_device_id("me/player/seek?position_ms=%s" % position_ms, device_id))
|
|
|
|
|
|
|
|
|
|
def repeat(self, state, device_id = None):
|
|
|
|
|
''' Set repeat mode for playback.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- state - `track`, `context`, or `off`
|
|
|
|
|
- device_id - device target for playback
|
|
|
|
|
'''
|
|
|
|
|
if state not in ['track', 'context', 'off']:
|
|
|
|
|
self._warn('invalid state')
|
|
|
|
|
return
|
|
|
|
|
self._put(self._append_device_id("me/player/repeat?state=%s" % state, device_id))
|
|
|
|
|
|
|
|
|
|
def volume(self, volume_percent, device_id = None):
|
|
|
|
|
''' Set playback volume.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- volume_percent - volume between 0 and 100
|
|
|
|
|
- device_id - device target for playback
|
|
|
|
|
'''
|
|
|
|
|
if not isinstance(volume_percent, int):
|
|
|
|
|
self._warn('volume must be an integer')
|
|
|
|
|
return
|
|
|
|
|
if volume_percent < 0 or volume_percent > 100:
|
|
|
|
|
self._warn('volume must be between 0 and 100, inclusive')
|
|
|
|
|
return
|
|
|
|
|
self._put(self._append_device_id("me/player/volume?volume_percent=%s" % volume_percent, device_id))
|
|
|
|
|
|
|
|
|
|
def shuffle(self, state, device_id = None):
|
|
|
|
|
''' Toggle playback shuffling.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- state - true or false
|
|
|
|
|
- device_id - device target for playback
|
|
|
|
|
'''
|
|
|
|
|
if not isinstance(state, bool):
|
|
|
|
|
self._warn('state must be a boolean')
|
|
|
|
|
return
|
|
|
|
|
state = str(state).lower()
|
|
|
|
|
self._put(self._append_device_id("me/player/shuffle?state=%s" % state, device_id))
|
|
|
|
|
|
|
|
|
|
def _append_device_id(self, path, device_id):
|
|
|
|
|
''' Append device ID to API path.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
- device_id - device id to append
|
|
|
|
|
'''
|
|
|
|
|
if device_id:
|
2017-08-18 00:57:04 +00:00
|
|
|
|
if '?' in path:
|
|
|
|
|
path += "&device_id=%s" % device_id
|
|
|
|
|
else:
|
|
|
|
|
path += "?device_id=%s" % device_id
|
2017-04-07 20:40:50 +00:00
|
|
|
|
return path
|
|
|
|
|
|
2014-08-20 18:04:29 +00:00
|
|
|
|
def _get_id(self, type, id):
|
|
|
|
|
fields = id.split(':')
|
|
|
|
|
if len(fields) >= 3:
|
|
|
|
|
if type != fields[-2]:
|
2016-06-01 00:02:25 +00:00
|
|
|
|
self._warn('expected id of type %s but found type %s %s',
|
|
|
|
|
type, fields[-2], id)
|
2014-08-20 18:04:29 +00:00
|
|
|
|
return fields[-1]
|
|
|
|
|
fields = id.split('/')
|
|
|
|
|
if len(fields) >= 3:
|
|
|
|
|
itype = fields[-2]
|
|
|
|
|
if type != itype:
|
2016-06-01 00:02:25 +00:00
|
|
|
|
self._warn('expected id of type %s but found type %s %s',
|
|
|
|
|
type, itype, id)
|
2014-08-20 18:04:29 +00:00
|
|
|
|
return fields[-1]
|
|
|
|
|
return id
|
|
|
|
|
|
|
|
|
|
def _get_uri(self, type, id):
|
|
|
|
|
return 'spotify:' + type + ":" + self._get_id(type, id)
|