Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,30 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from urllib.parse import urlparse, parse_qs
|
| 3 |
-
import requests
|
| 4 |
import spotipy
|
| 5 |
|
|
|
|
| 6 |
redirect_uri = "https://huggingface.co/sjw"
|
| 7 |
-
scope= ['user-library-read',
|
| 8 |
'user-read-playback-state',
|
| 9 |
'user-modify-playback-state',
|
| 10 |
'playlist-modify-public',
|
| 11 |
'user-top-read']
|
| 12 |
|
| 13 |
-
with gr.Blocks() as demo:
|
| 14 |
-
with gr.Row():
|
| 15 |
-
client_id = gr.Textbox(label="Client ID")
|
| 16 |
-
link_button = gr.Button("Get Link")
|
| 17 |
-
link_result = gr.Markdown()
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
auth_result = gr.Textbox()
|
| 22 |
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def spotify_auth(client_id, url=None):
|
| 26 |
if url:
|
|
@@ -30,16 +34,21 @@ with gr.Blocks() as demo:
|
|
| 30 |
|
| 31 |
sp = spotipy.Spotify(auth=access_token)
|
| 32 |
device_id = sp.devices()['devices'][0]['id']
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
track_uri = results['tracks']['items'][0]['uri']
|
| 35 |
-
|
| 36 |
|
| 37 |
return access_token
|
| 38 |
else:
|
| 39 |
auth_url = f"https://accounts.spotify.com/authorize?response_type=token&client_id={client_id}&scope={'%20'.join(scope)}&redirect_uri={redirect_uri}"
|
| 40 |
return f"Please authorize the app by clicking [here]({auth_url}) and then paste the URL you are redirected to below."
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
|
| 45 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from urllib.parse import urlparse, parse_qs
|
|
|
|
| 3 |
import spotipy
|
| 4 |
|
| 5 |
+
|
| 6 |
redirect_uri = "https://huggingface.co/sjw"
|
| 7 |
+
scope = ['user-library-read',
|
| 8 |
'user-read-playback-state',
|
| 9 |
'user-modify-playback-state',
|
| 10 |
'playlist-modify-public',
|
| 11 |
'user-top-read']
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
sp_state = gr.State()
|
| 15 |
+
device_id_state = gr.State()
|
|
|
|
| 16 |
|
| 17 |
|
| 18 |
+
with gr.Blocks() as auth_page:
|
| 19 |
+
|
| 20 |
+
with gr.Row():
|
| 21 |
+
client_id = gr.Textbox(label="Spotify Client ID")
|
| 22 |
+
generate_link = gr.Button("Get Authentication Link")
|
| 23 |
+
display_link = gr.Markdown()
|
| 24 |
+
|
| 25 |
+
url = gr.Textbox(label="Paste URL")
|
| 26 |
+
authorize_url = gr.Button("Authorize URL")
|
| 27 |
+
auth_result = gr.Textbox()
|
| 28 |
|
| 29 |
def spotify_auth(client_id, url=None):
|
| 30 |
if url:
|
|
|
|
| 34 |
|
| 35 |
sp = spotipy.Spotify(auth=access_token)
|
| 36 |
device_id = sp.devices()['devices'][0]['id']
|
| 37 |
+
|
| 38 |
+
sp_state.value = sp
|
| 39 |
+
device_id_state.value = device_id
|
| 40 |
+
print(sp_state.value, device_id_state.value)
|
| 41 |
+
|
| 42 |
+
results = sp_state.value.search(q="Passionfruit", type='track')
|
| 43 |
track_uri = results['tracks']['items'][0]['uri']
|
| 44 |
+
sp_state.value.start_playback(device_id=device_id_state.value, uris=[track_uri])
|
| 45 |
|
| 46 |
return access_token
|
| 47 |
else:
|
| 48 |
auth_url = f"https://accounts.spotify.com/authorize?response_type=token&client_id={client_id}&scope={'%20'.join(scope)}&redirect_uri={redirect_uri}"
|
| 49 |
return f"Please authorize the app by clicking [here]({auth_url}) and then paste the URL you are redirected to below."
|
| 50 |
|
| 51 |
+
generate_link.click(spotify_auth, inputs=[client_id], outputs=display_link)
|
| 52 |
+
authorize_url.click(spotify_auth, inputs=[client_id, url], outputs=auth_result)
|
| 53 |
|
| 54 |
+
auth_page.launch()
|