Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,11 +4,7 @@ import spotipy
|
|
4 |
from spotipy.oauth2 import SpotifyOAuth
|
5 |
import random
|
6 |
|
7 |
-
# ββββββββββββββββββββββββββββββ
|
8 |
-
# 1. Hard-code your Redirect URI here:
|
9 |
REDIRECT_URI = "https://jisaacso219-rng-shuffle.hf.space/"
|
10 |
-
|
11 |
-
# 2. Keep only your client creds in Secrets:
|
12 |
CLIENT_ID = os.environ["SPOTIFY_CLIENT_ID"]
|
13 |
CLIENT_SECRET = os.environ["SPOTIFY_CLIENT_SECRET"]
|
14 |
SCOPE = (
|
@@ -32,25 +28,21 @@ def get_auth_url():
|
|
32 |
return sp_oauth.get_authorize_url()
|
33 |
|
34 |
def check_login(code: str):
|
35 |
-
"""Receives the `code` directly from the page-load JS."""
|
36 |
global sp, user_playlists
|
37 |
-
# No code β hide everything until they come back from Spotify
|
38 |
if not code:
|
|
|
39 |
return gr.update(visible=False), gr.update(visible=False, choices=[])
|
40 |
-
|
41 |
-
# Exchange for token, fetch playlists
|
42 |
token = sp_oauth.get_access_token(code, as_dict=False)
|
43 |
sp = spotipy.Spotify(auth=token)
|
44 |
pls = sp.current_user_playlists(limit=50)["items"]
|
45 |
user_playlists = {p["name"]: p["id"] for p in pls}
|
46 |
-
|
47 |
return (
|
48 |
gr.update(visible=True, value="β
Logged in! Choose a playlist below."),
|
49 |
gr.update(visible=True, choices=list(user_playlists.keys())),
|
50 |
)
|
51 |
|
52 |
def load_playlist_info(pl_name: str):
|
53 |
-
pid
|
54 |
data = sp.playlist(pid)
|
55 |
img = data["images"][0]["url"] if data["images"] else ""
|
56 |
owner = data["owner"]["display_name"]
|
@@ -69,12 +61,10 @@ def shuffle_and_play(pl_name: str):
|
|
69 |
while results["next"]:
|
70 |
results = sp.next(results)
|
71 |
tracks.extend(results["items"])
|
72 |
-
|
73 |
uris = [t["track"]["uri"] for t in tracks if t["track"]]
|
74 |
devices = sp.devices()["devices"]
|
75 |
if not devices:
|
76 |
return gr.update(value="β οΈ No active device. Open Spotify and try again.", visible=True)
|
77 |
-
|
78 |
sp.start_playback(device_id=devices[0]["id"], uris=uris)
|
79 |
now = sp.current_playback()
|
80 |
if now and now.get("item"):
|
@@ -94,45 +84,32 @@ with gr.Blocks() as demo:
|
|
94 |
gr.Markdown("## π΅ RNG Spotify Playlist Shuffler")
|
95 |
gr.Markdown("Login, pick a playlist, then shuffle & playβall in one flow.")
|
96 |
|
97 |
-
# 1
|
98 |
-
login_btn = gr.Button(
|
99 |
-
"π Step 1: Login to Spotify",
|
100 |
-
link=get_auth_url()
|
101 |
-
)
|
102 |
|
103 |
-
#
|
104 |
-
|
105 |
-
dropdown = gr.Dropdown(choices=[], label="Step 2: Select a Playlist", visible=False)
|
106 |
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
|
|
111 |
|
112 |
-
#
|
113 |
demo.load(
|
114 |
fn=check_login,
|
115 |
-
inputs=[],
|
116 |
outputs=[status, dropdown],
|
117 |
-
js="""
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
)
|
122 |
-
|
123 |
-
# When they pick a playlist, show its artwork/info and reveal βShuffleβ
|
124 |
-
dropdown.change(
|
125 |
-
fn=load_playlist_info,
|
126 |
-
inputs=[dropdown],
|
127 |
-
outputs=[info_html, shuffle_btn]
|
128 |
)
|
129 |
|
130 |
-
|
131 |
-
shuffle_btn.click(
|
132 |
-
fn=shuffle_and_play,
|
133 |
-
inputs=[dropdown],
|
134 |
-
outputs=[result]
|
135 |
-
)
|
136 |
|
137 |
if __name__ == "__main__":
|
138 |
demo.launch()
|
|
|
|
4 |
from spotipy.oauth2 import SpotifyOAuth
|
5 |
import random
|
6 |
|
|
|
|
|
7 |
REDIRECT_URI = "https://jisaacso219-rng-shuffle.hf.space/"
|
|
|
|
|
8 |
CLIENT_ID = os.environ["SPOTIFY_CLIENT_ID"]
|
9 |
CLIENT_SECRET = os.environ["SPOTIFY_CLIENT_SECRET"]
|
10 |
SCOPE = (
|
|
|
28 |
return sp_oauth.get_authorize_url()
|
29 |
|
30 |
def check_login(code: str):
|
|
|
31 |
global sp, user_playlists
|
|
|
32 |
if not code:
|
33 |
+
# hide everything until we actually get a code
|
34 |
return gr.update(visible=False), gr.update(visible=False, choices=[])
|
|
|
|
|
35 |
token = sp_oauth.get_access_token(code, as_dict=False)
|
36 |
sp = spotipy.Spotify(auth=token)
|
37 |
pls = sp.current_user_playlists(limit=50)["items"]
|
38 |
user_playlists = {p["name"]: p["id"] for p in pls}
|
|
|
39 |
return (
|
40 |
gr.update(visible=True, value="β
Logged in! Choose a playlist below."),
|
41 |
gr.update(visible=True, choices=list(user_playlists.keys())),
|
42 |
)
|
43 |
|
44 |
def load_playlist_info(pl_name: str):
|
45 |
+
pid = user_playlists[pl_name]
|
46 |
data = sp.playlist(pid)
|
47 |
img = data["images"][0]["url"] if data["images"] else ""
|
48 |
owner = data["owner"]["display_name"]
|
|
|
61 |
while results["next"]:
|
62 |
results = sp.next(results)
|
63 |
tracks.extend(results["items"])
|
|
|
64 |
uris = [t["track"]["uri"] for t in tracks if t["track"]]
|
65 |
devices = sp.devices()["devices"]
|
66 |
if not devices:
|
67 |
return gr.update(value="β οΈ No active device. Open Spotify and try again.", visible=True)
|
|
|
68 |
sp.start_playback(device_id=devices[0]["id"], uris=uris)
|
69 |
now = sp.current_playback()
|
70 |
if now and now.get("item"):
|
|
|
84 |
gr.Markdown("## π΅ RNG Spotify Playlist Shuffler")
|
85 |
gr.Markdown("Login, pick a playlist, then shuffle & playβall in one flow.")
|
86 |
|
87 |
+
# Step 1: Login button
|
88 |
+
login_btn = gr.Button("π Step 1: Login to Spotify", link=get_auth_url())
|
|
|
|
|
|
|
89 |
|
90 |
+
# βββ hereβs the hidden Textbox that JS will write into:
|
91 |
+
code_box = gr.Textbox(visible=False, elem_id="auth_code")
|
|
|
92 |
|
93 |
+
status = gr.Markdown(visible=False)
|
94 |
+
dropdown = gr.Dropdown(choices=[], label="Step 2: Select a Playlist", visible=False)
|
95 |
+
info_html = gr.HTML(visible=False)
|
96 |
+
shuffle_btn= gr.Button("π Step 3: Shuffle & Play", visible=False)
|
97 |
+
result = gr.HTML(visible=False)
|
98 |
|
99 |
+
# On page-load, grab the ?code=β¦ and feed it into `code_box`, then call check_login
|
100 |
demo.load(
|
101 |
fn=check_login,
|
102 |
+
inputs=[code_box], # single input to match check_login(code)
|
103 |
outputs=[status, dropdown],
|
104 |
+
js="""
|
105 |
+
const code = new URLSearchParams(window.location.search).get('code');
|
106 |
+
return code;
|
107 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
)
|
109 |
|
110 |
+
dropdown.change(load_playlist_info, [dropdown], [info_html, shuffle_btn])
|
111 |
+
shuffle_btn.click(shuffle_and_play, [dropdown], [result])
|
|
|
|
|
|
|
|
|
112 |
|
113 |
if __name__ == "__main__":
|
114 |
demo.launch()
|
115 |
+
|