jisaacso219 commited on
Commit
3582f7d
Β·
verified Β·
1 Parent(s): 6645382

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -45
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 check_login(code: str):
34
  global sp, user_playlists
35
- if not code:
36
- return (
37
- gr.update(visible=False),
38
- gr.update(visible=False),
39
- gr.update(visible=False),
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(visible=True, value="βœ… Logged in! Select a playlist below."),
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 = user_playlists[playlist_name]
81
  data = sp.playlist(pid)
82
- img = data["images"][0]["url"] if data["images"] else ""
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.HTML("""
118
- <script>
119
- if (window.self !== window.top) {
120
- window.top.location.href = window.location.href;
121
- }
122
- </script>
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
- code_state = gr.Textbox(visible=False) # βœ… Critical Fix
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
- demo.load(
144
- fn=check_login,
145
- inputs=[code_state],
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
+