Mthrfkr commited on
Commit
447f2fc
·
verified ·
1 Parent(s): 5003f5e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -24
app.py CHANGED
@@ -2,38 +2,41 @@ import os
2
  import gradio as gr
3
  import requests
4
 
5
- # 1️⃣ LEE TUS CREDENCIALES DE ENV
6
- SPOTIFY_CLIENT_ID = os.getenv("SPOTIFY_CLIENT_IDS", "").split(',')[0]
7
- SPOTIFY_CLIENT_SECRET = os.getenv("SPOTIFY_CLIENT_SECRETS", "").split(',')[0]
8
 
9
- def test_spotify_api():
10
- """
11
- Pide un token a Spotify y regresa 'OK' o el error que truene.
12
- """
13
- if not SPOTIFY_CLIENT_ID or not SPOTIFY_CLIENT_SECRET:
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": "application/x-www-form-urlencoded"},
20
- data={"grant_type": "client_credentials"},
21
- auth=(SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET)
22
  )
 
 
 
 
 
 
 
 
23
  if resp.status_code == 200:
24
- token = resp.json().get("access_token")
25
- return f"✅ Token OK: {token[:20]}"
26
  else:
27
- return f"❌ Status {resp.status_code}: {resp.text}"
28
 
29
- # 3️⃣ Monta la interfaz Gradio
30
  iface = gr.Interface(
31
- fn=test_spotify_api,
32
- inputs=[],
33
  outputs="text",
34
- title="🛠️ Test Spotify API",
35
- description="Pulsa el botón y checa si obtenemos token de Spotify"
36
  )
37
 
38
- if __name__ == "__main__":
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()