Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,7 +26,6 @@ CLIENT_ID = os.getenv("SPOTIFY_CLIENT_IDS", "").split(',')[0]
|
|
| 26 |
CLIENT_SECRET = os.getenv("SPOTIFY_CLIENT_SECRETS", "").split(',')[0]
|
| 27 |
|
| 28 |
# Funci贸n para obtener token
|
| 29 |
-
|
| 30 |
def get_token():
|
| 31 |
resp = requests.post(
|
| 32 |
"https://accounts.spotify.com/api/token",
|
|
@@ -37,7 +36,6 @@ def get_token():
|
|
| 37 |
return resp.json()["access_token"]
|
| 38 |
|
| 39 |
# Extrae el ID de playlist de la URL
|
| 40 |
-
|
| 41 |
def extract_pid(url):
|
| 42 |
return url.strip().rstrip('/').split('/')[-1].split('?')[0]
|
| 43 |
|
|
@@ -52,8 +50,7 @@ def get_artist_genres(aid, headers):
|
|
| 52 |
_artist_cache[aid] = genres
|
| 53 |
return genres
|
| 54 |
|
| 55 |
-
# Fetch y devuelve DataFrame
|
| 56 |
-
|
| 57 |
def fetch_playlist_table(url):
|
| 58 |
if not CLIENT_ID or not CLIENT_SECRET:
|
| 59 |
return pd.DataFrame([{'Error': 'Faltan credenciales'}])
|
|
@@ -95,25 +92,27 @@ def fetch_playlist_table(url):
|
|
| 95 |
df = pd.DataFrame(records)
|
| 96 |
return df
|
| 97 |
|
| 98 |
-
#
|
| 99 |
|
| 100 |
def main(url):
|
| 101 |
df = fetch_playlist_table(url)
|
| 102 |
# Guardar CSV temporalmente
|
| 103 |
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".csv")
|
| 104 |
df.to_csv(tmp.name, index=False)
|
| 105 |
-
|
|
|
|
| 106 |
|
|
|
|
| 107 |
iface = gr.Interface(
|
| 108 |
fn=main,
|
| 109 |
inputs=gr.Textbox(label="URL de playlist"),
|
| 110 |
outputs=[
|
| 111 |
gr.Dataframe(headers=['Artist','Title','ISRC','URL','URI','Genres'], label="Tabla"),
|
| 112 |
-
gr.File(label="Descargar CSV")
|
| 113 |
],
|
| 114 |
title="馃幍 Tabla de Playlist Spotify",
|
| 115 |
description="Trae Artist, Title, ISRC, URL, URI y G茅neros de cada track y permite descargar CSV"
|
| 116 |
)
|
| 117 |
|
| 118 |
if __name__ == '__main__':
|
| 119 |
-
iface.launch(server_name='0.0.0.0', server_port=7860)
|
|
|
|
| 26 |
CLIENT_SECRET = os.getenv("SPOTIFY_CLIENT_SECRETS", "").split(',')[0]
|
| 27 |
|
| 28 |
# Funci贸n para obtener token
|
|
|
|
| 29 |
def get_token():
|
| 30 |
resp = requests.post(
|
| 31 |
"https://accounts.spotify.com/api/token",
|
|
|
|
| 36 |
return resp.json()["access_token"]
|
| 37 |
|
| 38 |
# Extrae el ID de playlist de la URL
|
|
|
|
| 39 |
def extract_pid(url):
|
| 40 |
return url.strip().rstrip('/').split('/')[-1].split('?')[0]
|
| 41 |
|
|
|
|
| 50 |
_artist_cache[aid] = genres
|
| 51 |
return genres
|
| 52 |
|
| 53 |
+
# Fetch y devuelve DataFrame de toda la playlist
|
|
|
|
| 54 |
def fetch_playlist_table(url):
|
| 55 |
if not CLIENT_ID or not CLIENT_SECRET:
|
| 56 |
return pd.DataFrame([{'Error': 'Faltan credenciales'}])
|
|
|
|
| 92 |
df = pd.DataFrame(records)
|
| 93 |
return df
|
| 94 |
|
| 95 |
+
# Funci贸n principal: retorna DataFrame y habilita descarga
|
| 96 |
|
| 97 |
def main(url):
|
| 98 |
df = fetch_playlist_table(url)
|
| 99 |
# Guardar CSV temporalmente
|
| 100 |
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".csv")
|
| 101 |
df.to_csv(tmp.name, index=False)
|
| 102 |
+
# Mostrar tabla y luego revelar bot贸n de descarga
|
| 103 |
+
return df, gr.File.update(value=tmp.name, visible=True)
|
| 104 |
|
| 105 |
+
# Definici贸n de interfaz con bot贸n de descarga oculto inicialmente
|
| 106 |
iface = gr.Interface(
|
| 107 |
fn=main,
|
| 108 |
inputs=gr.Textbox(label="URL de playlist"),
|
| 109 |
outputs=[
|
| 110 |
gr.Dataframe(headers=['Artist','Title','ISRC','URL','URI','Genres'], label="Tabla"),
|
| 111 |
+
gr.File(label="Descargar CSV", visible=False)
|
| 112 |
],
|
| 113 |
title="馃幍 Tabla de Playlist Spotify",
|
| 114 |
description="Trae Artist, Title, ISRC, URL, URI y G茅neros de cada track y permite descargar CSV"
|
| 115 |
)
|
| 116 |
|
| 117 |
if __name__ == '__main__':
|
| 118 |
+
iface.launch(server_name='0.0.0.0', server_port=7860)
|