-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotipy_login_example.py
31 lines (25 loc) · 1004 Bytes
/
spotipy_login_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import sys
import spotipy
import spotipy.util as util
scope = 'user-library-read user-modify-playback-state'
if len(sys.argv) > 1:
username = sys.argv[1]
else:
print("Usage: %s username" % (sys.argv[0],))
sys.exit()
token = util.prompt_for_user_token(username,
scope,
client_id="b8a306fd829d4ea4a757cb1411baf0eb",
client_secret="a00b878607994e4fbcc08cf9c053bd21",
redirect_uri="http://localhost:5000/callback/spotify")
if token:
sp = spotipy.Spotify(auth=token)
results = sp.current_user_saved_tracks()
for item in results['items']:
track = item['track']
print(track['name'] + ' - ' + track['artists'][0]['name'])
results = sp.search(q="polaris masochist")
track_id = results['tracks']['items'][0]['id']
sp.start_playback(uris=['spotify:track:' + track_id])
else:
print("Can't get token for", username)