Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,26 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
|
|
|
| 4 |
from tempfile import NamedTemporaryFile
|
| 5 |
from openpyxl import Workbook
|
| 6 |
import shutil
|
| 7 |
|
| 8 |
# Lista de credenciales de API de Spotify
|
| 9 |
-
client_ids = [
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
current_api_index = 0
|
| 12 |
|
| 13 |
-
# Funciones para Spotify
|
| 14 |
def obtener_token(client_id, client_secret):
|
| 15 |
print(f"Obteniendo token de Spotify con client_id: {client_id}")
|
| 16 |
url = 'https://accounts.spotify.com/api/token'
|
|
@@ -28,6 +38,7 @@ def cambiar_api_key():
|
|
| 28 |
global current_api_index
|
| 29 |
current_api_index = (current_api_index + 1) % len(client_ids)
|
| 30 |
print(f"Cambiando a la siguiente API Key, índice actual: {current_api_index}")
|
|
|
|
| 31 |
return obtener_token(client_ids[current_api_index], client_secrets[current_api_index])
|
| 32 |
|
| 33 |
def buscar_playlists_spotify(token, query, limit=50):
|
|
@@ -57,6 +68,7 @@ def buscar_playlists_spotify(token, query, limit=50):
|
|
| 57 |
playlists.extend(response.json().get('playlists', {}).get('items', []))
|
| 58 |
limit -= min(50, limit)
|
| 59 |
offset += 50
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
print(f"Error al buscar playlists: {e}")
|
| 62 |
|
|
@@ -80,8 +92,6 @@ def obtener_canciones_playlist_spotify(token, playlist_id, playlist_name):
|
|
| 80 |
track = item.get('track')
|
| 81 |
if track:
|
| 82 |
audio_features = obtener_caracteristicas_audio(token, track['id'])
|
| 83 |
-
audio_analysis = obtener_analisis_audio(token, track['id'])
|
| 84 |
-
key = obtener_clave(audio_analysis)
|
| 85 |
canciones.append({
|
| 86 |
'playlist_name': playlist_name,
|
| 87 |
'artista': track['artists'][0]['name'] if track['artists'] else 'Desconocido',
|
|
@@ -96,14 +106,7 @@ def obtener_canciones_playlist_spotify(token, playlist_id, playlist_name):
|
|
| 96 |
'instrumentalness': audio_features.get('instrumentalness', 'No disponible'),
|
| 97 |
'duration': track.get('duration_ms', 'No disponible'),
|
| 98 |
'release_year': track.get('album', {}).get('release_date', 'No disponible').split('-')[0] if track.get('album', {}).get('release_date') else 'No disponible',
|
| 99 |
-
'
|
| 100 |
-
'timbre': audio_analysis.get('segments', [{}])[0].get('timbre', 'No disponible'),
|
| 101 |
-
'acousticness': audio_features.get('acousticness', 'No disponible'),
|
| 102 |
-
'liveness': audio_features.get('liveness', 'No disponible'),
|
| 103 |
-
'key': key,
|
| 104 |
-
'link': track['external_urls']['spotify'],
|
| 105 |
-
'record_label': obtener_record_label_spotify(track['album']['id'], token),
|
| 106 |
-
'source': 'Spotify'
|
| 107 |
})
|
| 108 |
except Exception as e:
|
| 109 |
print(f"Error al obtener canciones de la playlist: {e}")
|
|
@@ -120,35 +123,6 @@ def obtener_caracteristicas_audio(token, track_id):
|
|
| 120 |
response = requests.get(url, headers={'Authorization': f'Bearer {token}'})
|
| 121 |
return response.json() if response.status_code == 200 else {}
|
| 122 |
|
| 123 |
-
def obtener_analisis_audio(token, track_id):
|
| 124 |
-
url = f'https://api.spotify.com/v1/audio-analysis/{track_id}'
|
| 125 |
-
headers = {'Authorization': f'Bearer {token}'}
|
| 126 |
-
response = requests.get(url, headers=headers)
|
| 127 |
-
if response.status_code == 429: # Límite alcanzado
|
| 128 |
-
print("Límite de peticiones alcanzado al obtener análisis de audio, cambiando API Key...")
|
| 129 |
-
token = cambiar_api_key()
|
| 130 |
-
response = requests.get(url, headers={'Authorization': f'Bearer {token}'})
|
| 131 |
-
return response.json() if response.status_code == 200 else {}
|
| 132 |
-
|
| 133 |
-
def obtener_clave(audio_analysis):
|
| 134 |
-
key_map = {
|
| 135 |
-
-1: 'No Key',
|
| 136 |
-
0: 'C',
|
| 137 |
-
1: 'C#/Db',
|
| 138 |
-
2: 'D',
|
| 139 |
-
3: 'D#/Eb',
|
| 140 |
-
4: 'E',
|
| 141 |
-
5: 'F',
|
| 142 |
-
6: 'F#/Gb',
|
| 143 |
-
7: 'G',
|
| 144 |
-
8: 'G#/Ab',
|
| 145 |
-
9: 'A',
|
| 146 |
-
10: 'A#/Bb',
|
| 147 |
-
11: 'B'
|
| 148 |
-
}
|
| 149 |
-
key = audio_analysis.get('track', {}).get('key', -1)
|
| 150 |
-
return key_map.get(key, 'Unknown')
|
| 151 |
-
|
| 152 |
def obtener_record_label_spotify(album_id, token):
|
| 153 |
url = f'https://api.spotify.com/v1/albums/{album_id}'
|
| 154 |
headers = {'Authorization': f'Bearer {token}'}
|
|
@@ -169,6 +143,7 @@ def interface(project_name, query, num_spotify_playlists=50):
|
|
| 169 |
for playlist in playlists_spotify:
|
| 170 |
songs = obtener_canciones_playlist_spotify(token_spotify, playlist['playlist_id'], playlist['playlist_name'])
|
| 171 |
canciones_spotify.extend(songs)
|
|
|
|
| 172 |
|
| 173 |
# Crear DataFrame
|
| 174 |
df = pd.DataFrame(canciones_spotify)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
import pandas as pd
|
| 4 |
+
import time
|
| 5 |
from tempfile import NamedTemporaryFile
|
| 6 |
from openpyxl import Workbook
|
| 7 |
import shutil
|
| 8 |
|
| 9 |
# Lista de credenciales de API de Spotify
|
| 10 |
+
client_ids = [
|
| 11 |
+
'807008cb2ce041178c1871973fc81716', '107286416613436ab5c63b56d3564f9e',
|
| 12 |
+
'528d1d6733ba46eb8e9532e56ced61eb', 'aed6435c51484dc18b8c18aa74dd51f8',
|
| 13 |
+
'9df51caba5d247dc921b21de35a47c44', '191227c66e0d4be692bc8ee73ea6eb3d',
|
| 14 |
+
'e272c0705c7c4fd68937c58adaa446ed'
|
| 15 |
+
]
|
| 16 |
+
client_secrets = [
|
| 17 |
+
'b6c3fbe2304145e4b268f05eefd6ab2a', '4de4c14ed109420c9517c083b8018f8c',
|
| 18 |
+
'3dbdcf9a12634cfca9e6aca8d3093020', '8662b33d594f4d198ea025d4aa9f0b98',
|
| 19 |
+
'0e39502ec7e74fe99bb74245678d5f0d', '2d2a895d85874c088897dd9894dc64ad',
|
| 20 |
+
'9fdfa58ea0a94ce7a0cb34fa19fb7d74'
|
| 21 |
+
]
|
| 22 |
current_api_index = 0
|
| 23 |
|
|
|
|
| 24 |
def obtener_token(client_id, client_secret):
|
| 25 |
print(f"Obteniendo token de Spotify con client_id: {client_id}")
|
| 26 |
url = 'https://accounts.spotify.com/api/token'
|
|
|
|
| 38 |
global current_api_index
|
| 39 |
current_api_index = (current_api_index + 1) % len(client_ids)
|
| 40 |
print(f"Cambiando a la siguiente API Key, índice actual: {current_api_index}")
|
| 41 |
+
time.sleep(60) # Pausa de 1 minuto para evitar el límite de tasa
|
| 42 |
return obtener_token(client_ids[current_api_index], client_secrets[current_api_index])
|
| 43 |
|
| 44 |
def buscar_playlists_spotify(token, query, limit=50):
|
|
|
|
| 68 |
playlists.extend(response.json().get('playlists', {}).get('items', []))
|
| 69 |
limit -= min(50, limit)
|
| 70 |
offset += 50
|
| 71 |
+
time.sleep(5) # Pausa de 5 segundos entre las solicitudes para evitar el límite de tasa
|
| 72 |
except Exception as e:
|
| 73 |
print(f"Error al buscar playlists: {e}")
|
| 74 |
|
|
|
|
| 92 |
track = item.get('track')
|
| 93 |
if track:
|
| 94 |
audio_features = obtener_caracteristicas_audio(token, track['id'])
|
|
|
|
|
|
|
| 95 |
canciones.append({
|
| 96 |
'playlist_name': playlist_name,
|
| 97 |
'artista': track['artists'][0]['name'] if track['artists'] else 'Desconocido',
|
|
|
|
| 106 |
'instrumentalness': audio_features.get('instrumentalness', 'No disponible'),
|
| 107 |
'duration': track.get('duration_ms', 'No disponible'),
|
| 108 |
'release_year': track.get('album', {}).get('release_date', 'No disponible').split('-')[0] if track.get('album', {}).get('release_date') else 'No disponible',
|
| 109 |
+
'record_label': obtener_record_label_spotify(track['album']['id'], token)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
})
|
| 111 |
except Exception as e:
|
| 112 |
print(f"Error al obtener canciones de la playlist: {e}")
|
|
|
|
| 123 |
response = requests.get(url, headers={'Authorization': f'Bearer {token}'})
|
| 124 |
return response.json() if response.status_code == 200 else {}
|
| 125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
def obtener_record_label_spotify(album_id, token):
|
| 127 |
url = f'https://api.spotify.com/v1/albums/{album_id}'
|
| 128 |
headers = {'Authorization': f'Bearer {token}'}
|
|
|
|
| 143 |
for playlist in playlists_spotify:
|
| 144 |
songs = obtener_canciones_playlist_spotify(token_spotify, playlist['playlist_id'], playlist['playlist_name'])
|
| 145 |
canciones_spotify.extend(songs)
|
| 146 |
+
time.sleep(1) # Pausa de 1 segundo entre la obtención de canciones para evitar el límite de tasa
|
| 147 |
|
| 148 |
# Crear DataFrame
|
| 149 |
df = pd.DataFrame(canciones_spotify)
|