atepper commited on
Commit
7863f26
·
1 Parent(s): d689fb1

successful spotify integration

Browse files
Files changed (1) hide show
  1. app.py +12 -56
app.py CHANGED
@@ -3,24 +3,21 @@ import spotipy
3
  # from spotipy.oauth2 import SpotifyOAuth
4
  from streamlit_oauth import OAuth2Component
5
 
6
- SPOTIFY_SCOPE = "user-read-recently-played"
7
- # REDIRECT_URI = "https://huggingface.co/spaces/atepper/tracklist-analysis"
8
- REDIRECT_URI = "http://localhost:8501/component/streamlit_oauth.authorize_button/index.html"
 
 
 
 
 
9
 
10
- # sp_oauth = SpotifyOAuth(client_id=st.secrets["SPOTIFY_CLIENT_ID"],
11
- # client_secret=st.secrets["SPOTIFY_CLIENT_SECRET"],
12
- # redirect_uri=REDIRECT_URI,
13
- # scope=SPOTIFY_SCOPE)
14
 
15
- oauth2 = OAuth2Component(st.secrets["SPOTIFY_CLIENT_ID"],
16
- st.secrets["SPOTIFY_CLIENT_SECRET"],
17
- "https://accounts.spotify.com/authorize",
18
- "https://accounts.spotify.com/api/token",
19
- "https://accounts.spotify.com/api/token",
20
- "https://accounts.spotify.com/api/token")
21
 
22
  if 'token' not in st.session_state:
23
- result = oauth2.authorize_button("authorize", redirect_uri=REDIRECT_URI, scope=SPOTIFY_SCOPE)
24
  if result and 'token' in result:
25
  # If authorization successful, save token in session state
26
  st.session_state.token = result.get('token')
@@ -36,49 +33,8 @@ else:
36
  st.rerun()
37
 
38
  sp = spotipy.Spotify(token['access_token'])
39
- results = sp.current_user_recently_played(limit=5)
40
 
41
  for idx, item in enumerate(results['items']):
42
  track = item['track']
43
  st.write(f"{idx+1}. {track['name']} by {', '.join([artist['name'] for artist in track['artists']])}")
44
-
45
-
46
-
47
-
48
-
49
- # st.markdown(f'Client ID: {st.secrets["SPOTIFY_CLIENT_ID"]}')
50
- # st.markdown(f'Client Secret: {st.secrets["SPOTIFY_CLIENT_SECRET"]}')
51
-
52
- # if 'auth_token' not in st.session_state:
53
- # st.session_state['auth_token'] = None
54
-
55
- # # Initialize code_used state to ensure the code is processed only once
56
- # if 'code_used' not in st.session_state:
57
- # st.session_state['code_used'] = False
58
-
59
- # if st.session_state['auth_token'] is None and not st.session_state['code_used']:
60
- # # Display login button
61
- # auth_url = sp_oauth.get_authorize_url()
62
- # st.markdown(f'[Log in with Spotify]({auth_url})')
63
-
64
- # # The page will redirect to the redirect URI with a code parameter
65
- # code = st.query_params.get('code')
66
- # if code and not st.session_state['code_used']:
67
- # try:
68
- # # st.write(f"Code: {code}") # Debug line to display the code
69
- # code = code[0] # Get the first item from the list
70
- # token_info = sp_oauth.get_access_token(code, as_dict=False)
71
- # st.session_state['auth_token'] = token_info['access_token']
72
- # st.session_state['code_used'] = True # Mark the code as used
73
- # except spotipy.SpotifyOauthError as e:
74
- # st.error(f"Failed to authenticate: {e}")
75
-
76
- # if st.session_state['auth_token']:
77
- # # We are logged in, display the user's recently played tracks
78
- # # st.markdown(f'auth_token: {st.session_state["auth_token"]}')
79
- # sp = spotipy.Spotify(auth=st.session_state['auth_token'])
80
- # results = sp.current_user_recently_played(limit=5)
81
-
82
- # for idx, item in enumerate(results['items']):
83
- # track = item['track']
84
- # st.write(f"{idx+1}. {track['name']} by {', '.join([artist['name'] for artist in track['artists']])}")
 
3
  # from spotipy.oauth2 import SpotifyOAuth
4
  from streamlit_oauth import OAuth2Component
5
 
6
+ AUTHORIZE_URL = "https://accounts.spotify.com/authorize"
7
+ TOKEN_URL = "https://accounts.spotify.com/api/token"
8
+ REFRESH_TOKEN_URL = TOKEN_URL
9
+ REVOKE_TOKEN_URL = TOKEN_URL
10
+ CLIENT_ID = st.secrets["SPOTIFY_CLIENT_ID"]
11
+ CLIENT_SECRET = st.secrets["SPOTIFY_CLIENT_SECRET"]
12
+ REDIRECT_URI = "http://localhost:8501"
13
+ SCOPE = "user-read-recently-played"
14
 
 
 
 
 
15
 
16
+
17
+ oauth2 = OAuth2Component(CLIENT_ID, CLIENT_SECRET, AUTHORIZE_URL, TOKEN_URL, REFRESH_TOKEN_URL, REVOKE_TOKEN_URL)
 
 
 
 
18
 
19
  if 'token' not in st.session_state:
20
+ result = oauth2.authorize_button("Login with Spotify", REDIRECT_URI, SCOPE)
21
  if result and 'token' in result:
22
  # If authorization successful, save token in session state
23
  st.session_state.token = result.get('token')
 
33
  st.rerun()
34
 
35
  sp = spotipy.Spotify(token['access_token'])
36
+ results = sp.current_user_recently_played(limit=25)
37
 
38
  for idx, item in enumerate(results['items']):
39
  track = item['track']
40
  st.write(f"{idx+1}. {track['name']} by {', '.join([artist['name'] for artist in track['artists']])}")