Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,15 @@
|
|
1 |
import os
|
2 |
import random
|
|
|
3 |
import gradio as gr
|
4 |
import spotipy
|
5 |
from spotipy.oauth2 import SpotifyOAuth
|
6 |
|
7 |
-
# Spotify credentials (set in HF Spaces Secrets)
|
8 |
CLIENT_ID = os.environ["SPOTIFY_CLIENT_ID"]
|
9 |
CLIENT_SECRET = os.environ["SPOTIFY_CLIENT_SECRET"]
|
10 |
REDIRECT_URI = "https://jisaacso219-rng-shuffle.hf.space/"
|
11 |
SCOPE = "user-read-playback-state user-modify-playback-state playlist-read-private"
|
12 |
|
13 |
-
# OAuth setup
|
14 |
sp_oauth = SpotifyOAuth(
|
15 |
client_id=CLIENT_ID,
|
16 |
client_secret=CLIENT_SECRET,
|
@@ -23,10 +22,12 @@ sp = None
|
|
23 |
user_playlists = {}
|
24 |
device_map = {}
|
25 |
|
|
|
26 |
def get_auth_url():
|
27 |
return sp_oauth.get_authorize_url()
|
28 |
|
29 |
-
|
|
|
30 |
global sp, user_playlists, device_map
|
31 |
|
32 |
if "?code=" not in current_url:
|
@@ -42,39 +43,42 @@ def try_login(current_url):
|
|
42 |
access_token = token_info["access_token"]
|
43 |
sp = spotipy.Spotify(auth=access_token)
|
44 |
|
45 |
-
# Get playlists
|
46 |
playlists = sp.current_user_playlists(limit=50)["items"]
|
47 |
-
user_playlists
|
|
|
|
|
|
|
|
|
48 |
|
49 |
-
# Get active devices
|
50 |
devices = sp.devices()["devices"]
|
51 |
-
device_map
|
|
|
|
|
52 |
|
53 |
if not device_map:
|
54 |
return (
|
55 |
-
gr.update(value="β οΈ No active Spotify devices found.
|
56 |
gr.update(visible=False),
|
57 |
gr.update(visible=False),
|
58 |
gr.update(visible=False),
|
59 |
)
|
60 |
|
61 |
return (
|
62 |
-
gr.update(value="β
Logged in!
|
63 |
gr.update(visible=True, choices=list(user_playlists.keys())),
|
64 |
gr.update(visible=True, choices=list(device_map.keys())),
|
65 |
gr.update(visible=True),
|
66 |
)
|
67 |
|
|
|
68 |
def shuffle_and_play(playlist_name, device_name):
|
69 |
pid = user_playlists.get(playlist_name)
|
70 |
device_id = device_map.get(device_name)
|
71 |
|
72 |
if not pid or not device_id:
|
73 |
-
return "β Invalid playlist or device
|
74 |
|
75 |
-
|
76 |
-
tracks = []
|
77 |
-
res = sp.playlist_tracks(pid)
|
78 |
tracks.extend(res["items"])
|
79 |
while res["next"]:
|
80 |
res = sp.next(res)
|
@@ -84,32 +88,58 @@ def shuffle_and_play(playlist_name, device_name):
|
|
84 |
random.shuffle(uris)
|
85 |
|
86 |
try:
|
87 |
-
# Limit to first 100 tracks to avoid 413 error
|
88 |
sp.start_playback(device_id=device_id, uris=uris[:100])
|
89 |
-
|
|
|
|
|
|
|
|
|
90 |
except Exception as e:
|
91 |
return f"β Playback failed: {str(e)}"
|
92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
with gr.Blocks() as demo:
|
94 |
gr.Markdown("## π΅ RNG Spotify Playlist Shuffler")
|
|
|
95 |
|
96 |
-
gr.
|
97 |
-
|
98 |
-
|
99 |
-
status = gr.Markdown()
|
100 |
playlist_dd = gr.Dropdown(label="Step 2: Select a Playlist", visible=False)
|
101 |
-
device_dd
|
102 |
shuffle_btn = gr.Button("π Step 4: Shuffle & Play", visible=False)
|
103 |
-
result
|
|
|
|
|
104 |
|
105 |
demo.load(
|
106 |
fn=try_login,
|
107 |
-
inputs=[url_state],
|
108 |
outputs=[status, playlist_dd, device_dd, shuffle_btn],
|
109 |
js="() => window.location.href"
|
110 |
)
|
111 |
|
112 |
shuffle_btn.click(shuffle_and_play, [playlist_dd, device_dd], [result])
|
|
|
113 |
|
114 |
if __name__ == "__main__":
|
115 |
demo.launch(server_name="0.0.0.0", ssr_mode=False)
|
|
|
1 |
import os
|
2 |
import random
|
3 |
+
import time
|
4 |
import gradio as gr
|
5 |
import spotipy
|
6 |
from spotipy.oauth2 import SpotifyOAuth
|
7 |
|
|
|
8 |
CLIENT_ID = os.environ["SPOTIFY_CLIENT_ID"]
|
9 |
CLIENT_SECRET = os.environ["SPOTIFY_CLIENT_SECRET"]
|
10 |
REDIRECT_URI = "https://jisaacso219-rng-shuffle.hf.space/"
|
11 |
SCOPE = "user-read-playback-state user-modify-playback-state playlist-read-private"
|
12 |
|
|
|
13 |
sp_oauth = SpotifyOAuth(
|
14 |
client_id=CLIENT_ID,
|
15 |
client_secret=CLIENT_SECRET,
|
|
|
22 |
user_playlists = {}
|
23 |
device_map = {}
|
24 |
|
25 |
+
|
26 |
def get_auth_url():
|
27 |
return sp_oauth.get_authorize_url()
|
28 |
|
29 |
+
|
30 |
+
def try_login(current_url, filter_keyword):
|
31 |
global sp, user_playlists, device_map
|
32 |
|
33 |
if "?code=" not in current_url:
|
|
|
43 |
access_token = token_info["access_token"]
|
44 |
sp = spotipy.Spotify(auth=access_token)
|
45 |
|
|
|
46 |
playlists = sp.current_user_playlists(limit=50)["items"]
|
47 |
+
user_playlists.clear()
|
48 |
+
for p in playlists:
|
49 |
+
name = p["name"]
|
50 |
+
if not filter_keyword or filter_keyword.lower() in name.lower():
|
51 |
+
user_playlists[name] = p["id"]
|
52 |
|
|
|
53 |
devices = sp.devices()["devices"]
|
54 |
+
device_map.clear()
|
55 |
+
for d in devices:
|
56 |
+
device_map[d["name"]] = d["id"]
|
57 |
|
58 |
if not device_map:
|
59 |
return (
|
60 |
+
gr.update(value="β οΈ No active Spotify devices found."),
|
61 |
gr.update(visible=False),
|
62 |
gr.update(visible=False),
|
63 |
gr.update(visible=False),
|
64 |
)
|
65 |
|
66 |
return (
|
67 |
+
gr.update(value="β
Logged in!"),
|
68 |
gr.update(visible=True, choices=list(user_playlists.keys())),
|
69 |
gr.update(visible=True, choices=list(device_map.keys())),
|
70 |
gr.update(visible=True),
|
71 |
)
|
72 |
|
73 |
+
|
74 |
def shuffle_and_play(playlist_name, device_name):
|
75 |
pid = user_playlists.get(playlist_name)
|
76 |
device_id = device_map.get(device_name)
|
77 |
|
78 |
if not pid or not device_id:
|
79 |
+
return "β Invalid playlist or device."
|
80 |
|
81 |
+
tracks, res = [], sp.playlist_tracks(pid)
|
|
|
|
|
82 |
tracks.extend(res["items"])
|
83 |
while res["next"]:
|
84 |
res = sp.next(res)
|
|
|
88 |
random.shuffle(uris)
|
89 |
|
90 |
try:
|
|
|
91 |
sp.start_playback(device_id=device_id, uris=uris[:100])
|
92 |
+
# queue remaining
|
93 |
+
for uri in uris[100:]:
|
94 |
+
sp.add_to_queue(uri, device_id=device_id)
|
95 |
+
time.sleep(0.1) # avoid rate limit
|
96 |
+
return f"βΆοΈ Now playing **{playlist_name}** on **{device_name}**."
|
97 |
except Exception as e:
|
98 |
return f"β Playback failed: {str(e)}"
|
99 |
|
100 |
+
|
101 |
+
def now_playing():
|
102 |
+
try:
|
103 |
+
data = sp.current_playback()
|
104 |
+
if not data or not data.get("is_playing"):
|
105 |
+
return "β οΈ Nothing is currently playing."
|
106 |
+
track = data["item"]
|
107 |
+
name = track["name"]
|
108 |
+
artist = ", ".join([a["name"] for a in track["artists"]])
|
109 |
+
album = track["album"]["name"]
|
110 |
+
image = track["album"]["images"][0]["url"]
|
111 |
+
html = f"""
|
112 |
+
<img src='{image}' width='300'><br>
|
113 |
+
<b>{name}</b> by {artist}<br>
|
114 |
+
<i>{album}</i>
|
115 |
+
"""
|
116 |
+
return html
|
117 |
+
except Exception as e:
|
118 |
+
return f"β Error fetching current track: {e}"
|
119 |
+
|
120 |
with gr.Blocks() as demo:
|
121 |
gr.Markdown("## π΅ RNG Spotify Playlist Shuffler")
|
122 |
+
gr.HTML(f'<a href="{get_auth_url()}"><button style="width:100%;height:40px;">π Login to Spotify</button></a>')
|
123 |
|
124 |
+
url_state = gr.Textbox(visible=False)
|
125 |
+
filter_txt = gr.Textbox(label="Optional: Filter Playlists by Keyword")
|
126 |
+
status = gr.Markdown()
|
|
|
127 |
playlist_dd = gr.Dropdown(label="Step 2: Select a Playlist", visible=False)
|
128 |
+
device_dd = gr.Dropdown(label="Step 3: Select a Device", visible=False)
|
129 |
shuffle_btn = gr.Button("π Step 4: Shuffle & Play", visible=False)
|
130 |
+
result = gr.Markdown()
|
131 |
+
now_box = gr.HTML()
|
132 |
+
refresh_btn = gr.Button("π Show Current Track")
|
133 |
|
134 |
demo.load(
|
135 |
fn=try_login,
|
136 |
+
inputs=[url_state, filter_txt],
|
137 |
outputs=[status, playlist_dd, device_dd, shuffle_btn],
|
138 |
js="() => window.location.href"
|
139 |
)
|
140 |
|
141 |
shuffle_btn.click(shuffle_and_play, [playlist_dd, device_dd], [result])
|
142 |
+
refresh_btn.click(now_playing, outputs=[now_box])
|
143 |
|
144 |
if __name__ == "__main__":
|
145 |
demo.launch(server_name="0.0.0.0", ssr_mode=False)
|