Spaces:
Running
Running
import os | |
import json | |
import random | |
import gradio as gr | |
import spotipy | |
from spotipy.oauth2 import SpotifyOAuth | |
CLIENT_ID = os.environ["SPOTIFY_CLIENT_ID"] | |
CLIENT_SECRET = os.environ["SPOTIFY_CLIENT_SECRET"] | |
REDIRECT_URI = "https://jisaacso219-rng-shuffle.hf.space/" | |
SCOPE = "streaming user-read-playback-state user-modify-playback-state playlist-read-private" | |
sp_oauth = SpotifyOAuth( | |
client_id=CLIENT_ID, | |
client_secret=CLIENT_SECRET, | |
redirect_uri=REDIRECT_URI, | |
scope=SCOPE, | |
show_dialog=True, | |
) | |
sp = None | |
user_playlists = {} | |
def get_auth_url(): | |
return sp_oauth.get_authorize_url() | |
def complete_login(full_url): | |
global sp, user_playlists | |
# Extract code from full URL | |
try: | |
code = full_url.split("?code=")[1].split("&")[0] | |
except IndexError: | |
return gr.update(value="β No code found in URL. Please try logging in again."), gr.update(visible=False), gr.update(visible=False) | |
tokinfo = sp_oauth.get_access_token(code, as_dict=True) | |
access_token = tokinfo["access_token"] | |
sp = spotipy.Spotify(auth=access_token) | |
items = sp.current_user_playlists(limit=50)["items"] | |
user_playlists = {p["name"]: p["id"] for p in items} | |
sdk_js = f""" | |
<script> | |
window.ACCESS_TOKEN = "{access_token}"; | |
</script> | |
<script src="https://sdk.scdn.co/spotify-player.js"></script> | |
<script> | |
window.onSpotifyWebPlaybackSDKReady = () => {{ | |
const player = new Spotify.Player({{ | |
name: 'RNG Web Player', | |
getOAuthToken: cb => cb(window.ACCESS_TOKEN), | |
volume: 0.5 | |
}}); | |
player.addListener('ready', ({ device_id }) => {{ | |
window._webDeviceId = device_id; | |
console.log('device_id:', device_id); | |
}}); | |
player.connect(); | |
}}; | |
</script> | |
""" | |
return ( | |
gr.update(value="β Logged in! Select a playlist."), | |
gr.update(visible=True, choices=list(user_playlists.keys())), | |
gr.update(visible=True, value=sdk_js) | |
) | |
def load_playlist_info(playlist_name: str): | |
pid = user_playlists[playlist_name] | |
data = sp.playlist(pid) | |
img = data["images"][0]["url"] if data["images"] else "" | |
owner = data["owner"]["display_name"] | |
desc = data.get("description","") | |
html = f""" | |
<img src="{img}" width="300"/><br/> | |
<strong>{playlist_name}</strong> by {owner}<br/>{desc} | |
""" | |
return html, gr.update(visible=True) | |
def shuffle_and_play(playlist_name: str): | |
pid = user_playlists[playlist_name] | |
tracks, res = [], sp.playlist_tracks(pid) | |
tracks.extend(res["items"]) | |
while res["next"]: | |
res = sp.next(res) | |
tracks.extend(res["items"]) | |
uris = [t["track"]["uri"] for t in tracks if t["track"]] | |
random.shuffle(uris) | |
uris_json = json.dumps(uris) | |
play_js = f""" | |
<script> | |
fetch('https://api.spotify.com/v1/me/player/play?device_id=' + window._webDeviceId, {{ | |
method: 'PUT', | |
headers: {{ | |
'Authorization': 'Bearer ' + window.ACCESS_TOKEN, | |
'Content-Type': 'application/json' | |
}}, | |
body: JSON.stringify({{ uris: {uris_json} }}) | |
}}); | |
</script> | |
""" | |
return gr.update(value="βΆοΈ Now playing in-browser!" + play_js, visible=True) | |
with gr.Blocks() as demo: | |
gr.Markdown("### RNG Spotify Playlist Shuffler π§") | |
login_url = get_auth_url() | |
login_btn = gr.HTML(f'<a href="{login_url}" target="_blank"><button style="width:100%; height:40px; font-size:16px;">π Login to Spotify</button></a>') | |
gr.Markdown("After logging in to Spotify, copy the **full redirected URL** and paste it below to complete login.") | |
redirect_url_box = gr.Textbox(label="Paste redirected URL here") | |
complete_login_btn = gr.Button("β Complete Login") | |
status = gr.Markdown() | |
playlist_dd = gr.Dropdown([], label="Select a Playlist", visible=False) | |
sdk_html = gr.HTML(visible=False) | |
info_html = gr.HTML(visible=False) | |
shuffle_btn = gr.Button("π Shuffle & Play", visible=False) | |
result = gr.HTML(visible=False) | |
complete_login_btn.click( | |
complete_login, | |
inputs=[redirect_url_box], | |
outputs=[status, playlist_dd, sdk_html] | |
) | |
playlist_dd.change(load_playlist_info, [playlist_dd], [info_html, shuffle_btn]) | |
shuffle_btn.click(shuffle_and_play, [playlist_dd], [result]) | |
if __name__ == "__main__": | |
demo.launch(server_name="0.0.0.0", ssr_mode=False) | |