atepper commited on
Commit
d689fb1
·
1 Parent(s): 281748f

basic spotify integration

Browse files
Files changed (2) hide show
  1. app.py +72 -27
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,39 +1,84 @@
1
  import streamlit as st
2
  import spotipy
3
- from spotipy.oauth2 import SpotifyOAuth
 
4
 
5
  SPOTIFY_SCOPE = "user-read-recently-played"
6
- REDIRECT_URI = "https://huggingface.co/spaces/atepper/tracklist-analysis"
 
7
 
8
- sp_oauth = SpotifyOAuth(client_id=st.secrets["SPOTIFY_CLIENT_ID"],
9
- client_secret=st.secrets["SPOTIFY_CLIENT_SECRET"],
10
- redirect_uri=REDIRECT_URI,
11
- scope=SPOTIFY_SCOPE)
12
 
13
- # st.markdown(f'Client ID: {st.secrets["SPOTIFY_CLIENT_ID"]}')
14
- # st.markdown(f'Client Secret: {st.secrets["SPOTIFY_CLIENT_SECRET"]}')
15
-
16
- if 'auth_token' not in st.session_state:
17
- st.session_state['auth_token'] = None
 
18
 
19
- if st.session_state['auth_token'] is None:
20
- # Display login button
21
- auth_url = sp_oauth.get_authorize_url()
22
- st.markdown(f'[Log in with Spotify]({auth_url})')
23
-
24
- # The page will redirect to the redirect URI with a code parameter
25
- code = st.query_params.get('code')
26
- if code:
27
- # st.write(f"Code: {code}") # Debug line to display the code
28
- code = code[0] # Get the first item from the list
29
- token_info = sp_oauth.get_access_token(code, as_dict=False)
30
- st.session_state['auth_token'] = token_info['access_token']
31
  else:
32
- # We are logged in, display the user's recently played tracks
33
- st.markdown(f'auth_token: {st.session_state["auth_token"]}')
34
- sp = spotipy.Spotify(auth=st.session_state['auth_token'])
35
- results = sp.current_user_recently_played(limit=5)
 
 
 
 
36
 
 
 
 
37
  for idx, item in enumerate(results['items']):
38
  track = item['track']
39
  st.write(f"{idx+1}. {track['name']} by {', '.join([artist['name'] for artist in track['artists']])}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import streamlit as st
2
  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')
27
+ st.rerun()
 
 
 
 
 
 
28
  else:
29
+ # If token exists in session state, show the token
30
+ token = st.session_state['token']
31
+ st.json(token)
32
+ if st.button("Refresh Token"):
33
+ # If refresh token button is clicked, refresh the token
34
+ token = oauth2.refresh_token(token)
35
+ st.session_state.token = token
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']])}")
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  streamlit
2
- spotipy
 
 
1
  streamlit
2
+ spotipy
3
+ streamlit-oauth