| |
| |
| |
| |
| |
|
|
|
|
| |
| |
| |
| |
| |
| |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| |
|
|
| |
| |
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
| |
| |
|
|
| import os |
| import spotipy |
| from spotipy.oauth2 import SpotifyOAuth |
| import gradio as gr |
| from urllib.parse import urlparse, parse_qs |
|
|
|
|
| redirect_uri = "https://huggingface.co/sjw" |
| scope= ['user-library-read', |
| 'user-read-playback-state', |
| 'user-modify-playback-state', |
| 'playlist-modify-public', |
| 'user-top-read'] |
|
|
|
|
| def spotify_auth(client_id, client_secret, code=None, state=gr.State()): |
| auth_manager = SpotifyOAuth(client_id=client_id, |
| client_secret=client_secret, |
| redirect_uri=redirect_uri, |
| scope=scope) |
| if code: |
| parsed_url = urlparse(code) |
| code = parse_qs(parsed_url.query)['code'][0] |
| auth_manager.get_access_token(code=code, as_dict=False) |
| sp = spotipy.Spotify(auth_manager=auth_manager) |
| devices = sp.devices() |
| device_id = devices['devices'][0]['id'] |
| |
| results = sp.search(q="No Pole", type='track') |
| track_uri = results['tracks']['items'][0]['uri'] |
| sp.start_playback(device_id=device_id, uris=[track_uri]) |
| |
| |
| state = auth_manager.get_access_token(as_dict=False) |
| print(state) |
| |
| return f"Account switched successfully.\n\n{devices}", state |
| else: |
| auth_url = auth_manager.get_authorize_url() |
| return f"Please authorize the app by clicking [here]({auth_url}) and then paste the code below.", state |
|
|
|
|
| with gr.Blocks() as demo: |
|
|
| with gr.Row(): |
| client_id = gr.Textbox(label="Client ID") |
| client_secret = gr.Textbox(label="Client Secret") |
| link_button = gr.Button("Get Link") |
| link_result = gr.Markdown() |
|
|
| link = gr.Textbox(label="Paste Link") |
| auth_button = gr.Button("Authorize") |
| auth_result = gr.Textbox() |
|
|
| link_button.click(spotify_auth, inputs=[client_id, client_secret, gr.State()], outputs=[link_result, gr.State()]) |
| auth_button.click(spotify_auth, inputs=[client_id, client_secret, link, gr.State()], outputs=[auth_result, gr.State()]) |
| demo.launch() |