2014-05-16 15:32:55 +00:00
|
|
|
# shows a user's playlists (need to be authenticated via oauth)
|
|
|
|
|
|
|
|
import pprint
|
|
|
|
import sys
|
2014-05-18 11:08:51 +00:00
|
|
|
import os
|
2014-05-19 11:24:09 +00:00
|
|
|
import subprocess
|
2014-05-16 15:32:55 +00:00
|
|
|
|
|
|
|
import spotipy
|
2014-05-18 11:08:51 +00:00
|
|
|
import spotipy.oauth2 as oauth2
|
2014-05-16 15:32:55 +00:00
|
|
|
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
username = sys.argv[1]
|
|
|
|
else:
|
2014-05-18 11:08:51 +00:00
|
|
|
print "Whoops, need your username!"
|
|
|
|
print "usage: python user_playlists.py [username]"
|
2014-05-16 15:32:55 +00:00
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
# Create these via developer.spotify.com/my-applications
|
|
|
|
|
2014-05-20 12:30:48 +00:00
|
|
|
client_id = os.getenv('CLIENT_ID', 'YOUR_CLIENT_ID')
|
|
|
|
client_secret = os.getenv('CLIENT_SECRET', 'YOUR_CLIENT_SECRET')
|
|
|
|
redirect_uri = os.getenv('REDIRECT_URI', 'YOUR_REDIRECT_URI')
|
2014-05-18 11:08:51 +00:00
|
|
|
|
|
|
|
print 'client_id', client_id
|
|
|
|
print 'client_secret', client_secret
|
|
|
|
print 'redirect_uri', redirect_uri
|
2014-05-16 15:32:55 +00:00
|
|
|
|
2014-05-20 12:30:48 +00:00
|
|
|
sp_oauth = oauth2.SpotifyOAuth(client_id, client_secret, redirect_uri, cache_path=username)
|
2014-05-19 11:24:09 +00:00
|
|
|
|
2014-05-20 12:30:48 +00:00
|
|
|
|
|
|
|
# try to get a valid token for this user, from the cache,
|
|
|
|
# if not in the cache, the create a new (this will send
|
|
|
|
# the user to a web page where they can authorize this app)
|
|
|
|
|
|
|
|
token_info = sp_oauth.get_cached_token()
|
|
|
|
|
|
|
|
if not token_info:
|
|
|
|
auth_url = sp_oauth.get_authorize_url()
|
|
|
|
try:
|
|
|
|
subprocess.call(["open", auth_url])
|
|
|
|
print "Opening %s in your browser" % auth_url
|
|
|
|
except:
|
|
|
|
print "Please navigate here: %s" % auth_url
|
|
|
|
response = raw_input("The URL you were redirected to: ")
|
|
|
|
code = sp_oauth.parse_response_code(response)
|
|
|
|
token_info = sp_oauth.get_access_token(code)
|
2014-05-16 15:32:55 +00:00
|
|
|
|
|
|
|
# Auth'ed API request
|
2014-05-20 12:30:48 +00:00
|
|
|
sp = spotipy.Spotify(auth=token_info['access_token'])
|
2014-05-16 15:32:55 +00:00
|
|
|
|
|
|
|
playlists = sp.user_playlists(username)
|
2014-05-20 12:30:48 +00:00
|
|
|
for playlist in playlists['items']:
|
|
|
|
print playlist['name']
|