mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-06-19 09:13:53 +00:00
* auto-refresh user token * example for a long-running user-request app * wrap long lines * combine duplicate code into _refresh_token_if_expired method * add changelog entry
43 lines
981 B
Python
43 lines
981 B
Python
# long running example (need to be authenticated via oauth)
|
|
|
|
import sys
|
|
import time
|
|
from datetime import datetime
|
|
|
|
import spotipy
|
|
import spotipy.util as util
|
|
|
|
if len(sys.argv) > 1:
|
|
username = sys.argv[1]
|
|
else:
|
|
print("Whoops, need your username!")
|
|
print("usage: python user_playlists.py [username]")
|
|
sys.exit()
|
|
|
|
SCOPE = ','.join([
|
|
'user-modify-playback-state',
|
|
'user-read-playback-state',
|
|
'user-read-currently-playing'
|
|
])
|
|
|
|
oauth = util.prompt_for_user_token(
|
|
username,
|
|
redirect_uri="http://localhost/",
|
|
scope=SCOPE)
|
|
|
|
if oauth:
|
|
sp = spotipy.Spotify(auth=oauth)
|
|
# sp.trace = True
|
|
# sp.trace_out = True
|
|
start = datetime.now()
|
|
while True:
|
|
current_playback = sp.current_playback()
|
|
print(datetime.now() - start,
|
|
"is_playing?",
|
|
None if current_playback is None
|
|
else current_playback['is_playing'])
|
|
|
|
time.sleep(60)
|
|
else:
|
|
print("Can't get token for", username)
|