Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import os
|
2 |
import random
|
3 |
import time
|
|
|
4 |
import gradio as gr
|
5 |
import spotipy
|
6 |
from spotipy.oauth2 import SpotifyOAuth
|
@@ -67,6 +68,15 @@ def try_login(current_url):
|
|
67 |
gr.update(visible=True),
|
68 |
)
|
69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
def shuffle_and_play(playlist_name, device_name):
|
71 |
pid = user_playlists.get(playlist_name)
|
72 |
device_id = device_map.get(device_name)
|
@@ -85,9 +95,10 @@ def shuffle_and_play(playlist_name, device_name):
|
|
85 |
|
86 |
try:
|
87 |
sp.start_playback(device_id=device_id, uris=uris[:100])
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
91 |
return now_playing()
|
92 |
except Exception as e:
|
93 |
return f"β Playback failed: {str(e)}"
|
@@ -134,3 +145,4 @@ with gr.Blocks() as demo:
|
|
134 |
if __name__ == "__main__":
|
135 |
demo.launch(server_name="0.0.0.0", ssr_mode=False)
|
136 |
|
|
|
|
1 |
import os
|
2 |
import random
|
3 |
import time
|
4 |
+
import threading
|
5 |
import gradio as gr
|
6 |
import spotipy
|
7 |
from spotipy.oauth2 import SpotifyOAuth
|
|
|
68 |
gr.update(visible=True),
|
69 |
)
|
70 |
|
71 |
+
def background_queue_tracks(uris, device_id):
|
72 |
+
for i, uri in enumerate(uris):
|
73 |
+
try:
|
74 |
+
sp.add_to_queue(uri, device_id=device_id)
|
75 |
+
time.sleep(0.25) # Reduce rate-limit risk
|
76 |
+
except Exception as e:
|
77 |
+
print(f"[Queue Error] Track {i}: {e}")
|
78 |
+
time.sleep(1)
|
79 |
+
|
80 |
def shuffle_and_play(playlist_name, device_name):
|
81 |
pid = user_playlists.get(playlist_name)
|
82 |
device_id = device_map.get(device_name)
|
|
|
95 |
|
96 |
try:
|
97 |
sp.start_playback(device_id=device_id, uris=uris[:100])
|
98 |
+
|
99 |
+
# Start queuing the rest of the playlist in background
|
100 |
+
threading.Thread(target=background_queue_tracks, args=(uris[100:200], device_id)).start()
|
101 |
+
|
102 |
return now_playing()
|
103 |
except Exception as e:
|
104 |
return f"β Playback failed: {str(e)}"
|
|
|
145 |
if __name__ == "__main__":
|
146 |
demo.launch(server_name="0.0.0.0", ssr_mode=False)
|
147 |
|
148 |
+
|