Spaces:
Running
Running
Update app.py
Browse files
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 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
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):
|