jisaacso219 commited on
Commit
2f35dbb
Β·
verified Β·
1 Parent(s): 03438b7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -18
app.py CHANGED
@@ -31,7 +31,7 @@ def complete_login(full_url):
31
  code = full_url.split("?code=")[1].split("&")[0]
32
  except IndexError:
33
  return (
34
- gr.update(value="❌ OAuth code not found. Try logging in again."),
35
  gr.update(visible=False),
36
  gr.update(visible=False),
37
  )
@@ -48,13 +48,13 @@ def complete_login(full_url):
48
 
49
  if not devices_dict:
50
  return (
51
- gr.update(value="⚠️ No active Spotify devices found. Open Spotify app."),
52
  gr.update(visible=False),
53
  gr.update(visible=False),
54
  )
55
 
56
  return (
57
- gr.update(value="βœ… Logged in! Select a playlist and playback device."),
58
  gr.update(visible=True, choices=list(user_playlists.keys())),
59
  gr.update(visible=True, choices=list(devices_dict.keys())),
60
  )
@@ -79,9 +79,8 @@ def shuffle_and_play(playlist_name, device_name):
79
  with gr.Blocks() as demo:
80
  gr.Markdown("## 🎡 RNG Spotify Playlist Shuffler")
81
 
82
- login_url = get_auth_url()
83
  login_btn = gr.HTML(
84
- f'<a href="{login_url}"><button style="width:100%; height:40px; font-size:16px;">πŸ” Login to Spotify</button></a>'
85
  )
86
 
87
  status = gr.Markdown()
@@ -91,10 +90,8 @@ with gr.Blocks() as demo:
91
  shuffle_btn = gr.Button("πŸ”€ Shuffle & Play", visible=False)
92
  result = gr.Markdown()
93
 
94
- # Hidden textbox to automatically capture redirected URL
95
- redirect_url_box = gr.Textbox(visible=False)
96
-
97
- complete_login_btn = gr.Button(visible=False)
98
 
99
  complete_login_btn.click(
100
  complete_login,
@@ -110,18 +107,21 @@ with gr.Blocks() as demo:
110
  outputs=[result]
111
  )
112
 
113
- # Automatic URL handling (no manual paste needed!)
114
  demo.load(None, None, None, js="""
115
- () => {
116
- const params = new URLSearchParams(window.location.search);
117
- const code = params.get('code');
118
- if(code) {
119
- const box = document.querySelector('#component-redirect_url_box textarea');
120
- if(box){ box.value = window.location.href; }
121
- const button = document.querySelector('#component-complete_login_btn');
122
- if(button){ button.click(); }
 
 
123
  }
124
  }
 
125
  """)
126
 
127
  if __name__ == "__main__":
 
31
  code = full_url.split("?code=")[1].split("&")[0]
32
  except IndexError:
33
  return (
34
+ gr.update(value="❌ OAuth code not found. Please try again."),
35
  gr.update(visible=False),
36
  gr.update(visible=False),
37
  )
 
48
 
49
  if not devices_dict:
50
  return (
51
+ gr.update(value="⚠️ No active Spotify devices found. Open Spotify."),
52
  gr.update(visible=False),
53
  gr.update(visible=False),
54
  )
55
 
56
  return (
57
+ gr.update(value="βœ… Logged in! Choose playlist & device."),
58
  gr.update(visible=True, choices=list(user_playlists.keys())),
59
  gr.update(visible=True, choices=list(devices_dict.keys())),
60
  )
 
79
  with gr.Blocks() as demo:
80
  gr.Markdown("## 🎡 RNG Spotify Playlist Shuffler")
81
 
 
82
  login_btn = gr.HTML(
83
+ f'<a href="{get_auth_url()}"><button style="width:100%;height:40px;font-size:16px;">πŸ” Login to Spotify</button></a>'
84
  )
85
 
86
  status = gr.Markdown()
 
90
  shuffle_btn = gr.Button("πŸ”€ Shuffle & Play", visible=False)
91
  result = gr.Markdown()
92
 
93
+ redirect_url_box = gr.Textbox(visible=False, elem_id="redirect_url_box")
94
+ complete_login_btn = gr.Button(visible=False, elem_id="complete_login_btn")
 
 
95
 
96
  complete_login_btn.click(
97
  complete_login,
 
107
  outputs=[result]
108
  )
109
 
110
+ # Correct automation JavaScript snippet:
111
  demo.load(None, None, None, js="""
112
+ () => {
113
+ const params = new URLSearchParams(window.location.search);
114
+ const code = params.get('code');
115
+ if (code) {
116
+ const box = document.querySelector("#redirect_url_box textarea");
117
+ const btn = document.querySelector("#complete_login_btn button");
118
+ if (box && btn) {
119
+ box.value = window.location.href;
120
+ box.dispatchEvent(new Event('input', { bubbles: true }));
121
+ btn.click();
122
  }
123
  }
124
+ }
125
  """)
126
 
127
  if __name__ == "__main__":