Spaces:
Running
Running
Update app.py
Browse filesFixed now playing and spotify auth issue (hopefully)
app.py
CHANGED
@@ -27,70 +27,94 @@ def get_auth_url():
|
|
27 |
return sp_oauth.get_authorize_url()
|
28 |
|
29 |
def try_login(current_url):
|
30 |
-
|
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):
|
87 |
try:
|
88 |
sp.add_to_queue(uri, device_id=device_id)
|
89 |
-
time.sleep(0.25)
|
90 |
except Exception as e:
|
91 |
print(f"[Queue Error] Track {i}: {e}")
|
92 |
time.sleep(1)
|
93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
def shuffle_and_play(playlist_name, device_name):
|
95 |
pid = user_playlists.get(playlist_name)
|
96 |
device_id = device_map.get(device_name)
|
@@ -109,33 +133,11 @@ def shuffle_and_play(playlist_name, device_name):
|
|
109 |
|
110 |
try:
|
111 |
sp.start_playback(device_id=device_id, uris=uris[:100])
|
112 |
-
|
113 |
-
# Start queuing the rest of the playlist in background
|
114 |
threading.Thread(target=background_queue_tracks, args=(uris[100:200], device_id)).start()
|
115 |
-
|
116 |
-
return now_playing()
|
117 |
except Exception as e:
|
118 |
return f"❌ Playback failed: {str(e)}"
|
119 |
|
120 |
-
def now_playing():
|
121 |
-
try:
|
122 |
-
data = sp.current_playback()
|
123 |
-
if not data or not data.get("is_playing"):
|
124 |
-
return "⚠️ Nothing is currently playing."
|
125 |
-
track = data["item"]
|
126 |
-
name = track["name"]
|
127 |
-
artist = ", ".join([a["name"] for a in track["artists"]])
|
128 |
-
album = track["album"]["name"]
|
129 |
-
image = track["album"]["images"][0]["url"]
|
130 |
-
html = f"""
|
131 |
-
<img src='{image}' width='300'><br>
|
132 |
-
<b>{name}</b> by {artist}<br/>
|
133 |
-
<i>{album}</i>
|
134 |
-
"""
|
135 |
-
return html
|
136 |
-
except Exception as e:
|
137 |
-
return f"❌ Error fetching current track: {e}"
|
138 |
-
|
139 |
with gr.Blocks() as demo:
|
140 |
gr.Markdown("## 🎵 RNG Spotify Playlist Shuffler")
|
141 |
gr.HTML(f'<a href="{get_auth_url()}"><button style="width:100%;height:40px;">🔐 Login to Spotify</button></a>')
|
@@ -154,7 +156,12 @@ with gr.Blocks() as demo:
|
|
154 |
js="() => { return [window.location.href]; }"
|
155 |
)
|
156 |
|
157 |
-
|
|
|
|
|
|
|
|
|
|
|
158 |
|
159 |
if __name__ == "__main__":
|
160 |
demo.launch(server_name="0.0.0.0", ssr_mode=False)
|
|
|
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_cached_token()
|
43 |
+
if not token_info:
|
44 |
+
token_info = sp_oauth.get_access_token(code)
|
45 |
+
access_token = token_info["access_token"]
|
46 |
+
except Exception as e:
|
47 |
+
print("🔁 Token refresh failed:", str(e))
|
48 |
+
return (
|
49 |
+
f"❌ Failed to log in: {e}",
|
50 |
+
gr.update(visible=False),
|
51 |
+
gr.update(visible=False),
|
52 |
+
gr.update(visible=False),
|
53 |
+
)
|
54 |
+
|
55 |
+
sp = spotipy.Spotify(auth=access_token)
|
56 |
+
|
57 |
+
try:
|
58 |
+
playlists = sp.current_user_playlists(limit=50)["items"]
|
59 |
+
except Exception as e:
|
60 |
+
return f"❌ Failed to get playlists: {e}", *[gr.update(visible=False)] * 3
|
61 |
+
|
62 |
+
user_playlists.clear()
|
63 |
+
for p in playlists:
|
64 |
+
user_playlists[p["name"]] = p["id"]
|
65 |
+
|
66 |
+
devices = sp.devices()["devices"]
|
67 |
+
device_map.clear()
|
68 |
+
for d in devices:
|
69 |
+
device_map[d["name"]] = d["id"]
|
70 |
+
|
71 |
+
if not device_map:
|
72 |
+
return (
|
73 |
+
"⚠️ No active Spotify devices found.",
|
74 |
+
gr.update(visible=False),
|
75 |
+
gr.update(visible=False),
|
76 |
+
gr.update(visible=False),
|
77 |
+
)
|
78 |
+
|
79 |
+
return (
|
80 |
+
"✅ Logged in!",
|
81 |
+
gr.update(visible=True, choices=list(user_playlists.keys())),
|
82 |
+
gr.update(visible=True, choices=list(device_map.keys())),
|
83 |
+
gr.update(visible=True),
|
84 |
+
)
|
85 |
|
86 |
def background_queue_tracks(uris, device_id):
|
87 |
for i, uri in enumerate(uris):
|
88 |
try:
|
89 |
sp.add_to_queue(uri, device_id=device_id)
|
90 |
+
time.sleep(0.25)
|
91 |
except Exception as e:
|
92 |
print(f"[Queue Error] Track {i}: {e}")
|
93 |
time.sleep(1)
|
94 |
|
95 |
+
def now_playing():
|
96 |
+
try:
|
97 |
+
data = sp.current_playback()
|
98 |
+
if not data or not data.get("is_playing"):
|
99 |
+
return "⚠️ Nothing is currently playing."
|
100 |
+
track = data["item"]
|
101 |
+
name = track["name"]
|
102 |
+
artist = ", ".join([a["name"] for a in track["artists"]])
|
103 |
+
album = track["album"]["name"]
|
104 |
+
image = track["album"]["images"][0]["url"]
|
105 |
+
html = f"""
|
106 |
+
<img src='{image}' width='300'><br>
|
107 |
+
<b>{name}</b> by {artist}<br/>
|
108 |
+
<i>{album}</i>
|
109 |
+
"""
|
110 |
+
return html
|
111 |
+
except Exception as e:
|
112 |
+
return f"❌ Error fetching current track: {e}"
|
113 |
+
|
114 |
+
def delayed_now_playing(output):
|
115 |
+
time.sleep(10)
|
116 |
+
output.update(now_playing())
|
117 |
+
|
118 |
def shuffle_and_play(playlist_name, device_name):
|
119 |
pid = user_playlists.get(playlist_name)
|
120 |
device_id = device_map.get(device_name)
|
|
|
133 |
|
134 |
try:
|
135 |
sp.start_playback(device_id=device_id, uris=uris[:100])
|
|
|
|
|
136 |
threading.Thread(target=background_queue_tracks, args=(uris[100:200], device_id)).start()
|
137 |
+
return "✅ Shuffling... Song info will display in ~10 seconds."
|
|
|
138 |
except Exception as e:
|
139 |
return f"❌ Playback failed: {str(e)}"
|
140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
with gr.Blocks() as demo:
|
142 |
gr.Markdown("## 🎵 RNG Spotify Playlist Shuffler")
|
143 |
gr.HTML(f'<a href="{get_auth_url()}"><button style="width:100%;height:40px;">🔐 Login to Spotify</button></a>')
|
|
|
156 |
js="() => { return [window.location.href]; }"
|
157 |
)
|
158 |
|
159 |
+
def handle_shuffle(playlist_name, device_name):
|
160 |
+
result_text = shuffle_and_play(playlist_name, device_name)
|
161 |
+
threading.Thread(target=delayed_now_playing, args=(result,)).start()
|
162 |
+
return result_text
|
163 |
+
|
164 |
+
shuffle_btn.click(handle_shuffle, [playlist_dd, device_dd], [result])
|
165 |
|
166 |
if __name__ == "__main__":
|
167 |
demo.launch(server_name="0.0.0.0", ssr_mode=False)
|