spotipy/examples/long_running.py
Peter 87a72a060f Automatic refresh of Authorization Code Flow Tokens in long-running Applications (#428)
* 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
2020-01-22 20:01:30 +00:00

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)