Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,53 @@
|
|
| 1 |
import os, gradio as gr, requests
|
| 2 |
|
| 3 |
-
# Toma las creds
|
| 4 |
CID = os.getenv("SPOTIFY_CLIENT_IDS", "").split(',')[0]
|
| 5 |
CSEC = os.getenv("SPOTIFY_CLIENT_SECRETS", "").split(',')[0]
|
| 6 |
|
| 7 |
-
def
|
| 8 |
# 1) Auth
|
| 9 |
if not (CID and CSEC):
|
| 10 |
-
return "❌ Faltan
|
| 11 |
-
|
| 12 |
"https://accounts.spotify.com/api/token",
|
| 13 |
-
headers={"Content-Type":"application/x-www-form-urlencoded"},
|
| 14 |
data={"grant_type":"client_credentials"},
|
| 15 |
auth=(CID, CSEC)
|
| 16 |
)
|
| 17 |
-
if
|
| 18 |
-
return f"❌
|
| 19 |
-
token =
|
| 20 |
|
| 21 |
-
# 2)
|
| 22 |
-
pid =
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
# 3) Metadata
|
| 25 |
-
|
| 26 |
f"https://api.spotify.com/v1/playlists/{pid}",
|
| 27 |
headers={"Authorization":f"Bearer {token}"}
|
| 28 |
)
|
| 29 |
-
if
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
iface = gr.Interface(
|
| 38 |
-
fn=
|
| 39 |
-
inputs=gr.Textbox(label="URL de
|
| 40 |
outputs="text",
|
| 41 |
-
title="
|
| 42 |
-
description="
|
| 43 |
)
|
| 44 |
|
| 45 |
if __name__=="__main__":
|
|
|
|
| 1 |
import os, gradio as gr, requests
|
| 2 |
|
|
|
|
| 3 |
CID = os.getenv("SPOTIFY_CLIENT_IDS", "").split(',')[0]
|
| 4 |
CSEC = os.getenv("SPOTIFY_CLIENT_SECRETS", "").split(',')[0]
|
| 5 |
|
| 6 |
+
def fetch_public_playlist(url):
|
| 7 |
# 1) Auth
|
| 8 |
if not (CID and CSEC):
|
| 9 |
+
return "❌ Faltan creds SPOTIFY_CLIENT_IDS o SPOTIFY_CLIENT_SECRETS"
|
| 10 |
+
tr = requests.post(
|
| 11 |
"https://accounts.spotify.com/api/token",
|
|
|
|
| 12 |
data={"grant_type":"client_credentials"},
|
| 13 |
auth=(CID, CSEC)
|
| 14 |
)
|
| 15 |
+
if tr.status_code != 200:
|
| 16 |
+
return f"❌ Token error {tr.status_code}"
|
| 17 |
+
token = tr.json().get("access_token")
|
| 18 |
|
| 19 |
+
# 2) Extraemos ID
|
| 20 |
+
pid = url.strip().rstrip('/').split('/')[-1].split('?')[0]
|
| 21 |
+
if len(pid) != 22:
|
| 22 |
+
return f"❌ ID inválido: '{pid}' (len={len(pid)})"
|
| 23 |
|
| 24 |
+
# 3) Metadata
|
| 25 |
+
meta = requests.get(
|
| 26 |
f"https://api.spotify.com/v1/playlists/{pid}",
|
| 27 |
headers={"Authorization":f"Bearer {token}"}
|
| 28 |
)
|
| 29 |
+
if meta.status_code != 200:
|
| 30 |
+
return f"❌ No pública o no existe: status {meta.status_code}"
|
| 31 |
+
data = meta.json()
|
| 32 |
+
name = data.get("name","?")
|
| 33 |
+
total = data.get("tracks",{}).get("total","?")
|
| 34 |
+
|
| 35 |
+
# 4) Tracks (ejemplo, límite 5 para demo)
|
| 36 |
+
tracks = requests.get(
|
| 37 |
+
f"https://api.spotify.com/v1/playlists/{pid}/tracks",
|
| 38 |
+
headers={"Authorization":f"Bearer {token}"},
|
| 39 |
+
params={"limit":5}
|
| 40 |
+
).json().get("items",[])
|
| 41 |
+
titles = [t["track"]["name"] for t in tracks if t.get("track")]
|
| 42 |
+
|
| 43 |
+
return f"✅ “{name}”: {total} tracks\n🎵 Muestra: {titles}"
|
| 44 |
|
| 45 |
iface = gr.Interface(
|
| 46 |
+
fn=fetch_public_playlist,
|
| 47 |
+
inputs=gr.Textbox(label="URL de playlist"),
|
| 48 |
outputs="text",
|
| 49 |
+
title="🔎 Checa playlist pública",
|
| 50 |
+
description="Solo funciona con playlists públicas"
|
| 51 |
)
|
| 52 |
|
| 53 |
if __name__=="__main__":
|