Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from flask import Flask, request, redirect
|
4 |
+
from spotipy import Spotify, SpotifyOAuth
|
5 |
+
from threading import Thread
|
6 |
+
import random
|
7 |
+
import time
|
8 |
+
|
9 |
+
# ========== Spotify OAuth Setup ==========
|
10 |
+
CLIENT_ID = os.getenv("SPOTIFY_CLIENT_ID")
|
11 |
+
CLIENT_SECRET = os.getenv("SPOTIFY_CLIENT_SECRET")
|
12 |
+
REDIRECT_URI = REDIRECT_URI = "https://jisaacso219-rng-shuffle.hf.space/callback"
|
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,
|
20 |
+
redirect_uri=REDIRECT_URI,
|
21 |
+
scope=SCOPE
|
22 |
+
)
|
23 |
+
|
24 |
+
# ========== Flask Redirect Server ==========
|
25 |
+
app = Flask(__name__)
|
26 |
+
|
27 |
+
@app.route("/callback")
|
28 |
+
def callback():
|
29 |
+
code = request.args.get("code")
|
30 |
+
token_info = sp_oauth.get_access_token(code, as_dict=True)
|
31 |
+
auth_codes["user"] = token_info # Single-user session for now
|
32 |
+
return "✅ Spotify login successful! You can return to the app."
|
33 |
+
|
34 |
+
Thread(target=lambda: app.run(host="0.0.0.0", port=7861)).start()
|
35 |
+
|
36 |
+
# ========== Gradio UI ==========
|
37 |
+
def login():
|
38 |
+
return sp_oauth.get_authorize_url()
|
39 |
+
|
40 |
+
def list_playlists():
|
41 |
+
if "user" not in auth_codes:
|
42 |
+
return [], "⚠️ Please log in first."
|
43 |
+
sp = Spotify(auth=auth_codes["user"]["access_token"])
|
44 |
+
pl = sp.current_user_playlists(limit=50)
|
45 |
+
choices = {p['name']: p['id'] for p in pl['items']}
|
46 |
+
return list(choices.keys()), "Select a playlist to shuffle and play."
|
47 |
+
|
48 |
+
def shuffle_and_play(playlist_name):
|
49 |
+
if "user" not in auth_codes:
|
50 |
+
return "⚠️ Please log in first."
|
51 |
+
sp = Spotify(auth=auth_codes["user"]["access_token"])
|
52 |
+
pl = sp.current_user_playlists(limit=50)
|
53 |
+
pl_map = {p['name']: p['id'] for p in pl['items']}
|
54 |
+
playlist_id = pl_map[playlist_name]
|
55 |
+
|
56 |
+
tracks = []
|
57 |
+
results = sp.playlist_tracks(playlist_id)
|
58 |
+
tracks.extend(results['items'])
|
59 |
+
while results['next']:
|
60 |
+
results = sp.next(results)
|
61 |
+
tracks.extend(results['items'])
|
62 |
+
|
63 |
+
uris = [t['track']['uri'] for t in tracks if t['track']]
|
64 |
+
random.shuffle(uris)
|
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 |
+
with gr.Blocks() as demo:
|
75 |
+
gr.Markdown("# 🎶 Spotify Playlist Shuffler")
|
76 |
+
login_btn = gr.Button("🔐 Log in to Spotify")
|
77 |
+
login_out = gr.Textbox(label="Login URL")
|
78 |
+
|
79 |
+
playlist_list = gr.Dropdown(label="Your Playlists")
|
80 |
+
refresh_btn = gr.Button("🔄 Load My Playlists")
|
81 |
+
status = gr.Textbox()
|
82 |
+
|
83 |
+
play_btn = gr.Button("▶️ Shuffle + Play")
|
84 |
+
result = gr.Textbox()
|
85 |
+
|
86 |
+
login_btn.click(fn=login, outputs=login_out)
|
87 |
+
refresh_btn.click(fn=list_playlists, outputs=[playlist_list, status])
|
88 |
+
play_btn.click(fn=shuffle_and_play, inputs=playlist_list, outputs=result)
|
89 |
+
|
90 |
+
demo.launch(server_port=7860, server_name="0.0.0.0")
|