Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,104 +1,23 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import requests
|
| 3 |
-
import pandas as pd
|
| 4 |
from io import BytesIO
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
#
|
| 10 |
-
client_ids = os.getenv("SPOTIFY_CLIENT_IDS", "").split(',')
|
| 11 |
-
client_secrets = os.getenv("SPOTIFY_CLIENT_SECRETS", "").split(',')
|
| 12 |
-
if not client_ids or not client_secrets:
|
| 13 |
-
raise ValueError("Faltan SPOTIFY_CLIENT_IDS o SPOTIFY_CLIENT_SECRETS en entorno")
|
| 14 |
-
|
| 15 |
-
current_idx = 0
|
| 16 |
-
req_count = 0
|
| 17 |
-
MAX_REQ = 95
|
| 18 |
-
|
| 19 |
-
def rotate_client():
|
| 20 |
-
global current_idx, req_count
|
| 21 |
-
current_idx = (current_idx + 1) % len(client_ids)
|
| 22 |
-
req_count = 0
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
def get_token():
|
| 26 |
-
global current_idx
|
| 27 |
-
for _ in client_ids:
|
| 28 |
-
cid = client_ids[current_idx]
|
| 29 |
-
csec = client_secrets[current_idx]
|
| 30 |
-
r = requests.post(
|
| 31 |
-
"https://accounts.spotify.com/api/token",
|
| 32 |
-
data={"grant_type": "client_credentials"},
|
| 33 |
-
auth=(cid, csec)
|
| 34 |
-
)
|
| 35 |
-
if r.status_code == 200:
|
| 36 |
-
return r.json()["access_token"]
|
| 37 |
-
rotate_client()
|
| 38 |
-
raise RuntimeError("No se pudo obtener token")
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
def make_request(url, params=None):
|
| 42 |
-
global req_count
|
| 43 |
-
token = get_token()
|
| 44 |
-
headers = {"Authorization": f"Bearer {token}"}
|
| 45 |
-
for _ in range(3):
|
| 46 |
-
r = requests.get(url, headers=headers, params=params)
|
| 47 |
-
req_count += 1
|
| 48 |
-
if r.status_code == 429:
|
| 49 |
-
rotate_client()
|
| 50 |
-
continue
|
| 51 |
-
if r.status_code == 200:
|
| 52 |
-
if req_count >= MAX_REQ:
|
| 53 |
-
rotate_client()
|
| 54 |
-
return r.json()
|
| 55 |
-
return None
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
def extract_id(u, kind):
|
| 59 |
-
u = u.split('?')[0].rstrip('/')
|
| 60 |
-
parts = u.split('/')
|
| 61 |
-
if kind in parts:
|
| 62 |
-
return parts[parts.index(kind) + 1]
|
| 63 |
-
return None
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
def fetch_playlist(playlist_url):
|
| 67 |
-
pl_id = extract_id(playlist_url, 'playlist')
|
| 68 |
-
url = f'https://api.spotify.com/v1/playlists/{pl_id}/tracks'
|
| 69 |
-
params = {'limit':100}
|
| 70 |
-
tracks = []
|
| 71 |
-
while url:
|
| 72 |
-
data = make_request(url, params)
|
| 73 |
-
if not data: break
|
| 74 |
-
for item in data.get('items', []):
|
| 75 |
-
t = item.get('track')
|
| 76 |
-
if t:
|
| 77 |
-
tracks.append({
|
| 78 |
-
'Artist': t['artists'][0]['name'],
|
| 79 |
-
'Title': t['name'],
|
| 80 |
-
'ISRC': t.get('external_ids', {}).get('isrc',''),
|
| 81 |
-
'URL': t.get('external_urls', {}).get('spotify',''),
|
| 82 |
-
'URI': t.get('uri'),
|
| 83 |
-
'Genre': ", ".join(make_request(f"https://api.spotify.com/v1/artists/{t['artists'][0]['id']}").get('genres',[]))
|
| 84 |
-
})
|
| 85 |
-
url = data.get('next')
|
| 86 |
-
return pd.DataFrame(tracks)
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
def export_csv(playlist_url):
|
| 90 |
-
df = fetch_playlist(playlist_url)
|
| 91 |
buffer = BytesIO()
|
| 92 |
-
|
|
|
|
| 93 |
buffer.seek(0)
|
| 94 |
-
|
|
|
|
| 95 |
|
|
|
|
| 96 |
iface = gr.Interface(
|
| 97 |
-
fn=
|
| 98 |
-
inputs=gr.Textbox(label="
|
| 99 |
-
outputs=gr.File(label="Descargar
|
| 100 |
-
title="
|
| 101 |
-
description="
|
| 102 |
)
|
| 103 |
|
| 104 |
if __name__ == '__main__':
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
from io import BytesIO
|
|
|
|
| 3 |
|
| 4 |
+
# Función para generar un archivo de texto en memoria con la palabra ingresada
|
| 5 |
+
def crear_archivo(palabra):
|
| 6 |
+
# Crear buffer en memoria
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
buffer = BytesIO()
|
| 8 |
+
# Escribir la palabra en el buffer
|
| 9 |
+
buffer.write(palabra.encode('utf-8'))
|
| 10 |
buffer.seek(0)
|
| 11 |
+
# Devolver el buffer y el nombre de archivo para descargar
|
| 12 |
+
return buffer, "palabra.txt"
|
| 13 |
|
| 14 |
+
# Definir la interfaz de Gradio
|
| 15 |
iface = gr.Interface(
|
| 16 |
+
fn=crear_archivo,
|
| 17 |
+
inputs=gr.Textbox(label="Escribe una palabra", placeholder="Tu palabra aquí..."),
|
| 18 |
+
outputs=gr.File(label="Descargar archivo"),
|
| 19 |
+
title="📥 Generador de archivo de texto",
|
| 20 |
+
description="Escribe una palabra y descarga un archivo de texto con ella."
|
| 21 |
)
|
| 22 |
|
| 23 |
if __name__ == '__main__':
|