jisaacso219 commited on
Commit
a341963
Β·
verified Β·
1 Parent(s): 59e2f6d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +167 -0
app.py ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import random
3
+ import time
4
+ import threading
5
+ import gradio as gr
6
+ import spotipy
7
+ from spotipy.oauth2 import SpotifyOAuth
8
+
9
+ CLIENT_ID = os.environ["SPOTIFY_CLIENT_ID"]
10
+ CLIENT_SECRET = os.environ["SPOTIFY_CLIENT_SECRET"]
11
+ REDIRECT_URI = "https://jisaacso219-rng-shuffle.hf.space/"
12
+ SCOPE = "user-read-playback-state user-modify-playback-state playlist-read-private"
13
+
14
+ sp_oauth = SpotifyOAuth(
15
+ client_id=CLIENT_ID,
16
+ client_secret=CLIENT_SECRET,
17
+ redirect_uri=REDIRECT_URI,
18
+ scope=SCOPE,
19
+ show_dialog=True,
20
+ )
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
+ "πŸ” 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
+ try:
42
+ token_info = sp_oauth.get_cached_token()
43
+ if not token_info:
44
+ token_info = sp_oauth.get_access_token(code)
45
+ access_token = token_info["access_token"]
46
+ except Exception as e:
47
+ print("πŸ” Token refresh failed:", str(e))
48
+ return (
49
+ f"❌ Failed to log in: {e}",
50
+ gr.update(visible=False),
51
+ gr.update(visible=False),
52
+ gr.update(visible=False),
53
+ )
54
+
55
+ sp = spotipy.Spotify(auth=access_token)
56
+
57
+ try:
58
+ playlists = sp.current_user_playlists(limit=50)["items"]
59
+ except Exception as e:
60
+ return f"❌ Failed to get playlists: {e}", *[gr.update(visible=False)] * 3
61
+
62
+ user_playlists.clear()
63
+ for p in playlists:
64
+ user_playlists[p["name"]] = p["id"]
65
+
66
+ devices = sp.devices()["devices"]
67
+ device_map.clear()
68
+ for d in devices:
69
+ device_map[d["name"]] = d["id"]
70
+
71
+ if not device_map:
72
+ return (
73
+ "⚠️ No active Spotify devices found.",
74
+ gr.update(visible=False),
75
+ gr.update(visible=False),
76
+ gr.update(visible=False),
77
+ )
78
+
79
+ return (
80
+ "βœ… Logged in!",
81
+ gr.update(visible=True, choices=list(user_playlists.keys())),
82
+ gr.update(visible=True, choices=list(device_map.keys())),
83
+ gr.update(visible=True),
84
+ )
85
+
86
+ def background_queue_tracks(uris, device_id):
87
+ for i, uri in enumerate(uris):
88
+ try:
89
+ sp.add_to_queue(uri, device_id=device_id)
90
+ time.sleep(0.25)
91
+ except Exception as e:
92
+ print(f"[Queue Error] Track {i}: {e}")
93
+ time.sleep(1)
94
+
95
+ def now_playing():
96
+ try:
97
+ data = sp.current_playback()
98
+ if not data or not data.get("is_playing"):
99
+ return "⚠️ Nothing is currently playing."
100
+ track = data["item"]
101
+ name = track["name"]
102
+ artist = ", ".join([a["name"] for a in track["artists"]])
103
+ album = track["album"]["name"]
104
+ image = track["album"]["images"][0]["url"]
105
+ html = f"""
106
+ <img src='{image}' width='300'><br>
107
+ <b>{name}</b> by {artist}<br/>
108
+ <i>{album}</i>
109
+ """
110
+ return html
111
+ except Exception as e:
112
+ return f"❌ Error fetching current track: {e}"
113
+
114
+ def delayed_now_playing(output):
115
+ time.sleep(10)
116
+ output.update(now_playing())
117
+
118
+ def shuffle_and_play(playlist_name, device_name):
119
+ pid = user_playlists.get(playlist_name)
120
+ device_id = device_map.get(device_name)
121
+
122
+ if not pid or not device_id:
123
+ return "❌ Invalid playlist or device."
124
+
125
+ tracks, res = [], sp.playlist_tracks(pid)
126
+ tracks.extend(res["items"])
127
+ while res["next"]:
128
+ res = sp.next(res)
129
+ tracks.extend(res["items"])
130
+
131
+ uris = [t["track"]["uri"] for t in tracks if t["track"]]
132
+ random.shuffle(uris)
133
+
134
+ try:
135
+ sp.start_playback(device_id=device_id, uris=uris[:100])
136
+ threading.Thread(target=background_queue_tracks, args=(uris[100:200], device_id)).start()
137
+ return "βœ… Shuffling... Song info will display in ~10 seconds."
138
+ except Exception as e:
139
+ return f"❌ Playback failed: {str(e)}"
140
+
141
+ with gr.Blocks() as demo:
142
+ gr.Markdown("## 🎡 RNG Spotify Playlist Shuffler")
143
+ gr.HTML(f'<a href="{get_auth_url()}"><button style="width:100%;height:40px;">πŸ” Login to Spotify</button></a>')
144
+
145
+ url_state = gr.Textbox(visible=False)
146
+ status = gr.Markdown()
147
+ playlist_dd = gr.Dropdown(label="Step 2: Select a Playlist", visible=False)
148
+ device_dd = gr.Dropdown(label="Step 3: Select a Device", visible=False)
149
+ shuffle_btn = gr.Button("πŸ”€ Step 4: Shuffle & Play", visible=False)
150
+ result = gr.HTML()
151
+
152
+ demo.load(
153
+ fn=try_login,
154
+ inputs=[url_state],
155
+ outputs=[status, playlist_dd, device_dd, shuffle_btn],
156
+ js="() => { return [window.location.href]; }"
157
+ )
158
+
159
+ def handle_shuffle(playlist_name, device_name):
160
+ result_text = shuffle_and_play(playlist_name, device_name)
161
+ threading.Thread(target=delayed_now_playing, args=(result,)).start()
162
+ return result_text
163
+
164
+ shuffle_btn.click(handle_shuffle, [playlist_dd, device_dd], [result])
165
+
166
+ if __name__ == "__main__":
167
+ demo.launch(server_name="0.0.0.0", ssr_mode=False)