Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,11 +4,13 @@ import gradio as gr
|
|
4 |
import spotipy
|
5 |
from spotipy.oauth2 import SpotifyOAuth
|
6 |
|
|
|
7 |
CLIENT_ID = os.environ["SPOTIFY_CLIENT_ID"]
|
8 |
CLIENT_SECRET = os.environ["SPOTIFY_CLIENT_SECRET"]
|
9 |
REDIRECT_URI = "https://jisaacso219-rng-shuffle.hf.space/"
|
10 |
SCOPE = "user-read-playback-state user-modify-playback-state playlist-read-private"
|
11 |
|
|
|
12 |
sp_oauth = SpotifyOAuth(
|
13 |
client_id=CLIENT_ID,
|
14 |
client_secret=CLIENT_SECRET,
|
@@ -19,100 +21,93 @@ sp_oauth = SpotifyOAuth(
|
|
19 |
|
20 |
sp = None
|
21 |
user_playlists = {}
|
22 |
-
|
23 |
|
24 |
def get_auth_url():
|
25 |
return sp_oauth.get_authorize_url()
|
26 |
|
27 |
-
def try_login(
|
28 |
-
global sp, user_playlists,
|
29 |
|
30 |
-
if "?code=" not in
|
31 |
return (
|
32 |
-
gr.update(value="π Please login
|
33 |
gr.update(visible=False),
|
34 |
gr.update(visible=False),
|
35 |
-
)
|
36 |
-
|
37 |
-
try:
|
38 |
-
code = full_url.split("?code=")[1].split("&")[0]
|
39 |
-
token = sp_oauth.get_access_token(code, as_dict=True)
|
40 |
-
access_token = token["access_token"]
|
41 |
-
except Exception as e:
|
42 |
-
return (
|
43 |
-
gr.update(value=f"β OAuth failed: {str(e)}. Please log in again."),
|
44 |
-
gr.update(visible=False),
|
45 |
gr.update(visible=False),
|
46 |
)
|
47 |
|
|
|
|
|
|
|
48 |
sp = spotipy.Spotify(auth=access_token)
|
49 |
|
|
|
50 |
playlists = sp.current_user_playlists(limit=50)["items"]
|
51 |
user_playlists = {p["name"]: p["id"] for p in playlists}
|
52 |
|
|
|
53 |
devices = sp.devices()["devices"]
|
54 |
-
|
55 |
|
56 |
-
if not
|
57 |
return (
|
58 |
-
gr.update(value="β οΈ No active Spotify devices found. Open Spotify
|
|
|
59 |
gr.update(visible=False),
|
60 |
gr.update(visible=False),
|
61 |
)
|
62 |
|
63 |
return (
|
64 |
-
gr.update(value="β
Logged in!
|
65 |
gr.update(visible=True, choices=list(user_playlists.keys())),
|
66 |
-
gr.update(visible=True, choices=list(
|
|
|
67 |
)
|
68 |
|
69 |
def shuffle_and_play(playlist_name, device_name):
|
70 |
-
pid = user_playlists
|
71 |
-
device_id =
|
|
|
|
|
|
|
72 |
|
73 |
-
tracks
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
78 |
|
79 |
uris = [t["track"]["uri"] for t in tracks if t["track"]]
|
80 |
random.shuffle(uris)
|
81 |
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
85 |
|
86 |
with gr.Blocks() as demo:
|
87 |
gr.Markdown("## π΅ RNG Spotify Playlist Shuffler")
|
88 |
|
89 |
-
|
90 |
-
f'<a href="{get_auth_url()}"><button style="width:100%;height:40px;font-size:16px;">π Login to Spotify</button></a>'
|
91 |
-
)
|
92 |
|
93 |
-
|
|
|
94 |
playlist_dd = gr.Dropdown(label="Select a Playlist", visible=False)
|
95 |
-
device_dd
|
96 |
-
|
97 |
shuffle_btn = gr.Button("π Shuffle & Play", visible=False)
|
98 |
-
result
|
99 |
-
|
100 |
-
url_state = gr.Textbox(visible=False)
|
101 |
|
102 |
demo.load(
|
103 |
-
try_login,
|
104 |
inputs=[url_state],
|
105 |
-
outputs=[status, playlist_dd, device_dd],
|
106 |
js="() => window.location.href"
|
107 |
)
|
108 |
|
109 |
-
|
110 |
-
|
111 |
-
shuffle_btn.click(
|
112 |
-
shuffle_and_play,
|
113 |
-
inputs=[playlist_dd, device_dd],
|
114 |
-
outputs=[result]
|
115 |
-
)
|
116 |
|
117 |
if __name__ == "__main__":
|
118 |
demo.launch(server_name="0.0.0.0", ssr_mode=False)
|
|
|
4 |
import spotipy
|
5 |
from spotipy.oauth2 import SpotifyOAuth
|
6 |
|
7 |
+
# Set credentials from environment (Hugging Face 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 |
+
# Initialize Spotify auth
|
14 |
sp_oauth = SpotifyOAuth(
|
15 |
client_id=CLIENT_ID,
|
16 |
client_secret=CLIENT_SECRET,
|
|
|
21 |
|
22 |
sp = None
|
23 |
user_playlists = {}
|
24 |
+
device_map = {}
|
25 |
|
26 |
def get_auth_url():
|
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 |
+
gr.update(value="π 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 |
+
token_info = sp_oauth.get_access_token(code, as_dict=True)
|
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 = {p["name"]: p["id"] for p in playlists}
|
48 |
|
49 |
+
# Get devices
|
50 |
devices = sp.devices()["devices"]
|
51 |
+
device_map = {d["name"]: d["id"] for d in devices}
|
52 |
|
53 |
+
if not device_map:
|
54 |
return (
|
55 |
+
gr.update(value="β οΈ No active Spotify devices found. Open Spotify on your phone or PC."),
|
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! Pick a playlist and device."),
|
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 selection."
|
74 |
|
75 |
+
tracks = []
|
76 |
+
res = sp.playlist_tracks(pid)
|
77 |
+
tracks.extend(res["items"])
|
78 |
+
while res["next"]:
|
79 |
+
res = sp.next(res)
|
80 |
+
tracks.extend(res["items"])
|
81 |
|
82 |
uris = [t["track"]["uri"] for t in tracks if t["track"]]
|
83 |
random.shuffle(uris)
|
84 |
|
85 |
+
try:
|
86 |
+
sp.start_playback(device_id=device_id, uris=uris)
|
87 |
+
return f"βΆοΈ Now playing: **{playlist_name}** on **{device_name}**"
|
88 |
+
except Exception as e:
|
89 |
+
return f"β Playback failed: {str(e)}"
|
90 |
|
91 |
with gr.Blocks() as demo:
|
92 |
gr.Markdown("## π΅ RNG Spotify Playlist Shuffler")
|
93 |
|
94 |
+
gr.HTML(f'<a href="{get_auth_url()}"><button style="width:100%; height:40px;">π Login to Spotify</button></a>')
|
|
|
|
|
95 |
|
96 |
+
url_state = gr.Textbox(visible=False)
|
97 |
+
status = gr.Markdown()
|
98 |
playlist_dd = gr.Dropdown(label="Select a Playlist", visible=False)
|
99 |
+
device_dd = gr.Dropdown(label="Select a Device", visible=False)
|
|
|
100 |
shuffle_btn = gr.Button("π Shuffle & Play", visible=False)
|
101 |
+
result = gr.Markdown()
|
|
|
|
|
102 |
|
103 |
demo.load(
|
104 |
+
fn=try_login,
|
105 |
inputs=[url_state],
|
106 |
+
outputs=[status, playlist_dd, device_dd, shuffle_btn],
|
107 |
js="() => window.location.href"
|
108 |
)
|
109 |
|
110 |
+
shuffle_btn.click(shuffle_and_play, [playlist_dd, device_dd], [result])
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
if __name__ == "__main__":
|
113 |
demo.launch(server_name="0.0.0.0", ssr_mode=False)
|