Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,38 +2,41 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
| 9 |
-
def
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
return "❌ No encontré SPOTIFY_CLIENT_IDS o SPOTIFY_CLIENT_SECRETS"
|
| 15 |
-
|
| 16 |
-
# 2️⃣ Solicita token
|
| 17 |
-
resp = requests.post(
|
| 18 |
"https://accounts.spotify.com/api/token",
|
| 19 |
-
headers={"Content-Type":
|
| 20 |
-
data={"grant_type":
|
| 21 |
-
auth=(
|
| 22 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
if resp.status_code == 200:
|
| 24 |
-
|
| 25 |
-
return f"✅
|
| 26 |
else:
|
| 27 |
-
return f"❌
|
| 28 |
|
| 29 |
-
# 3️⃣ Monta la interfaz Gradio
|
| 30 |
iface = gr.Interface(
|
| 31 |
-
fn=
|
| 32 |
-
inputs=
|
| 33 |
outputs="text",
|
| 34 |
-
title="
|
| 35 |
-
description="
|
| 36 |
)
|
| 37 |
|
| 38 |
-
if __name__
|
| 39 |
iface.launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import requests
|
| 4 |
|
| 5 |
+
# Credenciales (solo tomamos el primer par)
|
| 6 |
+
CID = os.getenv("SPOTIFY_CLIENT_IDS", "").split(',')[0]
|
| 7 |
+
CSEC = os.getenv("SPOTIFY_CLIENT_SECRETS", "").split(',')[0]
|
| 8 |
|
| 9 |
+
def test_playlist(playlist_url):
|
| 10 |
+
if not (CID and CSEC):
|
| 11 |
+
return "❌ Faltan credenciales"
|
| 12 |
+
# 1) Obtener token
|
| 13 |
+
tok_resp = requests.post(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
"https://accounts.spotify.com/api/token",
|
| 15 |
+
headers={"Content-Type":"application/x-www-form-urlencoded"},
|
| 16 |
+
data={"grant_type":"client_credentials"},
|
| 17 |
+
auth=(CID, CSEC)
|
| 18 |
)
|
| 19 |
+
if tok_resp.status_code != 200:
|
| 20 |
+
return f"❌ Token error {tok_resp.status_code}"
|
| 21 |
+
token = tok_resp.json().get("access_token")
|
| 22 |
+
# 2) Llamar playlist
|
| 23 |
+
# Extraemos ID (asume /playlist/{id})
|
| 24 |
+
pid = playlist_url.rstrip('/').split('/')[-1].split('?')[0]
|
| 25 |
+
url = f"https://api.spotify.com/v1/playlists/{pid}/tracks"
|
| 26 |
+
resp = requests.get(url, headers={"Authorization":f"Bearer {token}"}, params={"limit":1})
|
| 27 |
if resp.status_code == 200:
|
| 28 |
+
total = resp.json().get("total", "¿?")
|
| 29 |
+
return f"✅ OK! La playlist tiene {total} tracks"
|
| 30 |
else:
|
| 31 |
+
return f"❌ Playlist error {resp.status_code}: {resp.json()}"
|
| 32 |
|
|
|
|
| 33 |
iface = gr.Interface(
|
| 34 |
+
fn=test_playlist,
|
| 35 |
+
inputs=gr.Textbox(label="URL de Playlist"),
|
| 36 |
outputs="text",
|
| 37 |
+
title="🔍 Test Playlist Spotify",
|
| 38 |
+
description="Pega una URL de playlist y te digo su número de tracks"
|
| 39 |
)
|
| 40 |
|
| 41 |
+
if __name__=="__main__":
|
| 42 |
iface.launch()
|