Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,22 +6,28 @@ from openpyxl import Workbook
|
|
| 6 |
import shutil
|
| 7 |
|
| 8 |
# Lista de credenciales de API de Spotify
|
| 9 |
-
client_ids = ['b4a2add66ffb4f1198b94b087b365c65', '9df51caba5d247dc921b21de35a47c44','191227c66e0d4be692bc8ee73ea6eb3d','807008cb2ce041178c1871973fc81716']
|
| 10 |
-
client_secrets = ['8045eacf956a477299d2bc41752f1f73', '0e39502ec7e74fe99bb74245678d5f0d','2d2a895d85874c088897dd9894dc64ad','b6c3fbe2304145e4b268f05eefd6ab2a']
|
| 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'
|
| 17 |
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
|
| 18 |
payload = {'grant_type': 'client_credentials'}
|
| 19 |
response = requests.post(url, headers=headers, data=payload, auth=(client_id, client_secret))
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def cambiar_api_key():
|
| 23 |
global current_api_index
|
| 24 |
current_api_index = (current_api_index + 1) % len(client_ids)
|
|
|
|
| 25 |
return obtener_token(client_ids[current_api_index], client_secrets[current_api_index])
|
| 26 |
|
| 27 |
def buscar_playlists_spotify(token, query, limit=50):
|
|
@@ -35,6 +41,7 @@ def buscar_playlists_spotify(token, query, limit=50):
|
|
| 35 |
params = {'q': query, 'type': 'playlist', 'limit': limit}
|
| 36 |
response = requests.get(url, headers=headers, params=params)
|
| 37 |
if response.status_code == 429: # Límite alcanzado
|
|
|
|
| 38 |
token = cambiar_api_key()
|
| 39 |
response = requests.get(url, headers={'Authorization': f'Bearer {token}'}, params=params)
|
| 40 |
playlists.extend(response.json().get('playlists', {}).get('items', []))
|
|
@@ -44,6 +51,7 @@ def buscar_playlists_spotify(token, query, limit=50):
|
|
| 44 |
params = {'q': query, 'type': 'playlist', 'limit': min(50, limit), 'offset': offset}
|
| 45 |
response = requests.get(url, headers=headers, params=params)
|
| 46 |
if response.status_code == 429: # Límite alcanzado
|
|
|
|
| 47 |
token = cambiar_api_key()
|
| 48 |
response = requests.get(url, headers={'Authorization': f'Bearer {token}'}, params=params)
|
| 49 |
playlists.extend(response.json().get('playlists', {}).get('items', []))
|
|
@@ -63,6 +71,7 @@ def obtener_canciones_playlist_spotify(token, playlist_id, playlist_name):
|
|
| 63 |
try:
|
| 64 |
response = requests.get(url, headers=headers)
|
| 65 |
if response.status_code == 429: # Límite alcanzado
|
|
|
|
| 66 |
token = cambiar_api_key()
|
| 67 |
response = requests.get(url, headers={'Authorization': f'Bearer {token}'})
|
| 68 |
if response.status_code == 200:
|
|
@@ -106,6 +115,7 @@ def obtener_caracteristicas_audio(token, track_id):
|
|
| 106 |
headers = {'Authorization': f'Bearer {token}'}
|
| 107 |
response = requests.get(url, headers=headers)
|
| 108 |
if response.status_code == 429: # Límite alcanzado
|
|
|
|
| 109 |
token = cambiar_api_key()
|
| 110 |
response = requests.get(url, headers={'Authorization': f'Bearer {token}'})
|
| 111 |
return response.json() if response.status_code == 200 else {}
|
|
@@ -115,6 +125,7 @@ def obtener_analisis_audio(token, track_id):
|
|
| 115 |
headers = {'Authorization': f'Bearer {token}'}
|
| 116 |
response = requests.get(url, headers=headers)
|
| 117 |
if response.status_code == 429: # Límite alcanzado
|
|
|
|
| 118 |
token = cambiar_api_key()
|
| 119 |
response = requests.get(url, headers={'Authorization': f'Bearer {token}'})
|
| 120 |
return response.json() if response.status_code == 200 else {}
|
|
@@ -143,6 +154,7 @@ def obtener_record_label_spotify(album_id, token):
|
|
| 143 |
headers = {'Authorization': f'Bearer {token}'}
|
| 144 |
response = requests.get(url, headers=headers)
|
| 145 |
if response.status_code == 429: # Límite alcanzado
|
|
|
|
| 146 |
token = cambiar_api_key()
|
| 147 |
response = requests.get(url, headers={'Authorization': f'Bearer {token}'})
|
| 148 |
album_info = response.json() if response.status_code == 200 else {}
|
|
@@ -187,4 +199,4 @@ iface = gr.Interface(
|
|
| 187 |
title="Spotify Playlist Fetcher",
|
| 188 |
description="Enter a search query to fetch playlists and their songs from Spotify. Client credentials are pre-configured."
|
| 189 |
)
|
| 190 |
-
iface.launch()
|
|
|
|
| 6 |
import shutil
|
| 7 |
|
| 8 |
# Lista de credenciales de API de Spotify
|
| 9 |
+
client_ids = ['b4a2add66ffb4f1198b94b087b365c65', '9df51caba5d247dc921b21de35a47c44', '191227c66e0d4be692bc8ee73ea6eb3d', '807008cb2ce041178c1871973fc81716']
|
| 10 |
+
client_secrets = ['8045eacf956a477299d2bc41752f1f73', '0e39502ec7e74fe99bb74245678d5f0d', '2d2a895d85874c088897dd9894dc64ad', 'b6c3fbe2304145e4b268f05eefd6ab2a']
|
| 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'
|
| 17 |
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
|
| 18 |
payload = {'grant_type': 'client_credentials'}
|
| 19 |
response = requests.post(url, headers=headers, data=payload, auth=(client_id, client_secret))
|
| 20 |
+
if response.status_code == 200:
|
| 21 |
+
print(f"Token obtenido exitosamente para client_id: {client_id}")
|
| 22 |
+
return response.json().get('access_token')
|
| 23 |
+
else:
|
| 24 |
+
print(f"Error al obtener token para client_id: {client_id}, status_code: {response.status_code}, response: {response.text}")
|
| 25 |
+
return None
|
| 26 |
|
| 27 |
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):
|
|
|
|
| 41 |
params = {'q': query, 'type': 'playlist', 'limit': limit}
|
| 42 |
response = requests.get(url, headers=headers, params=params)
|
| 43 |
if response.status_code == 429: # Límite alcanzado
|
| 44 |
+
print("Límite de peticiones alcanzado, cambiando API Key...")
|
| 45 |
token = cambiar_api_key()
|
| 46 |
response = requests.get(url, headers={'Authorization': f'Bearer {token}'}, params=params)
|
| 47 |
playlists.extend(response.json().get('playlists', {}).get('items', []))
|
|
|
|
| 51 |
params = {'q': query, 'type': 'playlist', 'limit': min(50, limit), 'offset': offset}
|
| 52 |
response = requests.get(url, headers=headers, params=params)
|
| 53 |
if response.status_code == 429: # Límite alcanzado
|
| 54 |
+
print("Límite de peticiones alcanzado, cambiando API Key...")
|
| 55 |
token = cambiar_api_key()
|
| 56 |
response = requests.get(url, headers={'Authorization': f'Bearer {token}'}, params=params)
|
| 57 |
playlists.extend(response.json().get('playlists', {}).get('items', []))
|
|
|
|
| 71 |
try:
|
| 72 |
response = requests.get(url, headers=headers)
|
| 73 |
if response.status_code == 429: # Límite alcanzado
|
| 74 |
+
print("Límite de peticiones alcanzado, cambiando API Key...")
|
| 75 |
token = cambiar_api_key()
|
| 76 |
response = requests.get(url, headers={'Authorization': f'Bearer {token}'})
|
| 77 |
if response.status_code == 200:
|
|
|
|
| 115 |
headers = {'Authorization': f'Bearer {token}'}
|
| 116 |
response = requests.get(url, headers=headers)
|
| 117 |
if response.status_code == 429: # Límite alcanzado
|
| 118 |
+
print("Límite de peticiones alcanzado al obtener características de audio, cambiando API Key...")
|
| 119 |
token = cambiar_api_key()
|
| 120 |
response = requests.get(url, headers={'Authorization': f'Bearer {token}'})
|
| 121 |
return response.json() if response.status_code == 200 else {}
|
|
|
|
| 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 {}
|
|
|
|
| 154 |
headers = {'Authorization': f'Bearer {token}'}
|
| 155 |
response = requests.get(url, headers=headers)
|
| 156 |
if response.status_code == 429: # Límite alcanzado
|
| 157 |
+
print("Límite de peticiones alcanzado al obtener la discográfica, cambiando API Key...")
|
| 158 |
token = cambiar_api_key()
|
| 159 |
response = requests.get(url, headers={'Authorization': f'Bearer {token}'})
|
| 160 |
album_info = response.json() if response.status_code == 200 else {}
|
|
|
|
| 199 |
title="Spotify Playlist Fetcher",
|
| 200 |
description="Enter a search query to fetch playlists and their songs from Spotify. Client credentials are pre-configured."
|
| 201 |
)
|
| 202 |
+
iface.launch()
|