mirror of
https://github.com/spotipy-dev/spotipy.git
synced 2026-06-19 09:13:53 +00:00
A few updates to the example app (#540)
* No username needed + can be used by multiple users * cleanup * Remove caches when signing out * Persist token only once * Always show auth page * Add one scope.. * Add a second scope and also a view which utilizes a scope. * Add a __main__ clause example. * Include further documentation. * Move utility function higher in document. * Add threading to application run call in __main__. * Add (or reinstate–I thought it had been there) the "me" view. * Add notes about SPOTIPY_REDIRECT_URI and using alternate port. And specify that it should be the address that the App runs over. * Whitespace only. * Here is an example of importing credentials. * Delete session_cache file on logout so new user can Authorize. * Not going to use at this point. * Update the "me" route method to use new approach to auth. * Update app.py Co-authored-by: Stephane Bruckert <contact@stephanebruckert.com> Co-authored-by: Stephane Bruckert <stephane.bruckert@gmail.com>
This commit is contained in:
parent
d211a33618
commit
004df7b2df
@ -7,12 +7,20 @@ Prerequisites
|
|||||||
export SPOTIPY_CLIENT_ID=client_id_here
|
export SPOTIPY_CLIENT_ID=client_id_here
|
||||||
export SPOTIPY_CLIENT_SECRET=client_secret_here
|
export SPOTIPY_CLIENT_SECRET=client_secret_here
|
||||||
export SPOTIPY_REDIRECT_URI='http://127.0.0.1:8080' // must contain a port
|
export SPOTIPY_REDIRECT_URI='http://127.0.0.1:8080' // must contain a port
|
||||||
|
// SPOTIPY_REDIRECT_URI must be added to your [app settings](https://developer.spotify.com/dashboard/applications)
|
||||||
|
OPTIONAL
|
||||||
|
// in development environment for debug output
|
||||||
|
export FLASK_ENV=development
|
||||||
|
// so that you can invoke the app outside of the file's directory include
|
||||||
|
export FLASK_APP=/path/to/spotipy/examples/app.py
|
||||||
|
|
||||||
// on Windows, use `SET` instead of `export`
|
// on Windows, use `SET` instead of `export`
|
||||||
|
|
||||||
Run app.py
|
Run app.py
|
||||||
|
|
||||||
python3 -m flask run --port=8080
|
python3 -m flask run --port=8080
|
||||||
|
NOTE: If receiving "port already in use" error, try other ports: 5000, 8090, 8888, etc...
|
||||||
|
(will need to be updated in your Spotify app and SPOTIPY_REDIRECT_URI variable)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
@ -31,6 +39,8 @@ caches_folder = './.spotify_caches/'
|
|||||||
if not os.path.exists(caches_folder):
|
if not os.path.exists(caches_folder):
|
||||||
os.makedirs(caches_folder)
|
os.makedirs(caches_folder)
|
||||||
|
|
||||||
|
def session_cache_path():
|
||||||
|
return caches_folder + session.get('uuid')
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def index():
|
def index():
|
||||||
@ -38,7 +48,10 @@ def index():
|
|||||||
# Step 1. Visitor is unknown, give random ID
|
# Step 1. Visitor is unknown, give random ID
|
||||||
session['uuid'] = str(uuid.uuid4())
|
session['uuid'] = str(uuid.uuid4())
|
||||||
|
|
||||||
auth_manager = spotipy.oauth2.SpotifyOAuth(cache_path=session_cache_path(), show_dialog=True)
|
auth_manager = spotipy.oauth2.SpotifyOAuth(scope='user-read-currently-playing playlist-modify-private',
|
||||||
|
cache_path=session_cache_path(),
|
||||||
|
show_dialog=True)
|
||||||
|
|
||||||
if request.args.get("code"):
|
if request.args.get("code"):
|
||||||
# Step 3. Being redirected from Spotify auth page
|
# Step 3. Being redirected from Spotify auth page
|
||||||
auth_manager.get_access_token(request.args.get("code"))
|
auth_manager.get_access_token(request.args.get("code"))
|
||||||
@ -53,20 +66,26 @@ def index():
|
|||||||
spotify = spotipy.Spotify(auth_manager=auth_manager)
|
spotify = spotipy.Spotify(auth_manager=auth_manager)
|
||||||
return f'<h2>Hi {spotify.me()["display_name"]}, ' \
|
return f'<h2>Hi {spotify.me()["display_name"]}, ' \
|
||||||
f'<small><a href="/sign_out">[sign out]<a/></small></h2>' \
|
f'<small><a href="/sign_out">[sign out]<a/></small></h2>' \
|
||||||
f'<a href="/playlists">my playlists</a>'
|
f'<a href="/playlists">my playlists</a> | ' \
|
||||||
|
f'<a href="/currently_playing">currently playing</a> | ' \
|
||||||
|
f'<a href="/current_user">me</a>' \
|
||||||
|
|
||||||
|
|
||||||
@app.route('/sign_out')
|
@app.route('/sign_out')
|
||||||
def sign_out():
|
def sign_out():
|
||||||
os.remove(session_cache_path())
|
os.remove(session_cache_path())
|
||||||
session.clear()
|
session.clear()
|
||||||
|
try:
|
||||||
|
# Remove the CACHE file (.cache-test) so that a new user can authorize.
|
||||||
|
os.remove(session_cache_path())
|
||||||
|
except OSError as e:
|
||||||
|
print ("Error: %s - %s." % (e.filename, e.strerror))
|
||||||
return redirect('/')
|
return redirect('/')
|
||||||
|
|
||||||
|
|
||||||
@app.route('/playlists')
|
@app.route('/playlists')
|
||||||
def playlists():
|
def playlists():
|
||||||
auth_manager = spotipy.oauth2.SpotifyOAuth(cache_path=session_cache_path())
|
auth_manager = spotipy.oauth2.SpotifyOAuth(cache_path=session_cache_path())
|
||||||
|
|
||||||
if not auth_manager.get_cached_token():
|
if not auth_manager.get_cached_token():
|
||||||
return redirect('/')
|
return redirect('/')
|
||||||
|
|
||||||
@ -74,5 +93,31 @@ def playlists():
|
|||||||
return spotify.current_user_playlists()
|
return spotify.current_user_playlists()
|
||||||
|
|
||||||
|
|
||||||
def session_cache_path():
|
@app.route('/currently_playing')
|
||||||
return caches_folder + session.get('uuid')
|
def currently_playing():
|
||||||
|
auth_manager = spotipy.oauth2.SpotifyOAuth(cache_path=session_cache_path())
|
||||||
|
if not auth_manager.get_cached_token():
|
||||||
|
return redirect('/')
|
||||||
|
spotify = spotipy.Spotify(auth_manager=auth_manager)
|
||||||
|
track = spotify.current_user_playing_track()
|
||||||
|
if not track is None:
|
||||||
|
return track
|
||||||
|
return "No track currently playing."
|
||||||
|
|
||||||
|
|
||||||
|
@app.route('/current_user')
|
||||||
|
def current_user():
|
||||||
|
auth_manager = spotipy.oauth2.SpotifyOAuth(cache_path=session_cache_path())
|
||||||
|
if not auth_manager.get_cached_token():
|
||||||
|
return redirect('/')
|
||||||
|
spotify = spotipy.Spotify(auth_manager=auth_manager)
|
||||||
|
return spotify.current_user()
|
||||||
|
|
||||||
|
|
||||||
|
'''
|
||||||
|
Following lines allow application to be run more conveniently with
|
||||||
|
`python app.py` (Make sure you're using python3)
|
||||||
|
(Also includes directive to leverage pythons threading capacity.)
|
||||||
|
'''
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app.run(threaded=True, port=int(os.environ.get("PORT", 8080)))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user