Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -29,24 +29,29 @@ sp = None
|
|
29 |
user_playlists = {}
|
30 |
|
31 |
def get_auth_url():
|
32 |
-
|
|
|
|
|
33 |
|
34 |
def check_login(code: str):
|
|
|
35 |
global sp, user_playlists
|
|
|
36 |
if not code:
|
37 |
return gr.update(visible=False), gr.update(visible=False, choices=[])
|
38 |
-
|
39 |
token = sp_oauth.get_access_token(code, as_dict=False)
|
40 |
sp = spotipy.Spotify(auth=token)
|
41 |
pls = sp.current_user_playlists(limit=50)["items"]
|
42 |
user_playlists = {p["name"]: p["id"] for p in pls}
|
43 |
-
|
44 |
return (
|
45 |
gr.update(visible=True, value="β
Logged in! Choose a playlist below."),
|
46 |
gr.update(visible=True, choices=list(user_playlists.keys())),
|
47 |
)
|
48 |
|
49 |
def load_playlist_info(pl_name: str):
|
|
|
50 |
pid = user_playlists[pl_name]
|
51 |
data = sp.playlist(pid)
|
52 |
img = data["images"][0]["url"] if data["images"] else ""
|
@@ -60,20 +65,19 @@ def load_playlist_info(pl_name: str):
|
|
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"]:
|
67 |
results = sp.next(results)
|
68 |
tracks.extend(results["items"])
|
69 |
-
|
70 |
uris = [t["track"]["uri"] for t in tracks if t["track"]]
|
71 |
devices = sp.devices()["devices"]
|
72 |
if not devices:
|
|
|
73 |
return gr.update(value="β οΈ No active device. Open Spotify and try again.", visible=True)
|
74 |
-
|
75 |
sp.start_playback(device_id=devices[0]["id"], uris=uris)
|
76 |
-
|
77 |
now = sp.current_playback()
|
78 |
if now and now.get("item"):
|
79 |
it = now["item"]
|
@@ -96,7 +100,12 @@ with gr.Blocks() as demo:
|
|
96 |
login_btn = gr.Button("π Step 1: Login to Spotify")
|
97 |
login_btn.click(
|
98 |
fn=None, inputs=None, outputs=None,
|
99 |
-
js=f"
|
|
|
|
|
|
|
|
|
|
|
100 |
)
|
101 |
|
102 |
# Hidden box to capture the `code` after redirect
|
@@ -117,21 +126,22 @@ with gr.Blocks() as demo:
|
|
117 |
inputs=[code_box],
|
118 |
outputs=[status, dropdown],
|
119 |
js="""
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
|
|
|
|
|
|
124 |
"""
|
125 |
)
|
126 |
|
127 |
-
# When a playlist is chosen, show its artwork/info and reveal βShuffleβ
|
128 |
dropdown.change(
|
129 |
fn=load_playlist_info,
|
130 |
inputs=[dropdown],
|
131 |
outputs=[info_html, shuffle_btn]
|
132 |
)
|
133 |
|
134 |
-
# Shuffle & start playback, then display the now-playing track
|
135 |
shuffle_btn.click(
|
136 |
fn=shuffle_and_play,
|
137 |
inputs=[dropdown],
|
@@ -141,4 +151,3 @@ with gr.Blocks() as demo:
|
|
141 |
if __name__ == "__main__":
|
142 |
demo.launch()
|
143 |
|
144 |
-
|
|
|
29 |
user_playlists = {}
|
30 |
|
31 |
def get_auth_url():
|
32 |
+
url = sp_oauth.get_authorize_url()
|
33 |
+
print(f"[DEBUG] Generated auth URL: {url}")
|
34 |
+
return url
|
35 |
|
36 |
def check_login(code: str):
|
37 |
+
print(f"[DEBUG] check_login called with code: {code}")
|
38 |
global sp, user_playlists
|
39 |
+
# Hide controls if no code yet
|
40 |
if not code:
|
41 |
return gr.update(visible=False), gr.update(visible=False, choices=[])
|
42 |
+
# Exchange code for token
|
43 |
token = sp_oauth.get_access_token(code, as_dict=False)
|
44 |
sp = spotipy.Spotify(auth=token)
|
45 |
pls = sp.current_user_playlists(limit=50)["items"]
|
46 |
user_playlists = {p["name"]: p["id"] for p in pls}
|
47 |
+
print(f"[DEBUG] Retrieved playlists: {list(user_playlists.keys())}")
|
48 |
return (
|
49 |
gr.update(visible=True, value="β
Logged in! Choose a playlist below."),
|
50 |
gr.update(visible=True, choices=list(user_playlists.keys())),
|
51 |
)
|
52 |
|
53 |
def load_playlist_info(pl_name: str):
|
54 |
+
print(f"[DEBUG] load_playlist_info called for: {pl_name}")
|
55 |
pid = user_playlists[pl_name]
|
56 |
data = sp.playlist(pid)
|
57 |
img = data["images"][0]["url"] if data["images"] else ""
|
|
|
65 |
return html, gr.update(visible=True)
|
66 |
|
67 |
def shuffle_and_play(pl_name: str):
|
68 |
+
print(f"[DEBUG] shuffle_and_play called for: {pl_name}")
|
69 |
pid = user_playlists[pl_name]
|
70 |
tracks, results = [], sp.playlist_tracks(pid)
|
71 |
tracks.extend(results["items"])
|
72 |
while results["next"]:
|
73 |
results = sp.next(results)
|
74 |
tracks.extend(results["items"])
|
|
|
75 |
uris = [t["track"]["uri"] for t in tracks if t["track"]]
|
76 |
devices = sp.devices()["devices"]
|
77 |
if not devices:
|
78 |
+
print("[DEBUG] No active devices found")
|
79 |
return gr.update(value="β οΈ No active device. Open Spotify and try again.", visible=True)
|
|
|
80 |
sp.start_playback(device_id=devices[0]["id"], uris=uris)
|
|
|
81 |
now = sp.current_playback()
|
82 |
if now and now.get("item"):
|
83 |
it = now["item"]
|
|
|
100 |
login_btn = gr.Button("π Step 1: Login to Spotify")
|
101 |
login_btn.click(
|
102 |
fn=None, inputs=None, outputs=None,
|
103 |
+
js=f"""
|
104 |
+
() => {{
|
105 |
+
console.log("π [JS] Login button clicked, redirecting...");
|
106 |
+
window.top.location.href = '{get_auth_url()}';
|
107 |
+
}}
|
108 |
+
"""
|
109 |
)
|
110 |
|
111 |
# Hidden box to capture the `code` after redirect
|
|
|
126 |
inputs=[code_box],
|
127 |
outputs=[status, dropdown],
|
128 |
js="""
|
129 |
+
() => {
|
130 |
+
const url = window.location.href;
|
131 |
+
const code = new URLSearchParams(window.location.search).get('code') || "";
|
132 |
+
console.log("π [JS] Current URL:", url);
|
133 |
+
console.log("π [JS] Parsed code:", code);
|
134 |
+
return code;
|
135 |
+
}
|
136 |
"""
|
137 |
)
|
138 |
|
|
|
139 |
dropdown.change(
|
140 |
fn=load_playlist_info,
|
141 |
inputs=[dropdown],
|
142 |
outputs=[info_html, shuffle_btn]
|
143 |
)
|
144 |
|
|
|
145 |
shuffle_btn.click(
|
146 |
fn=shuffle_and_play,
|
147 |
inputs=[dropdown],
|
|
|
151 |
if __name__ == "__main__":
|
152 |
demo.launch()
|
153 |
|
|