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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -21
app.py CHANGED
@@ -1,41 +1,45 @@
1
- import os
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__":
 
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 test_playlist_info(url_playlist):
8
+ # 1) Auth
9
  if not (CID and CSEC):
10
  return "❌ Faltan credenciales"
11
+ r = requests.post(
 
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 r.status_code != 200:
18
+ return f"❌ Error token {r.status_code}"
19
+ token = r.json().get("access_token")
20
+
21
+ # 2) ID limpio
22
+ pid = url_playlist.rstrip('/').split('/')[-1].split('?')[0]
23
+
24
+ # 3) Metadata endpoint
25
+ resp = requests.get(
26
+ f"https://api.spotify.com/v1/playlists/{pid}",
27
+ headers={"Authorization":f"Bearer {token}"}
28
+ )
29
  if resp.status_code == 200:
30
+ d = resp.json()
31
+ name = d.get("name","?")
32
+ total = d.get("tracks",{}).get("total","?")
33
+ return f"✅ Playlist “{name}”: {total} tracks"
34
  else:
35
+ return f"❌ Error {resp.status_code}: {resp.json()}"
36
 
37
  iface = gr.Interface(
38
+ fn=test_playlist_info,
39
  inputs=gr.Textbox(label="URL de Playlist"),
40
  outputs="text",
41
+ title="🔍 Test Info Playlist",
42
+ description="Chequea nombre y total de tracks de una playlist pública"
43
  )
44
 
45
  if __name__=="__main__":