jisaacso219 commited on
Commit
1a7baef
·
verified ·
1 Parent(s): 0df6c9f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -40
app.py CHANGED
@@ -27,46 +27,60 @@ def get_auth_url():
27
  return sp_oauth.get_authorize_url()
28
 
29
  def try_login(current_url):
30
- global sp, user_playlists, device_map
31
-
32
- if "?code=" not in current_url:
33
- return (
34
- "🔐 Please login to Spotify.",
35
- gr.update(visible=False),
36
- gr.update(visible=False),
37
- gr.update(visible=False),
38
- )
39
-
40
- code = current_url.split("?code=")[1].split("&")[0]
41
- token_info = sp_oauth.get_access_token(code, as_dict=True)
42
- access_token = token_info["access_token"]
43
- sp = spotipy.Spotify(auth=access_token)
44
-
45
- playlists = sp.current_user_playlists(limit=50)["items"]
46
- user_playlists.clear()
47
- for p in playlists:
48
- name = p["name"]
49
- user_playlists[name] = p["id"]
50
-
51
- devices = sp.devices()["devices"]
52
- device_map.clear()
53
- for d in devices:
54
- device_map[d["name"]] = d["id"]
55
-
56
- if not device_map:
57
- return (
58
- "⚠️ No active Spotify devices found.",
59
- gr.update(visible=False),
60
- gr.update(visible=False),
61
- gr.update(visible=False),
62
- )
63
-
64
- return (
65
- "✅ Logged in!",
66
- gr.update(visible=True, choices=list(user_playlists.keys())),
67
- gr.update(visible=True, choices=list(device_map.keys())),
68
- gr.update(visible=True),
69
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
  def background_queue_tracks(uris, device_id):
72
  for i, uri in enumerate(uris):
 
27
  return sp_oauth.get_authorize_url()
28
 
29
  def try_login(current_url):
30
+     global sp, user_playlists, device_map
31
+
32
+     if "?code=" not in current_url:
33
+         return (
34
+             "🔐 Please login to Spotify.",
35
+             gr.update(visible=False),
36
+             gr.update(visible=False),
37
+             gr.update(visible=False),
38
+         )
39
+
40
+     code = current_url.split("?code=")[1].split("&")[0]
41
+     try:
42
+         token_info = sp_oauth.get_access_token(code)
43
+         access_token = token_info["access_token"]
44
+     except Exception as e:
45
+         print("🔁 Token refresh failed:", str(e))
46
+         return (
47
+             f"❌ Failed to log in: {e}",
48
+             gr.update(visible=False),
49
+             gr.update(visible=False),
50
+             gr.update(visible=False),
51
+         )
52
+
53
+     sp = spotipy.Spotify(auth=access_token)
54
+
55
+     try:
56
+         playlists = sp.current_user_playlists(limit=50)["items"]
57
+     except Exception as e:
58
+         return f" Failed to get playlists: {e}", *[gr.update(visible=False)] * 3
59
+
60
+     user_playlists.clear()
61
+     for p in playlists:
62
+         user_playlists[p["name"]] = p["id"]
63
+
64
+     devices = sp.devices()["devices"]
65
+     device_map.clear()
66
+     for d in devices:
67
+         device_map[d["name"]] = d["id"]
68
+
69
+     if not device_map:
70
+         return (
71
+             "⚠️ No active Spotify devices found.",
72
+             gr.update(visible=False),
73
+             gr.update(visible=False),
74
+             gr.update(visible=False),
75
+         )
76
+
77
+     return (
78
+         "✅ Logged in!",
79
+         gr.update(visible=True, choices=list(user_playlists.keys())),
80
+         gr.update(visible=True, choices=list(device_map.keys())),
81
+         gr.update(visible=True),
82
+     )
83
+
84
 
85
  def background_queue_tracks(uris, device_id):
86
  for i, uri in enumerate(uris):