-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
60 lines (46 loc) · 1.71 KB
/
main.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
from flask import Flask, session, redirect, url_for, request
from spotipy import Spotify
from spotipy.oauth2 import SpotifyOAuth
from spotipy.cache_handler import FlaskSessionCacheHandler
app = Flask(__name__)
app.config['SECRET_KEY'] = os.urandom(64)
client_id = 'f1df06fa6dcf4da08d2393fdb7042eba'
client_secret = '683a0521431d4d39ae93a87aa1c60a21'
redirect_uri = 'http://localhost:5000/callback'
scope = 'playlist-read-private'
cache_handler = FlaskSessionCacheHandler(session)
sp_oauth = SpotifyOAuth(
client_id=client_id,
client_secret=client_secret,
redirect_uri=redirect_uri,
scope=scope,
cache_handler=cache_handler,
show_dialog=True
)
sp = Spotify(oauth_manager=sp_oauth)
@app.route('/')
def home():
if not sp_oauth.validate_token(cache_handler.get_cached_token()):
auth_url = sp_oauth.get_authorize_url()
return redirect(auth_url)
return redirect(url_for('get_playlists'))
@app.route('/callback')
def callback():
sp_oauth.get_access_token(request.args['code'])
return redirect(url_for('get_playlists'))
@app.route('/get_playlists')
def get_playlists():
if not sp_oauth.validate_token(cache_handler.get_cached_token()):
auth_url = sp_oauth.get_authorize_url()
return redirect(auth_url)
playlists = sp.current_user_playlists()
playlists_info = [(pl['name'], pl['external_urls']['spotify']) for pl in playlists['items']]
playlists_html = '<br>'.join([f'{name}: {url}' for name, url, in playlists_info])
return playlists_html
@app.route('/logout')
def logout():
session.clear()
return redirect(url_for('home'))
if __name__ == '__main__':
app.run(debug=True)