Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,7 @@ import shutil
|
|
| 9 |
|
| 10 |
# Lista de credenciales de API de Spotify
|
| 11 |
client_ids =[
|
|
|
|
| 12 |
'cfbe3754c86048d1a82542a5ab432b9a',
|
| 13 |
'37492f29c7fe478391e8fd8fe66f3f1b',
|
| 14 |
'ac3c1dd6252740e68aeb0d7dd4e6e37d',
|
|
@@ -21,6 +22,7 @@ client_ids =[
|
|
| 21 |
'e272c0705c7c4fd68937c58adaa446ed'
|
| 22 |
]
|
| 23 |
client_secrets = [
|
|
|
|
| 24 |
'c55801afd5c24df3b8673cc07468c7b6',
|
| 25 |
'b61fa17afff64c8693ea5147b79562be',
|
| 26 |
'ff49073ebd324e8cac2d997a8e34644c',
|
|
@@ -54,42 +56,51 @@ def cambiar_api_key():
|
|
| 54 |
print(f"Cambiando a la siguiente API Key, 铆ndice actual: {current_api_index}")
|
| 55 |
return obtener_token(client_ids[current_api_index], client_secrets[current_api_index])
|
| 56 |
|
| 57 |
-
def
|
| 58 |
if response.status_code == 429:
|
| 59 |
retry_after = int(response.headers.get('Retry-After', 1))
|
| 60 |
-
|
| 61 |
-
|
|
|
|
|
|
|
| 62 |
return True
|
| 63 |
return False
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
def buscar_playlists_spotify(token, query, limit=50):
|
| 66 |
print("Buscando playlists en Spotify...")
|
| 67 |
url = 'https://api.spotify.com/v1/search'
|
| 68 |
headers = {'Authorization': f'Bearer {token}'}
|
| 69 |
playlists = []
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
if manejar_errores(response):
|
| 76 |
-
token = cambiar_api_key()
|
| 77 |
-
response = requests.get(url, headers={'Authorization': f'Bearer {token}'}, params=params)
|
| 78 |
playlists.extend(response.json().get('playlists', {}).get('items', []))
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
token = cambiar_api_key()
|
| 86 |
-
response = requests.get(url, headers={'Authorization': f'Bearer {token}'}, params=params)
|
| 87 |
playlists.extend(response.json().get('playlists', {}).get('items', []))
|
| 88 |
limit -= min(50, limit)
|
| 89 |
offset += 50
|
| 90 |
-
time.sleep(
|
| 91 |
-
|
| 92 |
-
|
| 93 |
|
| 94 |
return [{'playlist_id': playlist['id'], 'playlist_name': playlist['name']} for playlist in playlists]
|
| 95 |
|
|
@@ -99,56 +110,42 @@ def obtener_canciones_playlist_spotify(token, playlist_id, playlist_name):
|
|
| 99 |
headers = {'Authorization': f'Bearer {token}'}
|
| 100 |
canciones = []
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
'duration': track.get('duration_ms', 'No disponible'),
|
| 126 |
-
'release_year': track.get('album', {}).get('release_date', 'No disponible').split('-')[0] if track.get('album', {}).get('release_date') else 'No disponible',
|
| 127 |
-
'record_label': obtener_record_label_spotify(track['album']['id'], token)
|
| 128 |
-
})
|
| 129 |
-
except Exception as e:
|
| 130 |
-
print(f"Error al obtener canciones de la playlist: {e}")
|
| 131 |
-
|
| 132 |
return canciones
|
| 133 |
|
| 134 |
def obtener_caracteristicas_audio(token, track_id):
|
| 135 |
url = f'https://api.spotify.com/v1/audio-features/{track_id}'
|
| 136 |
headers = {'Authorization': f'Bearer {token}'}
|
| 137 |
-
response =
|
| 138 |
-
|
| 139 |
-
token = cambiar_api_key()
|
| 140 |
-
response = requests.get(url, headers={'Authorization': f'Bearer {token}'})
|
| 141 |
-
return response.json() if response.status_code == 200 else {}
|
| 142 |
|
| 143 |
def obtener_record_label_spotify(album_id, token):
|
| 144 |
url = f'https://api.spotify.com/v1/albums/{album_id}'
|
| 145 |
headers = {'Authorization': f'Bearer {token}'}
|
| 146 |
-
response =
|
| 147 |
-
|
| 148 |
-
token = cambiar_api_key()
|
| 149 |
-
response = requests.get(url, headers={'Authorization': f'Bearer {token}'})
|
| 150 |
-
album_info = response.json() if response.status_code == 200 else {}
|
| 151 |
-
return album_info.get('label', 'No disponible')
|
| 152 |
|
| 153 |
# Funci贸n principal de la interfaz
|
| 154 |
def interface(project_name, query, num_spotify_playlists=50):
|
|
|
|
| 9 |
|
| 10 |
# Lista de credenciales de API de Spotify
|
| 11 |
client_ids =[
|
| 12 |
+
'066bb9a3e0ac40aba89732f9a97249bf',
|
| 13 |
'cfbe3754c86048d1a82542a5ab432b9a',
|
| 14 |
'37492f29c7fe478391e8fd8fe66f3f1b',
|
| 15 |
'ac3c1dd6252740e68aeb0d7dd4e6e37d',
|
|
|
|
| 22 |
'e272c0705c7c4fd68937c58adaa446ed'
|
| 23 |
]
|
| 24 |
client_secrets = [
|
| 25 |
+
'9cdae5b56ec24aed91bb3958823ff39e',
|
| 26 |
'c55801afd5c24df3b8673cc07468c7b6',
|
| 27 |
'b61fa17afff64c8693ea5147b79562be',
|
| 28 |
'ff49073ebd324e8cac2d997a8e34644c',
|
|
|
|
| 56 |
print(f"Cambiando a la siguiente API Key, 铆ndice actual: {current_api_index}")
|
| 57 |
return obtener_token(client_ids[current_api_index], client_secrets[current_api_index])
|
| 58 |
|
| 59 |
+
def manejar_rate_limit(response):
|
| 60 |
if response.status_code == 429:
|
| 61 |
retry_after = int(response.headers.get('Retry-After', 1))
|
| 62 |
+
# Sumar 10 segundos al tiempo de espera
|
| 63 |
+
wait_time = retry_after + 10
|
| 64 |
+
print(f"L铆mite de peticiones alcanzado, Retry-After: {retry_after} segundos. Esperando {wait_time} segundos.")
|
| 65 |
+
time.sleep(wait_time)
|
| 66 |
return True
|
| 67 |
return False
|
| 68 |
|
| 69 |
+
def hacer_request_con_reintento(url, headers, params=None, max_retries=5):
|
| 70 |
+
for i in range(max_retries):
|
| 71 |
+
response = requests.get(url, headers=headers, params=params)
|
| 72 |
+
if manejar_rate_limit(response):
|
| 73 |
+
continue
|
| 74 |
+
if response.status_code == 200:
|
| 75 |
+
return response
|
| 76 |
+
else:
|
| 77 |
+
print(f"Error en la solicitud: {response.status_code}, response: {response.text}")
|
| 78 |
+
break
|
| 79 |
+
return None
|
| 80 |
+
|
| 81 |
def buscar_playlists_spotify(token, query, limit=50):
|
| 82 |
print("Buscando playlists en Spotify...")
|
| 83 |
url = 'https://api.spotify.com/v1/search'
|
| 84 |
headers = {'Authorization': f'Bearer {token}'}
|
| 85 |
playlists = []
|
| 86 |
|
| 87 |
+
if limit <= 50:
|
| 88 |
+
params = {'q': query, 'type': 'playlist', 'limit': limit}
|
| 89 |
+
response = hacer_request_con_reintento(url, headers, params)
|
| 90 |
+
if response:
|
|
|
|
|
|
|
|
|
|
| 91 |
playlists.extend(response.json().get('playlists', {}).get('items', []))
|
| 92 |
+
else:
|
| 93 |
+
offset = 0
|
| 94 |
+
while limit > 0:
|
| 95 |
+
params = {'q': query, 'type': 'playlist', 'limit': min(50, limit), 'offset': offset}
|
| 96 |
+
response = hacer_request_con_reintento(url, headers, params)
|
| 97 |
+
if response:
|
|
|
|
|
|
|
| 98 |
playlists.extend(response.json().get('playlists', {}).get('items', []))
|
| 99 |
limit -= min(50, limit)
|
| 100 |
offset += 50
|
| 101 |
+
time.sleep(1) # Pausa de 1 segundo entre las solicitudes para evitar el l铆mite de tasa
|
| 102 |
+
else:
|
| 103 |
+
break
|
| 104 |
|
| 105 |
return [{'playlist_id': playlist['id'], 'playlist_name': playlist['name']} for playlist in playlists]
|
| 106 |
|
|
|
|
| 110 |
headers = {'Authorization': f'Bearer {token}'}
|
| 111 |
canciones = []
|
| 112 |
|
| 113 |
+
response = hacer_request_con_reintento(url, headers)
|
| 114 |
+
if response:
|
| 115 |
+
tracks = response.json().get('items')
|
| 116 |
+
for item in tracks:
|
| 117 |
+
track = item.get('track')
|
| 118 |
+
if track:
|
| 119 |
+
audio_features = obtener_caracteristicas_audio(token, track['id'])
|
| 120 |
+
canciones.append({
|
| 121 |
+
'playlist_name': playlist_name,
|
| 122 |
+
'artista': track['artists'][0]['name'] if track['artists'] else 'Desconocido',
|
| 123 |
+
'titulo': track['name'],
|
| 124 |
+
'isrc': track['external_ids'].get('isrc', 'No disponible'),
|
| 125 |
+
'popularity': track.get('popularity', 'No disponible'),
|
| 126 |
+
'valence': audio_features.get('valence', 'No disponible'),
|
| 127 |
+
'danceability': audio_features.get('danceability', 'No disponible'),
|
| 128 |
+
'energy': audio_features.get('energy', 'No disponible'),
|
| 129 |
+
'tempo': audio_features.get('tempo', 'No disponible'),
|
| 130 |
+
'speechiness': audio_features.get('speechiness', 'No disponible'),
|
| 131 |
+
'instrumentalness': audio_features.get('instrumentalness', 'No disponible'),
|
| 132 |
+
'duration': track.get('duration_ms', 'No disponible'),
|
| 133 |
+
'release_year': track.get('album', {}).get('release_date', 'No disponible').split('-')[0] if track.get('album', {}).get('release_date') else 'No disponible',
|
| 134 |
+
'record_label': obtener_record_label_spotify(track['album']['id'], token)
|
| 135 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
return canciones
|
| 137 |
|
| 138 |
def obtener_caracteristicas_audio(token, track_id):
|
| 139 |
url = f'https://api.spotify.com/v1/audio-features/{track_id}'
|
| 140 |
headers = {'Authorization': f'Bearer {token}'}
|
| 141 |
+
response = hacer_request_con_reintento(url, headers)
|
| 142 |
+
return response.json() if response else {}
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
def obtener_record_label_spotify(album_id, token):
|
| 145 |
url = f'https://api.spotify.com/v1/albums/{album_id}'
|
| 146 |
headers = {'Authorization': f'Bearer {token}'}
|
| 147 |
+
response = hacer_request_con_reintento(url, headers)
|
| 148 |
+
return response.json().get('label', 'No disponible') if response else 'No disponible'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
# Funci贸n principal de la interfaz
|
| 151 |
def interface(project_name, query, num_spotify_playlists=50):
|