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 = (
|
@@ -31,25 +27,19 @@ user_playlists = {}
|
|
31 |
def get_auth_url():
|
32 |
return sp_oauth.get_authorize_url()
|
33 |
|
34 |
-
|
35 |
def check_login(code: str):
|
36 |
-
"""Called on every page-load to see if Spotify has redirected us back with ?code=β¦"""
|
37 |
global sp, user_playlists
|
38 |
if not code:
|
39 |
-
return (
|
40 |
-
gr.update(visible=False), # hide status until code arrives
|
41 |
-
gr.update(visible=False, choices=[]),
|
42 |
-
)
|
43 |
token = sp_oauth.get_access_token(code, as_dict=False)
|
44 |
sp = spotipy.Spotify(auth=token)
|
45 |
pls = sp.current_user_playlists(limit=50)["items"]
|
46 |
user_playlists = {p["name"]: p["id"] for p in pls}
|
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 |
-
|
53 |
def load_playlist_info(pl_name: str):
|
54 |
pid = user_playlists[pl_name]
|
55 |
data = sp.playlist(pid)
|
@@ -63,7 +53,6 @@ def load_playlist_info(pl_name: str):
|
|
63 |
"""
|
64 |
return html, gr.update(visible=True)
|
65 |
|
66 |
-
|
67 |
def shuffle_and_play(pl_name: str):
|
68 |
pid = user_playlists[pl_name]
|
69 |
tracks, results = [], sp.playlist_tracks(pid)
|
@@ -92,7 +81,6 @@ def shuffle_and_play(pl_name: str):
|
|
92 |
return html
|
93 |
return "βΆοΈ Playing your shuffled playlist!"
|
94 |
|
95 |
-
|
96 |
with gr.Blocks() as demo:
|
97 |
gr.Markdown("## π΅ RNG Spotify Playlist Shuffler")
|
98 |
gr.Markdown("Login, pick a playlist, then shuffle & playβall in one flow.")
|
@@ -104,29 +92,25 @@ with gr.Blocks() as demo:
|
|
104 |
)
|
105 |
|
106 |
# Hidden container for the ?code=β¦ Spotify will append on redirect
|
107 |
-
code_box
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
dropdown = gr.Dropdown(choices=[], label="Step 2: Select a Playlist", visible=False)
|
112 |
-
|
113 |
-
# Playlist info + shuffle controls
|
114 |
-
info_html = gr.HTML(visible=False)
|
115 |
shuffle_btn = gr.Button("π Step 3: Shuffle & Play", visible=False)
|
116 |
-
result
|
117 |
|
118 |
# Run on every page-load: grab ?code=β¦ via JS and call check_login
|
119 |
demo.load(
|
120 |
fn=check_login,
|
121 |
inputs=[code_box],
|
122 |
outputs=[status, dropdown],
|
123 |
-
|
124 |
-
const
|
125 |
-
const
|
126 |
-
if (
|
127 |
document
|
128 |
.getElementById("component-auth_code")
|
129 |
-
.querySelector("textarea").value =
|
130 |
}
|
131 |
"""
|
132 |
)
|
|
|
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 = (
|
|
|
27 |
def get_auth_url():
|
28 |
return sp_oauth.get_authorize_url()
|
29 |
|
|
|
30 |
def check_login(code: str):
|
|
|
31 |
global sp, user_playlists
|
32 |
if not code:
|
33 |
+
return gr.update(visible=False), gr.update(visible=False, choices=[])
|
|
|
|
|
|
|
34 |
token = sp_oauth.get_access_token(code, as_dict=False)
|
35 |
sp = spotipy.Spotify(auth=token)
|
36 |
pls = sp.current_user_playlists(limit=50)["items"]
|
37 |
user_playlists = {p["name"]: p["id"] for p in pls}
|
38 |
return (
|
39 |
gr.update(visible=True, value="β
Logged in! Choose a playlist below."),
|
40 |
+
gr.update(visible=True, choices=list(user_playlists.keys()))
|
41 |
)
|
42 |
|
|
|
43 |
def load_playlist_info(pl_name: str):
|
44 |
pid = user_playlists[pl_name]
|
45 |
data = sp.playlist(pid)
|
|
|
53 |
"""
|
54 |
return html, gr.update(visible=True)
|
55 |
|
|
|
56 |
def shuffle_and_play(pl_name: str):
|
57 |
pid = user_playlists[pl_name]
|
58 |
tracks, results = [], sp.playlist_tracks(pid)
|
|
|
81 |
return html
|
82 |
return "βΆοΈ Playing your shuffled playlist!"
|
83 |
|
|
|
84 |
with gr.Blocks() as demo:
|
85 |
gr.Markdown("## π΅ RNG Spotify Playlist Shuffler")
|
86 |
gr.Markdown("Login, pick a playlist, then shuffle & playβall in one flow.")
|
|
|
92 |
)
|
93 |
|
94 |
# Hidden container for the ?code=β¦ Spotify will append on redirect
|
95 |
+
code_box = gr.Textbox(visible=False, elem_id="auth_code")
|
96 |
+
status = gr.Markdown(visible=False)
|
97 |
+
dropdown = gr.Dropdown(choices=[], label="Step 2: Select a Playlist", visible=False)
|
98 |
+
info_html = gr.HTML(visible=False)
|
|
|
|
|
|
|
|
|
99 |
shuffle_btn = gr.Button("π Step 3: Shuffle & Play", visible=False)
|
100 |
+
result = gr.HTML(visible=False)
|
101 |
|
102 |
# Run on every page-load: grab ?code=β¦ via JS and call check_login
|
103 |
demo.load(
|
104 |
fn=check_login,
|
105 |
inputs=[code_box],
|
106 |
outputs=[status, dropdown],
|
107 |
+
js="""
|
108 |
+
const params = new URLSearchParams(window.location.search);
|
109 |
+
const code = params.get("code");
|
110 |
+
if (code) {
|
111 |
document
|
112 |
.getElementById("component-auth_code")
|
113 |
+
.querySelector("textarea").value = code;
|
114 |
}
|
115 |
"""
|
116 |
)
|