mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-10-11 01:10:45 +00:00
Merge branch 'master' into patch-1
This commit is contained in:
42 files changed
+801
-526
No files matched your search
+53
-45
@@ -5,7 +5,7 @@ Welcome to Spotipy!
|
||||
===================================
|
||||
*Spotipy* is a lightweight Python library for the `Spotify Web API
|
||||
<https://developer.spotify.com/web-api/>`_. With *Spotipy*
|
||||
you get full access to all of the music data provided by the Spotify platform.
|
||||
you get full access to all of the music data provided by the Spotify platform.
|
||||
|
||||
Here's a quick example of using *Spotipy* to list the names of all the albums
|
||||
released by the artist 'Birdy'::
|
||||
@@ -27,6 +27,7 @@ released by the artist 'Birdy'::
|
||||
Here's another example showing how to get 30 second samples and cover art
|
||||
for the top 10 tracks for Led Zeppelin::
|
||||
|
||||
from __future__ import print_function
|
||||
import spotipy
|
||||
|
||||
lz_uri = 'spotify:artist:36QJpDe2go2KgaRleHCDTp'
|
||||
@@ -35,14 +36,15 @@ for the top 10 tracks for Led Zeppelin::
|
||||
results = spotify.artist_top_tracks(lz_uri)
|
||||
|
||||
for track in results['tracks'][:10]:
|
||||
print 'track : ' + track['name']
|
||||
print 'audio : ' + track['preview_url']
|
||||
print 'cover art: ' + track['album']['images'][0]['url']
|
||||
print
|
||||
print('track : ' + track['name'])
|
||||
print('audio : ' + track['preview_url'])
|
||||
print('cover art: ' + track['album']['images'][0]['url'])
|
||||
print()
|
||||
|
||||
Finally, here's an example that will get the URL for an artist image given the
|
||||
artist's name::
|
||||
|
||||
from __future__ import print_function
|
||||
import spotipy
|
||||
import sys
|
||||
|
||||
@@ -57,7 +59,7 @@ artist's name::
|
||||
items = results['artists']['items']
|
||||
if len(items) > 0:
|
||||
artist = items[0]
|
||||
print artist['name'], artist['images'][0]['url']
|
||||
print(artist['name'], artist['images'][0]['url'])
|
||||
|
||||
|
||||
Features
|
||||
@@ -87,10 +89,11 @@ Non-Authorized requests
|
||||
For methods that do not require authorization, simply create a Spotify object
|
||||
and start making method calls like so::
|
||||
|
||||
from __future__ import print_function
|
||||
import spotipy
|
||||
spotify = spotipy.Spotify()
|
||||
results = spotify.search(q='artist:' + name, type='artist')
|
||||
print results
|
||||
print(results)
|
||||
|
||||
Authorized requests
|
||||
=======================
|
||||
@@ -107,7 +110,7 @@ authenticating your app, you can simply copy the
|
||||
"http://localhost/?code=..." URL from your browser and paste it to the
|
||||
console where your script is running.
|
||||
|
||||
Register your app at
|
||||
Register your app at
|
||||
`My Applications
|
||||
<https://developer.spotify.com/my-applications/#!/applications>`_ and register the
|
||||
redirect URI mentioned in the above paragragh.
|
||||
@@ -115,7 +118,7 @@ redirect URI mentioned in the above paragragh.
|
||||
|
||||
*spotipy* supports two authorization flows:
|
||||
|
||||
- The **Authorization Code flow** This method is suitable for long-running applications
|
||||
- The **Authorization Code flow** This method is suitable for long-running applications
|
||||
which the user logs into once. It provides an access token that can be refreshed.
|
||||
|
||||
- The **Client Credentials flow** The method makes it possible
|
||||
@@ -129,15 +132,16 @@ To support the **Authorization Code Flow** *Spotipy* provides a
|
||||
utility method ``util.prompt_for_user_token`` that will attempt to authorize the
|
||||
user. You can pass your app credentials directly into the method as arguments::
|
||||
|
||||
util.prompt_for_user_token(username,scope,client_id='your-spotify-client-id',client_secret='your-spotify-client-secret',redirect_uri='your-app-redirect-url')
|
||||
|
||||
or if you are reluctant to immortalize your app credentials in your source code,
|
||||
or if you are reluctant to immortalize your app credentials in your source code,
|
||||
you can set environment variables like so::
|
||||
|
||||
export SPOTIPY_CLIENT_ID='your-spotify-client-id'
|
||||
export SPOTIPY_CLIENT_SECRET='your-spotify-client-secret'
|
||||
export SPOTIPY_REDIRECT_URI='your-app-redirect-url'
|
||||
|
||||
Call ``util.prompt_for_user_token`` method with the username and the
|
||||
Call ``util.prompt_for_user_token`` method with the username and the
|
||||
desired scope (see `Using
|
||||
Scopes <https://developer.spotify.com/web-api/using-scopes/>`_ for information
|
||||
about scopes) and credentials. This will coordinate the user authorization via
|
||||
@@ -147,6 +151,7 @@ are used to automatically re-authorized expired tokens.
|
||||
|
||||
Here's an example of getting user authorization to read a user's saved tracks::
|
||||
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
import spotipy
|
||||
import spotipy.util as util
|
||||
@@ -156,7 +161,7 @@ Here's an example of getting user authorization to read a user's saved tracks::
|
||||
if len(sys.argv) > 1:
|
||||
username = sys.argv[1]
|
||||
else:
|
||||
print "Usage: %s username" % (sys.argv[0],)
|
||||
print("Usage: %s username" % (sys.argv[0],))
|
||||
sys.exit()
|
||||
|
||||
token = util.prompt_for_user_token(username, scope)
|
||||
@@ -166,9 +171,9 @@ Here's an example of getting user authorization to read a user's saved tracks::
|
||||
results = sp.current_user_saved_tracks()
|
||||
for item in results['items']:
|
||||
track = item['track']
|
||||
print track['name'] + ' - ' + track['artists'][0]['name']
|
||||
print(track['name'] + ' - ' + track['artists'][0]['name'])
|
||||
else:
|
||||
print "Can't get token for", username
|
||||
print("Can't get token for", username)
|
||||
|
||||
Client Credentials Flow
|
||||
=======================
|
||||
@@ -192,8 +197,8 @@ class SpotifyClientCredentials that can be used to authenticate requests like so
|
||||
playlists = None
|
||||
|
||||
Client credentials flow is appropriate for requests that do not require access to a
|
||||
user's private data. Even if you are only making calls that do not require
|
||||
authorization, using this flow yields the benefit of a higher rate limit
|
||||
user's private data. Even if you are only making calls that do not require
|
||||
authorization, using this flow yields the benefit of a higher rate limit
|
||||
|
||||
IDs URIs and URLs
|
||||
=======================
|
||||
@@ -218,6 +223,7 @@ Here are a few more examples of using *Spotipy*.
|
||||
|
||||
Add tracks to a playlist::
|
||||
|
||||
from __future__ import print_function
|
||||
import pprint
|
||||
import sys
|
||||
|
||||
@@ -229,7 +235,7 @@ Add tracks to a playlist::
|
||||
playlist_id = sys.argv[2]
|
||||
track_ids = sys.argv[3:]
|
||||
else:
|
||||
print "Usage: %s username playlist_id track_id ..." % (sys.argv[0],)
|
||||
print("Usage: %s username playlist_id track_id ..." % (sys.argv[0],))
|
||||
sys.exit()
|
||||
|
||||
scope = 'playlist-modify-public'
|
||||
@@ -239,15 +245,16 @@ Add tracks to a playlist::
|
||||
sp = spotipy.Spotify(auth=token)
|
||||
sp.trace = False
|
||||
results = sp.user_playlist_add_tracks(username, playlist_id, track_ids)
|
||||
print results
|
||||
print(results)
|
||||
else:
|
||||
print "Can't get token for", username
|
||||
print("Can't get token for", username)
|
||||
|
||||
|
||||
Shows the contents of every playlist owned by a user::
|
||||
|
||||
# shows a user's playlists (need to be authenticated via oauth)
|
||||
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
import spotipy
|
||||
import spotipy.util as util
|
||||
@@ -255,16 +262,16 @@ Shows the contents of every playlist owned by a user::
|
||||
def show_tracks(tracks):
|
||||
for i, item in enumerate(tracks['items']):
|
||||
track = item['track']
|
||||
print " %d %32.32s %s" % (i, track['artists'][0]['name'],
|
||||
track['name'])
|
||||
print(" %d %32.32s %s" % (i, track['artists'][0]['name'],
|
||||
track['name']))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if len(sys.argv) > 1:
|
||||
username = sys.argv[1]
|
||||
else:
|
||||
print "Whoops, need your username!"
|
||||
print "usage: python user_playlists.py [username]"
|
||||
print("Whoops, need your username!")
|
||||
print("usage: python user_playlists.py [username]")
|
||||
sys.exit()
|
||||
|
||||
token = util.prompt_for_user_token(username)
|
||||
@@ -274,10 +281,10 @@ Shows the contents of every playlist owned by a user::
|
||||
playlists = sp.user_playlists(username)
|
||||
for playlist in playlists['items']:
|
||||
if playlist['owner']['id'] == username:
|
||||
print
|
||||
print playlist['name']
|
||||
print ' total tracks', playlist['tracks']['total']
|
||||
results = sp.user_playlist(username, playlist['id'],
|
||||
print()
|
||||
print(playlist['name'])
|
||||
print (' total tracks', playlist['tracks']['total'])
|
||||
results = sp.user_playlist(username, playlist['id'],
|
||||
fields="tracks,next")
|
||||
tracks = results['tracks']
|
||||
show_tracks(tracks)
|
||||
@@ -285,7 +292,7 @@ Shows the contents of every playlist owned by a user::
|
||||
tracks = sp.next(tracks)
|
||||
show_tracks(tracks)
|
||||
else:
|
||||
print "Can't get token for", username
|
||||
print("Can't get token for", username)
|
||||
|
||||
|
||||
More Examples
|
||||
@@ -293,7 +300,7 @@ More Examples
|
||||
There are many more examples of how to use *Spotipy* in the `Examples
|
||||
Directory <https://github.com/plamere/spotipy/tree/master/examples>`_ on Github
|
||||
|
||||
API Reference
|
||||
API Reference
|
||||
==============
|
||||
|
||||
:mod:`client` Module
|
||||
@@ -331,7 +338,7 @@ You can ask questions about Spotipy on Stack Overflow. Don’t forget to add t
|
||||
|
||||
http://stackoverflow.com/questions/ask
|
||||
|
||||
If you think you've found a bug, let us know at
|
||||
If you think you've found a bug, let us know at
|
||||
`Spotify Issues <https://github.com/plamere/spotipy/issues>`_
|
||||
|
||||
|
||||
@@ -339,24 +346,25 @@ Contribute
|
||||
==========
|
||||
Spotipy authored by Paul Lamere (plamere) with contributions by:
|
||||
|
||||
- Daniel Beaudry // danbeaudry
|
||||
- Faruk Emre Sahin // fsahin
|
||||
- George // rogueleaderr
|
||||
- Henry Greville // sethaurus
|
||||
- Hugo // hugovk
|
||||
- José Manuel Pérez // JMPerez
|
||||
- Lucas Nunno // lnunno
|
||||
- Lynn Root // econchick
|
||||
- Matt Dennewitz // mattdennewitz
|
||||
- Matthew Duck // mattduck
|
||||
- Michael Thelin // thelinmichael
|
||||
- Ryan Choi // ryankicks
|
||||
- Simon Metson // drsm79
|
||||
- Daniel Beaudry // danbeaudry
|
||||
- Faruk Emre Sahin // fsahin
|
||||
- George // rogueleaderr
|
||||
- Henry Greville // sethaurus
|
||||
- Hugo // hugovk
|
||||
- José Manuel Pérez // JMPerez
|
||||
- Lucas Nunno // lnunno
|
||||
- Lynn Root // econchick
|
||||
- Matt Dennewitz // mattdennewitz
|
||||
- Matthew Duck // mattduck
|
||||
- Michael Thelin // thelinmichael
|
||||
- Ryan Choi // ryankicks
|
||||
- Simon Metson // drsm79
|
||||
- Steve Winton // swinton
|
||||
- Tim Balzer // timbalzer
|
||||
- corycorycory // corycorycory
|
||||
- Tim Balzer // timbalzer
|
||||
- corycorycory // corycorycory
|
||||
- Nathan Coleman // nathancoleman
|
||||
- Michael Birtwell // mbirtwell
|
||||
- Harrison Hayes // Harrison97
|
||||
|
||||
License
|
||||
=======
|
||||
|
||||
Reference in new issue
Block a user