Update app.py
Browse files
app.py
CHANGED
|
@@ -56,6 +56,7 @@
|
|
| 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',
|
|
@@ -78,16 +79,21 @@ with gr.Blocks() as demo:
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
return access_token
|
| 93 |
else:
|
|
@@ -97,4 +103,4 @@ with gr.Blocks() as demo:
|
|
| 97 |
link_button.click(spotify_auth, inputs=[client_id], outputs=link_result)
|
| 98 |
auth_button.click(spotify_auth, inputs=[client_id, link], outputs=auth_result)
|
| 99 |
|
| 100 |
-
demo.launch()
|
|
|
|
| 56 |
import gradio as gr
|
| 57 |
from urllib.parse import urlparse, parse_qs
|
| 58 |
import requests
|
| 59 |
+
import spotipy
|
| 60 |
|
| 61 |
redirect_uri = "https://huggingface.co/sjw"
|
| 62 |
scope= ['user-library-read',
|
|
|
|
| 79 |
|
| 80 |
def spotify_auth(client_id, url=None):
|
| 81 |
if url:
|
| 82 |
+
parsed_url = urlparse(url),
|
| 83 |
fragment = parsed_url.fragment
|
| 84 |
access_token = parse_qs(fragment)['access_token'][0]
|
| 85 |
|
| 86 |
+
# # Start playing the song
|
| 87 |
+
# headers = {"Authorization": f"Bearer {access_token}"}
|
| 88 |
+
# data = {"uris": ["spotify:track:7hDc8b7IXETo14hHIHdnhd"]}
|
| 89 |
+
# response = requests.put("https://api.spotify.com/v1/me/player/play", headers=headers, json=data)
|
| 90 |
+
# if response.status_code != 204:
|
| 91 |
+
# return "Failed to start playback: " + response.text
|
| 92 |
+
sp = spotipy.Spotify(auth=access_token)
|
| 93 |
+
device_id = sp.devices()['devices'][0]['id']
|
| 94 |
+
results = sp.search(q="Passionfruit", type='track')
|
| 95 |
+
track_uri = results['tracks']['items'][0]['uri']
|
| 96 |
+
sp.start_playback(device_id=device_id, uris=[track_uri])
|
| 97 |
|
| 98 |
return access_token
|
| 99 |
else:
|
|
|
|
| 103 |
link_button.click(spotify_auth, inputs=[client_id], outputs=link_result)
|
| 104 |
auth_button.click(spotify_auth, inputs=[client_id, link], outputs=auth_result)
|
| 105 |
|
| 106 |
+
demo.launch()
|