jisaacso219 commited on
Commit
48b24f7
Β·
verified Β·
1 Parent(s): 2f35dbb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -30
app.py CHANGED
@@ -24,20 +24,27 @@ devices_dict = {}
24
  def get_auth_url():
25
  return sp_oauth.get_authorize_url()
26
 
27
- def complete_login(full_url):
28
  global sp, user_playlists, devices_dict
29
 
 
 
 
 
 
 
 
30
  try:
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
  )
38
 
39
- token = sp_oauth.get_access_token(code, as_dict=True)
40
- access_token = token["access_token"]
41
  sp = spotipy.Spotify(auth=access_token)
42
 
43
  playlists = sp.current_user_playlists(limit=50)["items"]
@@ -48,13 +55,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."),
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
  )
@@ -90,13 +97,13 @@ with gr.Blocks() as demo:
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,
98
- inputs=[redirect_url_box],
99
- outputs=[status, playlist_dd, device_dd]
 
100
  )
101
 
102
  playlist_dd.change(lambda: gr.update(visible=True), None, shuffle_btn)
@@ -107,23 +114,6 @@ with gr.Blocks() as demo:
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__":
128
  demo.launch(server_name="0.0.0.0", ssr_mode=False)
129
 
 
24
  def get_auth_url():
25
  return sp_oauth.get_authorize_url()
26
 
27
+ def try_login(full_url):
28
  global sp, user_playlists, devices_dict
29
 
30
+ if "?code=" not in full_url:
31
+ return (
32
+ gr.update(value="πŸ” Please login via Spotify to continue."),
33
+ gr.update(visible=False),
34
+ gr.update(visible=False),
35
+ )
36
+
37
  try:
38
  code = full_url.split("?code=")[1].split("&")[0]
39
+ token = sp_oauth.get_access_token(code, as_dict=True)
40
+ access_token = token["access_token"]
41
+ except Exception as e:
42
  return (
43
+ gr.update(value=f"❌ OAuth failed: {str(e)}. Please log in again."),
44
  gr.update(visible=False),
45
  gr.update(visible=False),
46
  )
47
 
 
 
48
  sp = spotipy.Spotify(auth=access_token)
49
 
50
  playlists = sp.current_user_playlists(limit=50)["items"]
 
55
 
56
  if not devices_dict:
57
  return (
58
+ gr.update(value="⚠️ No active Spotify devices found. Open Spotify app."),
59
  gr.update(visible=False),
60
  gr.update(visible=False),
61
  )
62
 
63
  return (
64
+ gr.update(value="βœ… Logged in! Select playlist and device."),
65
  gr.update(visible=True, choices=list(user_playlists.keys())),
66
  gr.update(visible=True, choices=list(devices_dict.keys())),
67
  )
 
97
  shuffle_btn = gr.Button("πŸ”€ Shuffle & Play", visible=False)
98
  result = gr.Markdown()
99
 
100
+ url_state = gr.Textbox(visible=False)
 
101
 
102
+ demo.load(
103
+ try_login,
104
+ inputs=[url_state],
105
+ outputs=[status, playlist_dd, device_dd],
106
+ js="() => window.location.href"
107
  )
108
 
109
  playlist_dd.change(lambda: gr.update(visible=True), None, shuffle_btn)
 
114
  outputs=[result]
115
  )
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  if __name__ == "__main__":
118
  demo.launch(server_name="0.0.0.0", ssr_mode=False)
119