jisaacso219 commited on
Commit
34ae2ff
Β·
verified Β·
1 Parent(s): 6c734a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -21
app.py CHANGED
@@ -4,7 +4,11 @@ import spotipy
4
  from spotipy.oauth2 import SpotifyOAuth
5
  import random
6
 
 
 
7
  REDIRECT_URI = "https://jisaacso219-rng-shuffle.hf.space/"
 
 
8
  CLIENT_ID = os.environ["SPOTIFY_CLIENT_ID"]
9
  CLIENT_SECRET = os.environ["SPOTIFY_CLIENT_SECRET"]
10
  SCOPE = (
@@ -28,16 +32,21 @@ def get_auth_url():
28
  return sp_oauth.get_authorize_url()
29
 
30
  def check_login(code: str):
 
31
  global sp, user_playlists
 
32
  if not code:
33
  return gr.update(visible=False), gr.update(visible=False, choices=[])
 
 
34
  token = sp_oauth.get_access_token(code, as_dict=False)
35
  sp = spotipy.Spotify(auth=token)
36
  pls = sp.current_user_playlists(limit=50)["items"]
37
  user_playlists = {p["name"]: p["id"] for p in pls}
 
38
  return (
39
  gr.update(visible=True, value="βœ… Logged in! Choose a playlist below."),
40
- gr.update(visible=True, choices=list(user_playlists.keys()))
41
  )
42
 
43
  def load_playlist_info(pl_name: str):
@@ -85,44 +94,40 @@ with gr.Blocks() as demo:
85
  gr.Markdown("## 🎡 RNG Spotify Playlist Shuffler")
86
  gr.Markdown("Login, pick a playlist, then shuffle & playβ€”all in one flow.")
87
 
88
- # Step 1: Login button using `link=` (Gradio β‰₯5.14.0)
89
  login_btn = gr.Button(
90
  "πŸ” Step 1: Login to Spotify",
91
  link=get_auth_url()
92
  )
93
 
94
- # Hidden container for the ?code=… Spotify will append on redirect
95
- code_box = gr.Textbox(visible=False, elem_id="auth_code")
96
- status = gr.Markdown(visible=False)
97
- dropdown = gr.Dropdown(choices=[], label="Step 2: Select a Playlist", visible=False)
98
- info_html = gr.HTML(visible=False)
 
99
  shuffle_btn = gr.Button("πŸ”€ Step 3: Shuffle & Play", visible=False)
100
  result = gr.HTML(visible=False)
101
 
102
- # Run on every page-load: grab ?code=… via JS and call check_login
103
  demo.load(
104
  fn=check_login,
105
- inputs=[code_box],
106
  outputs=[status, dropdown],
107
- js="""
108
- const params = new URLSearchParams(window.location.search);
109
- const code = params.get("code");
110
- if (code) {
111
- document
112
- .getElementById("component-auth_code")
113
- .querySelector("textarea").value = code;
114
- }
115
- """
116
  )
117
 
118
- # When user picks a playlist, show its artwork/info and reveal the shuffle button
119
  dropdown.change(
120
  fn=load_playlist_info,
121
  inputs=[dropdown],
122
  outputs=[info_html, shuffle_btn]
123
  )
124
 
125
- # Shuffle & start playback, then show track info
126
  shuffle_btn.click(
127
  fn=shuffle_and_play,
128
  inputs=[dropdown],
@@ -131,4 +136,3 @@ with gr.Blocks() as demo:
131
 
132
  if __name__ == "__main__":
133
  demo.launch()
134
-
 
4
  from spotipy.oauth2 import SpotifyOAuth
5
  import random
6
 
7
+ # β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”β€”
8
+ # 1. Hard-code your Redirect URI here:
9
  REDIRECT_URI = "https://jisaacso219-rng-shuffle.hf.space/"
10
+
11
+ # 2. Keep only your client creds in Secrets:
12
  CLIENT_ID = os.environ["SPOTIFY_CLIENT_ID"]
13
  CLIENT_SECRET = os.environ["SPOTIFY_CLIENT_SECRET"]
14
  SCOPE = (
 
32
  return sp_oauth.get_authorize_url()
33
 
34
  def check_login(code: str):
35
+ """Receives the `code` directly from the page-load JS."""
36
  global sp, user_playlists
37
+ # No code β†’ hide everything until they come back from Spotify
38
  if not code:
39
  return gr.update(visible=False), gr.update(visible=False, choices=[])
40
+
41
+ # Exchange for token, fetch playlists
42
  token = sp_oauth.get_access_token(code, as_dict=False)
43
  sp = spotipy.Spotify(auth=token)
44
  pls = sp.current_user_playlists(limit=50)["items"]
45
  user_playlists = {p["name"]: p["id"] for p in pls}
46
+
47
  return (
48
  gr.update(visible=True, value="βœ… Logged in! Choose a playlist below."),
49
+ gr.update(visible=True, choices=list(user_playlists.keys())),
50
  )
51
 
52
  def load_playlist_info(pl_name: str):
 
94
  gr.Markdown("## 🎡 RNG Spotify Playlist Shuffler")
95
  gr.Markdown("Login, pick a playlist, then shuffle & playβ€”all in one flow.")
96
 
97
+ # 1️⃣ Login button
98
  login_btn = gr.Button(
99
  "πŸ” Step 1: Login to Spotify",
100
  link=get_auth_url()
101
  )
102
 
103
+ # 2️⃣ Status message & playlist dropdown (hidden until login completes)
104
+ status = gr.Markdown(visible=False)
105
+ dropdown = gr.Dropdown(choices=[], label="Step 2: Select a Playlist", visible=False)
106
+
107
+ # 3️⃣ Show playlist info, then shuffle controls
108
+ info_html = gr.HTML(visible=False)
109
  shuffle_btn = gr.Button("πŸ”€ Step 3: Shuffle & Play", visible=False)
110
  result = gr.HTML(visible=False)
111
 
112
+ # πŸ”„ On every page-load: grab `?code=…` and hand it to check_login()
113
  demo.load(
114
  fn=check_login,
115
+ inputs=[], # no Components are bound client→server
116
  outputs=[status, dropdown],
117
+ js="""() => {
118
+ const code = new URLSearchParams(window.location.search).get('code');
119
+ return code;
120
+ }"""
 
 
 
 
 
121
  )
122
 
123
+ # When they pick a playlist, show its artwork/info and reveal β€œShuffle”
124
  dropdown.change(
125
  fn=load_playlist_info,
126
  inputs=[dropdown],
127
  outputs=[info_html, shuffle_btn]
128
  )
129
 
130
+ # Shuffle & play, then display currently playing track
131
  shuffle_btn.click(
132
  fn=shuffle_and_play,
133
  inputs=[dropdown],
 
136
 
137
  if __name__ == "__main__":
138
  demo.launch()