Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -9,12 +9,7 @@ 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 |
|
12 |
-
SCOPE =
|
13 |
-
"streaming "
|
14 |
-
"user-read-playback-state "
|
15 |
-
"user-modify-playback-state "
|
16 |
-
"playlist-read-private"
|
17 |
-
)
|
18 |
|
19 |
sp_oauth = SpotifyOAuth(
|
20 |
client_id=CLIENT_ID,
|
@@ -30,14 +25,14 @@ user_playlists = {}
|
|
30 |
def get_auth_url():
|
31 |
return sp_oauth.get_authorize_url()
|
32 |
|
33 |
-
def
|
34 |
global sp, user_playlists
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
)
|
41 |
|
42 |
tokinfo = sp_oauth.get_access_token(code, as_dict=True)
|
43 |
access_token = tokinfo["access_token"]
|
@@ -49,9 +44,6 @@ def check_login(code: str):
|
|
49 |
sdk_js = f"""
|
50 |
<script>
|
51 |
window.ACCESS_TOKEN = "{access_token}";
|
52 |
-
if (window.self !== window.top) {{
|
53 |
-
window.top.location.href = window.location.href;
|
54 |
-
}}
|
55 |
</script>
|
56 |
<script src="https://sdk.scdn.co/spotify-player.js"></script>
|
57 |
<script>
|
@@ -71,16 +63,16 @@ def check_login(code: str):
|
|
71 |
"""
|
72 |
|
73 |
return (
|
74 |
-
gr.update(
|
75 |
gr.update(visible=True, choices=list(user_playlists.keys())),
|
76 |
-
gr.update(visible=True, value=sdk_js)
|
77 |
)
|
78 |
|
79 |
def load_playlist_info(playlist_name: str):
|
80 |
-
pid
|
81 |
data = sp.playlist(pid)
|
82 |
-
img
|
83 |
-
owner= data["owner"]["display_name"]
|
84 |
desc = data.get("description","")
|
85 |
html = f"""
|
86 |
<img src="{img}" width="300"/><br/>
|
@@ -114,37 +106,25 @@ def shuffle_and_play(playlist_name: str):
|
|
114 |
return gr.update(value="βΆοΈ Now playing in-browser!" + play_js, visible=True)
|
115 |
|
116 |
with gr.Blocks() as demo:
|
117 |
-
gr.
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
""")
|
124 |
-
gr.HTML('<a href="https://jisaacso219-rng-shuffle.hf.space/" target="_blank">Open standalone</a>')
|
125 |
-
|
126 |
-
gr.Markdown("Login β Select playlist β Shuffle & Play")
|
127 |
-
|
128 |
-
login_btn = gr.Button("π Step 1: Login to Spotify")
|
129 |
-
login_btn.click(
|
130 |
-
fn=None, inputs=None, outputs=None,
|
131 |
-
js=f"() => window.location.href = '{get_auth_url()}'"
|
132 |
-
)
|
133 |
|
134 |
-
|
135 |
-
status = gr.Markdown(visible=False)
|
136 |
playlist_dd = gr.Dropdown([], label="Select a Playlist", visible=False)
|
137 |
sdk_html = gr.HTML(visible=False)
|
138 |
|
139 |
info_html = gr.HTML(visible=False)
|
140 |
-
shuffle_btn = gr.Button("Shuffle & Play", visible=False)
|
141 |
result = gr.HTML(visible=False)
|
142 |
|
143 |
-
|
144 |
-
|
145 |
-
inputs=[
|
146 |
-
outputs=[status, playlist_dd, sdk_html]
|
147 |
-
js="() => new URLSearchParams(window.location.search).get('code') || ''"
|
148 |
)
|
149 |
|
150 |
playlist_dd.change(load_playlist_info, [playlist_dd], [info_html, shuffle_btn])
|
@@ -153,3 +133,4 @@ with gr.Blocks() as demo:
|
|
153 |
if __name__ == "__main__":
|
154 |
demo.launch(server_name="0.0.0.0", ssr_mode=False)
|
155 |
|
|
|
|
9 |
CLIENT_SECRET = os.environ["SPOTIFY_CLIENT_SECRET"]
|
10 |
REDIRECT_URI = "https://jisaacso219-rng-shuffle.hf.space/"
|
11 |
|
12 |
+
SCOPE = "streaming user-read-playback-state user-modify-playback-state playlist-read-private"
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
sp_oauth = SpotifyOAuth(
|
15 |
client_id=CLIENT_ID,
|
|
|
25 |
def get_auth_url():
|
26 |
return sp_oauth.get_authorize_url()
|
27 |
|
28 |
+
def complete_login(full_url):
|
29 |
global sp, user_playlists
|
30 |
+
|
31 |
+
# Extract code from full URL
|
32 |
+
try:
|
33 |
+
code = full_url.split("?code=")[1].split("&")[0]
|
34 |
+
except IndexError:
|
35 |
+
return gr.update(value="β No code found in URL. Please try logging in again."), gr.update(visible=False), gr.update(visible=False)
|
36 |
|
37 |
tokinfo = sp_oauth.get_access_token(code, as_dict=True)
|
38 |
access_token = tokinfo["access_token"]
|
|
|
44 |
sdk_js = f"""
|
45 |
<script>
|
46 |
window.ACCESS_TOKEN = "{access_token}";
|
|
|
|
|
|
|
47 |
</script>
|
48 |
<script src="https://sdk.scdn.co/spotify-player.js"></script>
|
49 |
<script>
|
|
|
63 |
"""
|
64 |
|
65 |
return (
|
66 |
+
gr.update(value="β
Logged in! Select a playlist."),
|
67 |
gr.update(visible=True, choices=list(user_playlists.keys())),
|
68 |
+
gr.update(visible=True, value=sdk_js)
|
69 |
)
|
70 |
|
71 |
def load_playlist_info(playlist_name: str):
|
72 |
+
pid = user_playlists[playlist_name]
|
73 |
data = sp.playlist(pid)
|
74 |
+
img = data["images"][0]["url"] if data["images"] else ""
|
75 |
+
owner = data["owner"]["display_name"]
|
76 |
desc = data.get("description","")
|
77 |
html = f"""
|
78 |
<img src="{img}" width="300"/><br/>
|
|
|
106 |
return gr.update(value="βΆοΈ Now playing in-browser!" + play_js, visible=True)
|
107 |
|
108 |
with gr.Blocks() as demo:
|
109 |
+
gr.Markdown("### RNG Spotify Playlist Shuffler π§")
|
110 |
+
login_btn = gr.Button("π Login to Spotify", link=get_auth_url(), target="_blank")
|
111 |
+
|
112 |
+
gr.Markdown("After logging in to Spotify, copy the **full redirected URL** and paste it below to complete login.")
|
113 |
+
redirect_url_box = gr.Textbox(label="Paste redirected URL here")
|
114 |
+
complete_login_btn = gr.Button("β
Complete Login")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
+
status = gr.Markdown()
|
|
|
117 |
playlist_dd = gr.Dropdown([], label="Select a Playlist", visible=False)
|
118 |
sdk_html = gr.HTML(visible=False)
|
119 |
|
120 |
info_html = gr.HTML(visible=False)
|
121 |
+
shuffle_btn = gr.Button("π Shuffle & Play", visible=False)
|
122 |
result = gr.HTML(visible=False)
|
123 |
|
124 |
+
complete_login_btn.click(
|
125 |
+
complete_login,
|
126 |
+
inputs=[redirect_url_box],
|
127 |
+
outputs=[status, playlist_dd, sdk_html]
|
|
|
128 |
)
|
129 |
|
130 |
playlist_dd.change(load_playlist_info, [playlist_dd], [info_html, shuffle_btn])
|
|
|
133 |
if __name__ == "__main__":
|
134 |
demo.launch(server_name="0.0.0.0", ssr_mode=False)
|
135 |
|
136 |
+
|