Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import os
|
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
|
| 6 |
# βββββββββββββ
|
| 7 |
# Config Spotify (Client Credentials)
|
|
@@ -30,10 +31,7 @@ def get_artist_genres(aid, headers):
|
|
| 30 |
if aid in _artist_cache:
|
| 31 |
return _artist_cache[aid]
|
| 32 |
resp = requests.get(f"https://api.spotify.com/v1/artists/{aid}", headers=headers)
|
| 33 |
-
if resp.status_code == 200
|
| 34 |
-
genres = resp.json().get('genres', [])
|
| 35 |
-
else:
|
| 36 |
-
genres = []
|
| 37 |
_artist_cache[aid] = genres
|
| 38 |
return genres
|
| 39 |
|
|
@@ -62,7 +60,8 @@ def fetch_playlist_table(url):
|
|
| 62 |
r.raise_for_status()
|
| 63 |
for item in r.json().get('items', []):
|
| 64 |
tr = item.get('track', {})
|
| 65 |
-
if not tr:
|
|
|
|
| 66 |
aid = tr['artists'][0]['id'] if tr.get('artists') else None
|
| 67 |
genres = get_artist_genres(aid, headers) if aid else []
|
| 68 |
records.append({
|
|
@@ -79,16 +78,23 @@ def fetch_playlist_table(url):
|
|
| 79 |
return df
|
| 80 |
|
| 81 |
# Interfaz Gradio
|
|
|
|
| 82 |
def main(url):
|
| 83 |
df = fetch_playlist_table(url)
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
iface = gr.Interface(
|
| 87 |
fn=main,
|
| 88 |
inputs=gr.Textbox(label="URL de playlist"),
|
| 89 |
-
outputs=
|
|
|
|
|
|
|
|
|
|
| 90 |
title="π΅ Tabla de Playlist Spotify",
|
| 91 |
-
description="Trae Artist, Title, ISRC, URL, URI y GΓ©neros de cada track"
|
| 92 |
)
|
| 93 |
|
| 94 |
if __name__ == '__main__':
|
|
|
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
| 4 |
import gradio as gr
|
| 5 |
+
from tempfile import NamedTemporaryFile
|
| 6 |
|
| 7 |
# βββββββββββββ
|
| 8 |
# Config Spotify (Client Credentials)
|
|
|
|
| 31 |
if aid in _artist_cache:
|
| 32 |
return _artist_cache[aid]
|
| 33 |
resp = requests.get(f"https://api.spotify.com/v1/artists/{aid}", headers=headers)
|
| 34 |
+
genres = resp.json().get('genres', []) if resp.status_code == 200 else []
|
|
|
|
|
|
|
|
|
|
| 35 |
_artist_cache[aid] = genres
|
| 36 |
return genres
|
| 37 |
|
|
|
|
| 60 |
r.raise_for_status()
|
| 61 |
for item in r.json().get('items', []):
|
| 62 |
tr = item.get('track', {})
|
| 63 |
+
if not tr:
|
| 64 |
+
continue
|
| 65 |
aid = tr['artists'][0]['id'] if tr.get('artists') else None
|
| 66 |
genres = get_artist_genres(aid, headers) if aid else []
|
| 67 |
records.append({
|
|
|
|
| 78 |
return df
|
| 79 |
|
| 80 |
# Interfaz Gradio
|
| 81 |
+
|
| 82 |
def main(url):
|
| 83 |
df = fetch_playlist_table(url)
|
| 84 |
+
# Guarda CSV temporal
|
| 85 |
+
tmp = NamedTemporaryFile(delete=False, suffix='.csv')
|
| 86 |
+
df.to_csv(tmp.name, index=False)
|
| 87 |
+
return df, tmp.name
|
| 88 |
|
| 89 |
iface = gr.Interface(
|
| 90 |
fn=main,
|
| 91 |
inputs=gr.Textbox(label="URL de playlist"),
|
| 92 |
+
outputs=[
|
| 93 |
+
gr.Dataframe(headers=['Artist','Title','ISRC','URL','URI','Genres']),
|
| 94 |
+
gr.File(label="Descargar CSV")
|
| 95 |
+
],
|
| 96 |
title="π΅ Tabla de Playlist Spotify",
|
| 97 |
+
description="Trae Artist, Title, ISRC, URL, URI y GΓ©neros de cada track y permite descargar CSV"
|
| 98 |
)
|
| 99 |
|
| 100 |
if __name__ == '__main__':
|