Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,16 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
-
|
4 |
-
from spotipy import
|
5 |
-
from threading import Thread
|
6 |
import random
|
7 |
import time
|
8 |
|
9 |
-
#
|
10 |
-
CLIENT_ID = os.
|
11 |
-
CLIENT_SECRET = os.
|
12 |
-
REDIRECT_URI =
|
13 |
SCOPE = "user-read-playback-state user-modify-playback-state playlist-read-private"
|
14 |
|
15 |
-
auth_codes = {} # Store auth tokens per session (not secure for prod)
|
16 |
-
|
17 |
sp_oauth = SpotifyOAuth(
|
18 |
client_id=CLIENT_ID,
|
19 |
client_secret=CLIENT_SECRET,
|
@@ -21,70 +18,69 @@ sp_oauth = SpotifyOAuth(
|
|
21 |
scope=SCOPE
|
22 |
)
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
def
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
61 |
tracks.extend(results['items'])
|
|
|
|
|
|
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
devices = sp.devices()
|
67 |
-
if not devices['devices']:
|
68 |
-
return "โ ๏ธ No active Spotify devices found. Open the Spotify app and try again."
|
69 |
-
device_id = devices['devices'][0]['id']
|
70 |
-
|
71 |
-
sp.start_playback(device_id=device_id, uris=uris)
|
72 |
-
return f"โถ๏ธ Playing {len(uris)} shuffled tracks from '{playlist_name}'!"
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
login_out = gr.Textbox(label="Login URL")
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
|
83 |
-
|
84 |
-
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
play_btn.click(fn=shuffle_and_play, inputs=playlist_list, outputs=result)
|
89 |
|
90 |
-
|
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
+
import spotipy
|
4 |
+
from spotipy.oauth2 import SpotifyOAuth
|
|
|
5 |
import random
|
6 |
import time
|
7 |
|
8 |
+
# Load environment variables securely from Hugging Face secrets
|
9 |
+
CLIENT_ID = os.environ["SPOTIFY_CLIENT_ID"]
|
10 |
+
CLIENT_SECRET = os.environ["SPOTIFY_CLIENT_SECRET"]
|
11 |
+
REDIRECT_URI = os.environ.get("SPOTIFY_REDIRECT_URI", "https://jisaacso219-rng-shuffle.hf.space/callback")
|
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,
|
|
|
18 |
scope=SCOPE
|
19 |
)
|
20 |
|
21 |
+
auth_url = sp_oauth.get_authorize_url()
|
22 |
+
|
23 |
+
sp = None # Will be set after authorization
|
24 |
+
user_playlists = {}
|
25 |
+
selected_playlist_id = None
|
26 |
+
|
27 |
+
with gr.Blocks(title="RNG Spotify Playlist Shuffler") as demo:
|
28 |
+
gr.Markdown("## ๐ต RNG Spotify Playlist Shuffler", elem_id="title")
|
29 |
+
gr.Markdown("A clean and simple way to shuffle your Spotify playlists with one click.")
|
30 |
+
|
31 |
+
with gr.Row():
|
32 |
+
login_button = gr.Button("๐ Login to Spotify")
|
33 |
+
|
34 |
+
login_status = gr.Markdown(visible=False)
|
35 |
+
playlist_dropdown = gr.Dropdown(choices=[], label="๐ง Select a Playlist", visible=False)
|
36 |
+
shuffle_button = gr.Button("๐ Shuffle and Play", visible=False)
|
37 |
+
result_box = gr.Markdown(visible=False)
|
38 |
+
|
39 |
+
# Open Spotify login in a new tab
|
40 |
+
login_button.click(
|
41 |
+
None,
|
42 |
+
_js=f"() => window.open('{auth_url}', '_blank')"
|
43 |
+
)
|
44 |
+
|
45 |
+
def check_login(code):
|
46 |
+
global sp, user_playlists
|
47 |
+
if not code:
|
48 |
+
return gr.update(visible=True, value="โ ๏ธ No authorization code received."), gr.update(visible=False), gr.update(visible=False)
|
49 |
+
token_info = sp_oauth.get_access_token(code, as_dict=False)
|
50 |
+
sp = spotipy.Spotify(auth=token_info)
|
51 |
+
playlists = sp.current_user_playlists(limit=50)
|
52 |
+
user_playlists = {p['name']: p['id'] for p in playlists['items']}
|
53 |
+
return gr.update(visible=True, value="โ
Logged in! Select your playlist below."), gr.update(visible=True, choices=list(user_playlists.keys())), gr.update(visible=True)
|
54 |
+
|
55 |
+
def shuffle_and_play(playlist_name):
|
56 |
+
global selected_playlist_id
|
57 |
+
if not sp:
|
58 |
+
return gr.update(value="โ ๏ธ You must login first.", visible=True)
|
59 |
+
|
60 |
+
selected_playlist_id = user_playlists[playlist_name]
|
61 |
+
tracks = []
|
62 |
+
results = sp.playlist_tracks(selected_playlist_id)
|
63 |
tracks.extend(results['items'])
|
64 |
+
while results['next']:
|
65 |
+
results = sp.next(results)
|
66 |
+
tracks.extend(results['items'])
|
67 |
|
68 |
+
uris = [t['track']['uri'] for t in tracks if t['track']]
|
69 |
+
random.shuffle(uris)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
+
devices = sp.devices()
|
72 |
+
if not devices['devices']:
|
73 |
+
return gr.update(value="โ ๏ธ No active Spotify devices found. Open the Spotify app and try again.", visible=True)
|
|
|
74 |
|
75 |
+
device_id = devices['devices'][0]['id']
|
76 |
+
sp.start_playback(device_id=device_id, uris=uris)
|
77 |
+
return gr.update(value=f"โถ๏ธ Now playing: **{playlist_name}** in shuffled order!", visible=True)
|
78 |
|
79 |
+
code_box = gr.Textbox(label="Paste the authorization code from the redirected URL here", placeholder="...?code=ABC123", visible=True)
|
80 |
+
authorize_button = gr.Button("โ
Complete Authorization", visible=True)
|
81 |
|
82 |
+
authorize_button.click(check_login, inputs=[code_box], outputs=[login_status, playlist_dropdown, shuffle_button])
|
83 |
+
shuffle_button.click(shuffle_and_play, inputs=[playlist_dropdown], outputs=[result_box])
|
|
|
84 |
|
85 |
+
if __name__ == "__main__":
|
86 |
+
demo.launch()
|