jisaacso219 commited on
Commit
9c0c6aa
Β·
verified Β·
1 Parent(s): b6b5233

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -17
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,9 +32,10 @@ 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
- # hide everything until we actually get a code
34
  return gr.update(visible=False), gr.update(visible=False, choices=[])
35
  token = sp_oauth.get_access_token(code, as_dict=False)
36
  sp = spotipy.Spotify(auth=token)
@@ -55,7 +60,7 @@ def load_playlist_info(pl_name: str):
55
  return html, gr.update(visible=True)
56
 
57
  def shuffle_and_play(pl_name: str):
58
- pid = user_playlists[pl_name]
59
  tracks, results = [], sp.playlist_tracks(pid)
60
  tracks.extend(results["items"])
61
  while results["next"]:
@@ -66,6 +71,7 @@ def shuffle_and_play(pl_name: str):
66
  if not devices:
67
  return gr.update(value="⚠️ No active device. Open Spotify and try again.", visible=True)
68
  sp.start_playback(device_id=devices[0]["id"], uris=uris)
 
69
  now = sp.current_playback()
70
  if now and now.get("item"):
71
  it = now["item"]
@@ -84,32 +90,54 @@ with gr.Blocks() as demo:
84
  gr.Markdown("## 🎡 RNG Spotify Playlist Shuffler")
85
  gr.Markdown("Login, pick a playlist, then shuffle & playβ€”all in one flow.")
86
 
87
- # Step 1: Login button
88
- login_btn = gr.Button("πŸ” Step 1: Login to Spotify", link=get_auth_url())
 
 
 
 
 
 
 
89
 
90
- # ←–– here’s the hidden Textbox that JS will write into:
91
- code_box = gr.Textbox(visible=False, elem_id="auth_code")
 
92
 
93
- status = gr.Markdown(visible=False)
94
- dropdown = gr.Dropdown(choices=[], label="Step 2: Select a Playlist", visible=False)
95
- info_html = gr.HTML(visible=False)
96
- shuffle_btn= gr.Button("πŸ”€ Step 3: Shuffle & Play", visible=False)
97
- result = gr.HTML(visible=False)
98
 
99
- # On page-load, grab the ?code=… and feed it into `code_box`, then call check_login
100
  demo.load(
101
  fn=check_login,
102
- inputs=[code_box], # single input to match check_login(code)
103
  outputs=[status, dropdown],
104
  js="""
105
- const code = new URLSearchParams(window.location.search).get('code');
106
- return code;
 
 
 
107
  """
108
  )
109
 
110
- dropdown.change(load_playlist_info, [dropdown], [info_html, shuffle_btn])
111
- shuffle_btn.click(shuffle_and_play, [dropdown], [result])
 
 
 
 
 
 
 
 
 
 
 
112
 
113
  if __name__ == "__main__":
114
  demo.launch()
115
 
 
 
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
+ """Called on every page-load via demo.load; shows playlist dropdown once code is present."""
36
  global sp, user_playlists
37
  if not code:
38
+ # still waiting for Spotify redirect
39
  return gr.update(visible=False), gr.update(visible=False, choices=[])
40
  token = sp_oauth.get_access_token(code, as_dict=False)
41
  sp = spotipy.Spotify(auth=token)
 
60
  return html, gr.update(visible=True)
61
 
62
  def shuffle_and_play(pl_name: str):
63
+ pid = user_playlists[pl_name]
64
  tracks, results = [], sp.playlist_tracks(pid)
65
  tracks.extend(results["items"])
66
  while results["next"]:
 
71
  if not devices:
72
  return gr.update(value="⚠️ No active device. Open Spotify and try again.", visible=True)
73
  sp.start_playback(device_id=devices[0]["id"], uris=uris)
74
+
75
  now = sp.current_playback()
76
  if now and now.get("item"):
77
  it = now["item"]
 
90
  gr.Markdown("## 🎡 RNG Spotify Playlist Shuffler")
91
  gr.Markdown("Login, pick a playlist, then shuffle & playβ€”all in one flow.")
92
 
93
+ # 1️⃣ Login button: redirect current tab to Spotify auth
94
+ login_btn = gr.Button("πŸ” Step 1: Login to Spotify")
95
+ login_btn.click(
96
+ fn=None, inputs=None, outputs=None,
97
+ js=f"() => window.location.href = '{get_auth_url()}'"
98
+ )
99
+
100
+ # Hidden Textbox to receive the `code` from URL
101
+ code_box = gr.Textbox(visible=False, elem_id="auth_code")
102
 
103
+ # Status + playlist selector (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
+ # Playlist info + 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: extract `?code=` and call check_login(code)
113
  demo.load(
114
  fn=check_login,
115
+ inputs=[code_box],
116
  outputs=[status, dropdown],
117
  js="""
118
+ () => {
119
+ const params = new URLSearchParams(window.location.search);
120
+ const code = params.get('code') || "";
121
+ return code;
122
+ }
123
  """
124
  )
125
 
126
+ # When a playlist is selected, show its info and reveal the shuffle button
127
+ dropdown.change(
128
+ fn=load_playlist_info,
129
+ inputs=[dropdown],
130
+ outputs=[info_html, shuffle_btn]
131
+ )
132
+
133
+ # Shuffle & play, then display the currently playing track
134
+ shuffle_btn.click(
135
+ fn=shuffle_and_play,
136
+ inputs=[dropdown],
137
+ outputs=[result]
138
+ )
139
 
140
  if __name__ == "__main__":
141
  demo.launch()
142
 
143
+