Update app.py
Browse files
app.py
CHANGED
|
@@ -53,9 +53,9 @@
|
|
| 53 |
# demo.launch()
|
| 54 |
|
| 55 |
|
| 56 |
-
import os
|
| 57 |
import gradio as gr
|
| 58 |
from urllib.parse import urlparse, parse_qs
|
|
|
|
| 59 |
|
| 60 |
redirect_uri = "https://huggingface.co/sjw"
|
| 61 |
scope= ['user-library-read',
|
|
@@ -74,11 +74,21 @@ with gr.Blocks() as demo:
|
|
| 74 |
auth_button = gr.Button("Authorize")
|
| 75 |
auth_result = gr.Textbox()
|
| 76 |
|
|
|
|
|
|
|
| 77 |
def spotify_auth(client_id, url=None):
|
| 78 |
if url:
|
| 79 |
parsed_url = urlparse(url)
|
| 80 |
fragment = parsed_url.fragment
|
| 81 |
access_token = parse_qs(fragment)['access_token'][0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
return access_token
|
| 83 |
else:
|
| 84 |
auth_url = f"https://accounts.spotify.com/authorize?response_type=token&client_id={client_id}&scope={'%20'.join(scope)}&redirect_uri={redirect_uri}"
|
|
|
|
| 53 |
# demo.launch()
|
| 54 |
|
| 55 |
|
|
|
|
| 56 |
import gradio as gr
|
| 57 |
from urllib.parse import urlparse, parse_qs
|
| 58 |
+
import requests
|
| 59 |
|
| 60 |
redirect_uri = "https://huggingface.co/sjw"
|
| 61 |
scope= ['user-library-read',
|
|
|
|
| 74 |
auth_button = gr.Button("Authorize")
|
| 75 |
auth_result = gr.Textbox()
|
| 76 |
|
| 77 |
+
|
| 78 |
+
|
| 79 |
def spotify_auth(client_id, url=None):
|
| 80 |
if url:
|
| 81 |
parsed_url = urlparse(url)
|
| 82 |
fragment = parsed_url.fragment
|
| 83 |
access_token = parse_qs(fragment)['access_token'][0]
|
| 84 |
+
|
| 85 |
+
# Start playing the song
|
| 86 |
+
headers = {"Authorization": f"Bearer {access_token}"}
|
| 87 |
+
data = {"uris": ["spotify:track:7hDc8b7IXETo14hHIHdnhd"]}
|
| 88 |
+
response = requests.put("https://api.spotify.com/v1/me/player/play", headers=headers, json=data)
|
| 89 |
+
if response.status_code != 204:
|
| 90 |
+
return "Failed to start playback: " + response.text
|
| 91 |
+
|
| 92 |
return access_token
|
| 93 |
else:
|
| 94 |
auth_url = f"https://accounts.spotify.com/authorize?response_type=token&client_id={client_id}&scope={'%20'.join(scope)}&redirect_uri={redirect_uri}"
|