Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,6 +26,7 @@ client_secrets = [
|
|
| 26 |
'9fdfa58ea0a94ce7a0cb34fa19fb7d74'
|
| 27 |
]
|
| 28 |
current_api_index = 0
|
|
|
|
| 29 |
|
| 30 |
def obtener_token(client_id, client_secret):
|
| 31 |
print(f"Obteniendo token de Spotify con client_id: {client_id}")
|
|
@@ -40,11 +41,33 @@ def obtener_token(client_id, client_secret):
|
|
| 40 |
print(f"Error al obtener token para client_id: {client_id}, status_code: {response.status_code}, response: {response.text}")
|
| 41 |
return None
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
def cambiar_api_key():
|
| 44 |
global current_api_index
|
| 45 |
current_api_index = (current_api_index + 1) % len(client_ids)
|
| 46 |
print(f"Cambiando a la siguiente API Key, índice actual: {current_api_index}")
|
| 47 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
def buscar_playlists_spotify(token, query, limit=50):
|
| 50 |
print("Buscando playlists en Spotify...")
|
|
@@ -55,23 +78,19 @@ def buscar_playlists_spotify(token, query, limit=50):
|
|
| 55 |
try:
|
| 56 |
if limit <= 50:
|
| 57 |
params = {'q': query, 'type': 'playlist', 'limit': limit}
|
| 58 |
-
response =
|
| 59 |
if response.status_code == 429: # Límite alcanzado
|
| 60 |
print("Límite de peticiones alcanzado, cambiando API Key...")
|
| 61 |
-
|
| 62 |
-
time.sleep(1) # Pausa de 1 segundo para evitar el límite de tasa
|
| 63 |
-
response = requests.get(url, headers={'Authorization': f'Bearer {token}'}, params=params)
|
| 64 |
playlists.extend(response.json().get('playlists', {}).get('items', []))
|
| 65 |
else:
|
| 66 |
offset = 0
|
| 67 |
while limit > 0:
|
| 68 |
params = {'q': query, 'type': 'playlist', 'limit': min(50, limit), 'offset': offset}
|
| 69 |
-
response =
|
| 70 |
if response.status_code == 429: # Límite alcanzado
|
| 71 |
print("Límite de peticiones alcanzado, cambiando API Key...")
|
| 72 |
-
|
| 73 |
-
time.sleep(1) # Pausa de 1 segundo para evitar el límite de tasa
|
| 74 |
-
response = requests.get(url, headers={'Authorization': f'Bearer {token}'}, params=params)
|
| 75 |
playlists.extend(response.json().get('playlists', {}).get('items', []))
|
| 76 |
limit -= min(50, limit)
|
| 77 |
offset += 50
|
|
@@ -88,12 +107,10 @@ def obtener_canciones_playlist_spotify(token, playlist_id, playlist_name):
|
|
| 88 |
canciones = []
|
| 89 |
|
| 90 |
try:
|
| 91 |
-
response =
|
| 92 |
if response.status_code == 429: # Límite alcanzado
|
| 93 |
print("Límite de peticiones alcanzado, cambiando API Key...")
|
| 94 |
-
|
| 95 |
-
time.sleep(1) # Pausa de 1 segundo para evitar el límite de tasa
|
| 96 |
-
response = requests.get(url, headers={'Authorization': f'Bearer {token}'})
|
| 97 |
if response.status_code == 200:
|
| 98 |
tracks = response.json().get('items')
|
| 99 |
for item in tracks:
|
|
@@ -124,23 +141,19 @@ def obtener_canciones_playlist_spotify(token, playlist_id, playlist_name):
|
|
| 124 |
def obtener_caracteristicas_audio(token, track_id):
|
| 125 |
url = f'https://api.spotify.com/v1/audio-features/{track_id}'
|
| 126 |
headers = {'Authorization': f'Bearer {token}'}
|
| 127 |
-
response =
|
| 128 |
if response.status_code == 429: # Límite alcanzado
|
| 129 |
print("Límite de peticiones alcanzado al obtener características de audio, cambiando API Key...")
|
| 130 |
-
|
| 131 |
-
time.sleep(1) # Pausa de 1 segundo para evitar el límite de tasa
|
| 132 |
-
response = requests.get(url, headers={'Authorization': f'Bearer {token}'})
|
| 133 |
return response.json() if response.status_code == 200 else {}
|
| 134 |
|
| 135 |
def obtener_record_label_spotify(album_id, token):
|
| 136 |
url = f'https://api.spotify.com/v1/albums/{album_id}'
|
| 137 |
headers = {'Authorization': f'Bearer {token}'}
|
| 138 |
-
response =
|
| 139 |
if response.status_code == 429: # Límite alcanzado
|
| 140 |
print("Límite de peticiones alcanzado al obtener la discográfica, cambiando API Key...")
|
| 141 |
-
|
| 142 |
-
time.sleep(1) # Pausa de 1 segundo para evitar el límite de tasa
|
| 143 |
-
response = requests.get(url, headers={'Authorization': f'Bearer {token}'})
|
| 144 |
album_info = response.json() if response.status_code == 200 else {}
|
| 145 |
return album_info.get('label', 'No disponible')
|
| 146 |
|
|
|
|
| 26 |
'9fdfa58ea0a94ce7a0cb34fa19fb7d74'
|
| 27 |
]
|
| 28 |
current_api_index = 0
|
| 29 |
+
session = requests.Session()
|
| 30 |
|
| 31 |
def obtener_token(client_id, client_secret):
|
| 32 |
print(f"Obteniendo token de Spotify con client_id: {client_id}")
|
|
|
|
| 41 |
print(f"Error al obtener token para client_id: {client_id}, status_code: {response.status_code}, response: {response.text}")
|
| 42 |
return None
|
| 43 |
|
| 44 |
+
def cerrar_y_reiniciar_sesion():
|
| 45 |
+
global session
|
| 46 |
+
if session:
|
| 47 |
+
session.close()
|
| 48 |
+
print("Sesión cerrada.")
|
| 49 |
+
session = requests.Session()
|
| 50 |
+
print("Nueva sesión creada.")
|
| 51 |
+
|
| 52 |
def cambiar_api_key():
|
| 53 |
global current_api_index
|
| 54 |
current_api_index = (current_api_index + 1) % len(client_ids)
|
| 55 |
print(f"Cambiando a la siguiente API Key, índice actual: {current_api_index}")
|
| 56 |
+
return client_ids[current_api_index], client_secrets[current_api_index]
|
| 57 |
+
|
| 58 |
+
def obtener_nuevo_token():
|
| 59 |
+
cerrar_y_reiniciar_sesion()
|
| 60 |
+
client_id, client_secret = cambiar_api_key()
|
| 61 |
+
token = obtener_token(client_id, client_secret)
|
| 62 |
+
print(f"Nuevo token obtenido: {token}")
|
| 63 |
+
return token
|
| 64 |
+
|
| 65 |
+
def realizar_solicitud_con_nuevo_token(url, params):
|
| 66 |
+
token = obtener_nuevo_token()
|
| 67 |
+
headers = {'Authorization': f'Bearer {token}'}
|
| 68 |
+
response = session.get(url, headers=headers, params=params)
|
| 69 |
+
print(f"Solicitud realizada con nuevo token. Código de estado: {response.status_code}")
|
| 70 |
+
return response
|
| 71 |
|
| 72 |
def buscar_playlists_spotify(token, query, limit=50):
|
| 73 |
print("Buscando playlists en Spotify...")
|
|
|
|
| 78 |
try:
|
| 79 |
if limit <= 50:
|
| 80 |
params = {'q': query, 'type': 'playlist', 'limit': limit}
|
| 81 |
+
response = session.get(url, headers=headers, params=params)
|
| 82 |
if response.status_code == 429: # Límite alcanzado
|
| 83 |
print("Límite de peticiones alcanzado, cambiando API Key...")
|
| 84 |
+
response = realizar_solicitud_con_nuevo_token(url, params)
|
|
|
|
|
|
|
| 85 |
playlists.extend(response.json().get('playlists', {}).get('items', []))
|
| 86 |
else:
|
| 87 |
offset = 0
|
| 88 |
while limit > 0:
|
| 89 |
params = {'q': query, 'type': 'playlist', 'limit': min(50, limit), 'offset': offset}
|
| 90 |
+
response = session.get(url, headers=headers, params=params)
|
| 91 |
if response.status_code == 429: # Límite alcanzado
|
| 92 |
print("Límite de peticiones alcanzado, cambiando API Key...")
|
| 93 |
+
response = realizar_solicitud_con_nuevo_token(url, params)
|
|
|
|
|
|
|
| 94 |
playlists.extend(response.json().get('playlists', {}).get('items', []))
|
| 95 |
limit -= min(50, limit)
|
| 96 |
offset += 50
|
|
|
|
| 107 |
canciones = []
|
| 108 |
|
| 109 |
try:
|
| 110 |
+
response = session.get(url, headers=headers)
|
| 111 |
if response.status_code == 429: # Límite alcanzado
|
| 112 |
print("Límite de peticiones alcanzado, cambiando API Key...")
|
| 113 |
+
response = realizar_solicitud_con_nuevo_token(url, {})
|
|
|
|
|
|
|
| 114 |
if response.status_code == 200:
|
| 115 |
tracks = response.json().get('items')
|
| 116 |
for item in tracks:
|
|
|
|
| 141 |
def obtener_caracteristicas_audio(token, track_id):
|
| 142 |
url = f'https://api.spotify.com/v1/audio-features/{track_id}'
|
| 143 |
headers = {'Authorization': f'Bearer {token}'}
|
| 144 |
+
response = session.get(url, headers=headers)
|
| 145 |
if response.status_code == 429: # Límite alcanzado
|
| 146 |
print("Límite de peticiones alcanzado al obtener características de audio, cambiando API Key...")
|
| 147 |
+
response = realizar_solicitud_con_nuevo_token(url, {})
|
|
|
|
|
|
|
| 148 |
return response.json() if response.status_code == 200 else {}
|
| 149 |
|
| 150 |
def obtener_record_label_spotify(album_id, token):
|
| 151 |
url = f'https://api.spotify.com/v1/albums/{album_id}'
|
| 152 |
headers = {'Authorization': f'Bearer {token}'}
|
| 153 |
+
response = session.get(url, headers=headers)
|
| 154 |
if response.status_code == 429: # Límite alcanzado
|
| 155 |
print("Límite de peticiones alcanzado al obtener la discográfica, cambiando API Key...")
|
| 156 |
+
response = realizar_solicitud_con_nuevo_token(url, {})
|
|
|
|
|
|
|
| 157 |
album_info = response.json() if response.status_code == 200 else {}
|
| 158 |
return album_info.get('label', 'No disponible')
|
| 159 |
|