Mthrfkr commited on
Commit
74efcb1
·
verified ·
1 Parent(s): ab207b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -23
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 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__":
 
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__":