Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,14 +7,14 @@ from openpyxl import Workbook
|
|
| 7 |
import shutil
|
| 8 |
|
| 9 |
# Lista de credenciales de API de Spotify
|
| 10 |
-
client_ids = [
|
| 11 |
-
'
|
| 12 |
'528d1d6733ba46eb8e9532e56ced61eb', 'aed6435c51484dc18b8c18aa74dd51f8',
|
| 13 |
'9df51caba5d247dc921b21de35a47c44', '191227c66e0d4be692bc8ee73ea6eb3d',
|
| 14 |
'e272c0705c7c4fd68937c58adaa446ed'
|
| 15 |
]
|
| 16 |
client_secrets = [
|
| 17 |
-
'
|
| 18 |
'3dbdcf9a12634cfca9e6aca8d3093020', '8662b33d594f4d198ea025d4aa9f0b98',
|
| 19 |
'0e39502ec7e74fe99bb74245678d5f0d', '2d2a895d85874c088897dd9894dc64ad',
|
| 20 |
'9fdfa58ea0a94ce7a0cb34fa19fb7d74'
|
|
@@ -41,14 +41,14 @@ def cambiar_api_key():
|
|
| 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=
|
| 45 |
print("Buscando playlists en Spotify...")
|
| 46 |
url = 'https://api.spotify.com/v1/search'
|
| 47 |
headers = {'Authorization': f'Bearer {token}'}
|
| 48 |
playlists = []
|
| 49 |
|
| 50 |
try:
|
| 51 |
-
if limit <=
|
| 52 |
params = {'q': query, 'type': 'playlist', 'limit': limit}
|
| 53 |
response = requests.get(url, headers=headers, params=params)
|
| 54 |
if response.status_code == 429: # Límite alcanzado
|
|
@@ -59,16 +59,16 @@ def buscar_playlists_spotify(token, query, limit=50):
|
|
| 59 |
else:
|
| 60 |
offset = 0
|
| 61 |
while limit > 0:
|
| 62 |
-
params = {'q': query, 'type': 'playlist', 'limit': min(
|
| 63 |
response = requests.get(url, headers=headers, params=params)
|
| 64 |
if response.status_code == 429: # Límite alcanzado
|
| 65 |
print("Límite de peticiones alcanzado, cambiando API Key...")
|
| 66 |
token = cambiar_api_key()
|
| 67 |
response = requests.get(url, headers={'Authorization': f'Bearer {token}'}, params=params)
|
| 68 |
playlists.extend(response.json().get('playlists', {}).get('items', []))
|
| 69 |
-
limit -= min(
|
| 70 |
-
offset +=
|
| 71 |
-
time.sleep(
|
| 72 |
except Exception as e:
|
| 73 |
print(f"Error al buscar playlists: {e}")
|
| 74 |
|
|
@@ -135,7 +135,7 @@ def obtener_record_label_spotify(album_id, token):
|
|
| 135 |
return album_info.get('label', 'No disponible')
|
| 136 |
|
| 137 |
# Función principal de la interfaz
|
| 138 |
-
def interface(project_name, query, num_spotify_playlists=
|
| 139 |
# Obtener tokens y claves
|
| 140 |
token_spotify = obtener_token(client_ids[current_api_index], client_secrets[current_api_index])
|
| 141 |
playlists_spotify = buscar_playlists_spotify(token_spotify, query, num_spotify_playlists)
|
|
@@ -143,7 +143,7 @@ def interface(project_name, query, num_spotify_playlists=50):
|
|
| 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(
|
| 147 |
|
| 148 |
# Crear DataFrame
|
| 149 |
df = pd.DataFrame(canciones_spotify)
|
|
@@ -168,7 +168,7 @@ iface = gr.Interface(
|
|
| 168 |
inputs=[
|
| 169 |
gr.Textbox(label="Nombre del Proyecto"),
|
| 170 |
gr.Textbox(label="Keywords - Palabras Clave para tu búsqueda"),
|
| 171 |
-
gr.Number(label="Numero de Playlists que vamos a buscar con estas Keywords", value=
|
| 172 |
],
|
| 173 |
outputs=[gr.Dataframe(), gr.File(label="Download Excel")],
|
| 174 |
title="Spotify Playlist Fetcher",
|
|
|
|
| 7 |
import shutil
|
| 8 |
|
| 9 |
# Lista de credenciales de API de Spotify
|
| 10 |
+
client_ids = [
|
| 11 |
+
'107286416613436ab5c63b56d3564f9e','807008cb2ce041178c1871973fc81716',
|
| 12 |
'528d1d6733ba46eb8e9532e56ced61eb', 'aed6435c51484dc18b8c18aa74dd51f8',
|
| 13 |
'9df51caba5d247dc921b21de35a47c44', '191227c66e0d4be692bc8ee73ea6eb3d',
|
| 14 |
'e272c0705c7c4fd68937c58adaa446ed'
|
| 15 |
]
|
| 16 |
client_secrets = [
|
| 17 |
+
'4de4c14ed109420c9517c083b8018f8c', 'b6c3fbe2304145e4b268f05eefd6ab2a',
|
| 18 |
'3dbdcf9a12634cfca9e6aca8d3093020', '8662b33d594f4d198ea025d4aa9f0b98',
|
| 19 |
'0e39502ec7e74fe99bb74245678d5f0d', '2d2a895d85874c088897dd9894dc64ad',
|
| 20 |
'9fdfa58ea0a94ce7a0cb34fa19fb7d74'
|
|
|
|
| 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=40):
|
| 45 |
print("Buscando playlists en Spotify...")
|
| 46 |
url = 'https://api.spotify.com/v1/search'
|
| 47 |
headers = {'Authorization': f'Bearer {token}'}
|
| 48 |
playlists = []
|
| 49 |
|
| 50 |
try:
|
| 51 |
+
if limit <= 40:
|
| 52 |
params = {'q': query, 'type': 'playlist', 'limit': limit}
|
| 53 |
response = requests.get(url, headers=headers, params=params)
|
| 54 |
if response.status_code == 429: # Límite alcanzado
|
|
|
|
| 59 |
else:
|
| 60 |
offset = 0
|
| 61 |
while limit > 0:
|
| 62 |
+
params = {'q': query, 'type': 'playlist', 'limit': min(40, limit), 'offset': offset}
|
| 63 |
response = requests.get(url, headers=headers, params=params)
|
| 64 |
if response.status_code == 429: # Límite alcanzado
|
| 65 |
print("Límite de peticiones alcanzado, cambiando API Key...")
|
| 66 |
token = cambiar_api_key()
|
| 67 |
response = requests.get(url, headers={'Authorization': f'Bearer {token}'}, params=params)
|
| 68 |
playlists.extend(response.json().get('playlists', {}).get('items', []))
|
| 69 |
+
limit -= min(40, limit)
|
| 70 |
+
offset += 40
|
| 71 |
+
time.sleep(3) # Pausa de 3 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 |
|
|
|
|
| 135 |
return album_info.get('label', 'No disponible')
|
| 136 |
|
| 137 |
# Función principal de la interfaz
|
| 138 |
+
def interface(project_name, query, num_spotify_playlists=40):
|
| 139 |
# Obtener tokens y claves
|
| 140 |
token_spotify = obtener_token(client_ids[current_api_index], client_secrets[current_api_index])
|
| 141 |
playlists_spotify = buscar_playlists_spotify(token_spotify, query, num_spotify_playlists)
|
|
|
|
| 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(3) # Pausa de 3 segundos entre la obtención de canciones para evitar el límite de tasa
|
| 147 |
|
| 148 |
# Crear DataFrame
|
| 149 |
df = pd.DataFrame(canciones_spotify)
|
|
|
|
| 168 |
inputs=[
|
| 169 |
gr.Textbox(label="Nombre del Proyecto"),
|
| 170 |
gr.Textbox(label="Keywords - Palabras Clave para tu búsqueda"),
|
| 171 |
+
gr.Number(label="Numero de Playlists que vamos a buscar con estas Keywords", value=40, minimum=1, maximum=1000)
|
| 172 |
],
|
| 173 |
outputs=[gr.Dataframe(), gr.File(label="Download Excel")],
|
| 174 |
title="Spotify Playlist Fetcher",
|