Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from tempfile import NamedTemporaryFile
|
| 5 |
+
from openpyxl import Workbook
|
| 6 |
+
|
| 7 |
+
# Funciones para Spotify
|
| 8 |
+
def obtener_token(client_id, client_secret):
|
| 9 |
+
print("Obteniendo token de Spotify...")
|
| 10 |
+
url = 'https://accounts.spotify.com/api/token'
|
| 11 |
+
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
|
| 12 |
+
payload = {'grant_type': 'client_credentials'}
|
| 13 |
+
response = requests.post(url, headers=headers, data=payload, auth=(client_id, client_secret))
|
| 14 |
+
return response.json().get('access_token')
|
| 15 |
+
|
| 16 |
+
def buscar_playlists_spotify(token, query, limit=50):
|
| 17 |
+
print("Buscando playlists en Spotify...")
|
| 18 |
+
url = 'https://api.spotify.com/v1/search'
|
| 19 |
+
headers = {'Authorization': f'Bearer {token}'}
|
| 20 |
+
playlists = []
|
| 21 |
+
|
| 22 |
+
if limit <= 50:
|
| 23 |
+
params = {'q': query, 'type': 'playlist', 'limit': limit}
|
| 24 |
+
response = requests.get(url, headers=headers, params=params)
|
| 25 |
+
playlists.extend(response.json().get('playlists', {}).get('items', []))
|
| 26 |
+
else:
|
| 27 |
+
offset = 0
|
| 28 |
+
while limit > 0:
|
| 29 |
+
params = {'q': query, 'type': 'playlist', 'limit': min(50, limit), 'offset': offset}
|
| 30 |
+
response = requests.get(url, headers=headers, params=params)
|
| 31 |
+
playlists.extend(response.json().get('playlists', {}).get('items', []))
|
| 32 |
+
limit -= min(50, limit)
|
| 33 |
+
offset += 50
|
| 34 |
+
|
| 35 |
+
return [{'playlist_id': playlist['id'], 'playlist_name': playlist['name']} for playlist in playlists]
|
| 36 |
+
|
| 37 |
+
def obtener_canciones_playlist_spotify(token, playlist_id):
|
| 38 |
+
print(f"Obteniendo canciones de la playlist {playlist_id} de Spotify...")
|
| 39 |
+
url = f'https://api.spotify.com/v1/playlists/{playlist_id}/tracks'
|
| 40 |
+
headers = {'Authorization': f'Bearer {token}'}
|
| 41 |
+
response = requests.get(url, headers=headers)
|
| 42 |
+
canciones = []
|
| 43 |
+
if response.status_code == 200:
|
| 44 |
+
tracks = response.json().get('items')
|
| 45 |
+
for item in tracks:
|
| 46 |
+
track = item.get('track')
|
| 47 |
+
if track:
|
| 48 |
+
audio_features = obtener_caracteristicas_audio(token, track['id'])
|
| 49 |
+
canciones.append({
|
| 50 |
+
'artista': track['artists'][0]['name'] if track['artists'] else 'Desconocido',
|
| 51 |
+
'titulo': track['name'],
|
| 52 |
+
'isrc': track['external_ids'].get('isrc', 'No disponible'),
|
| 53 |
+
'popularity': track.get('popularity', 'No disponible'),
|
| 54 |
+
'valence': audio_features.get('valence', 'No disponible'),
|
| 55 |
+
'danceability': audio_features.get('danceability', 'No disponible'),
|
| 56 |
+
'energy': audio_features.get('energy', 'No disponible'),
|
| 57 |
+
'tempo': audio_features.get('tempo', 'No disponible'),
|
| 58 |
+
'speechiness': audio_features.get('speechiness', 'No disponible'),
|
| 59 |
+
'instrumentalness': audio_features.get('instrumentalness', 'No disponible'),
|
| 60 |
+
'link': track['external_urls']['spotify'],
|
| 61 |
+
'record_label': obtener_record_label_spotify(track['album']['id'], token),
|
| 62 |
+
'source': 'Spotify'
|
| 63 |
+
})
|
| 64 |
+
return canciones
|
| 65 |
+
|
| 66 |
+
def obtener_caracteristicas_audio(token, track_id):
|
| 67 |
+
url = f'https://api.spotify.com/v1/audio-features/{track_id}'
|
| 68 |
+
headers = {'Authorization': f'Bearer {token}'}
|
| 69 |
+
response = requests.get(url, headers=headers)
|
| 70 |
+
return response.json() if response.status_code == 200 else {}
|
| 71 |
+
|
| 72 |
+
def obtener_record_label_spotify(album_id, token):
|
| 73 |
+
url = f'https://api.spotify.com/v1/albums/{album_id}'
|
| 74 |
+
headers = {'Authorization': f'Bearer {token}'}
|
| 75 |
+
response = requests.get(url, headers=headers)
|
| 76 |
+
album_info = response.json() if response.status_code == 200 else {}
|
| 77 |
+
return album_info.get('label', 'No disponible')
|
| 78 |
+
|
| 79 |
+
# Funci贸n principal de la interfaz
|
| 80 |
+
def interface(query, num_spotify_playlists=50):
|
| 81 |
+
# Obtener tokens y claves
|
| 82 |
+
client_id = 'b4a2add66ffb4f1198b94b087b365c65'
|
| 83 |
+
client_secret = '8045eacf956a477299d2bc41752f1f73'
|
| 84 |
+
|
| 85 |
+
# Spotify
|
| 86 |
+
token_spotify = obtener_token(client_id, client_secret)
|
| 87 |
+
playlists_spotify = buscar_playlists_spotify(token_spotify, query, num_spotify_playlists)
|
| 88 |
+
canciones_spotify = []
|
| 89 |
+
for playlist in playlists_spotify:
|
| 90 |
+
songs = obtener_canciones_playlist_spotify(token_spotify, playlist['playlist_id'])
|
| 91 |
+
canciones_spotify.extend(songs)
|
| 92 |
+
|
| 93 |
+
# Crear DataFrame
|
| 94 |
+
df = pd.DataFrame(canciones_spotify)
|
| 95 |
+
df.rename(columns={'isrc': 'ISRCs'}, inplace=True)
|
| 96 |
+
|
| 97 |
+
# Ordenar por popularidad
|
| 98 |
+
df.sort_values(by=['popularity'], ascending=False, inplace=True)
|
| 99 |
+
|
| 100 |
+
with NamedTemporaryFile(delete=False, suffix='.xlsx') as tmpfile:
|
| 101 |
+
df.to_excel(tmpfile.name, index=False)
|
| 102 |
+
return df, tmpfile.name # Devuelve el DataFrame y el enlace al archivo Excel
|
| 103 |
+
|
| 104 |
+
# Configuraci贸n de Gradio
|
| 105 |
+
iface = gr.Interface(
|
| 106 |
+
fn=interface,
|
| 107 |
+
inputs=[
|
| 108 |
+
gr.Textbox(label="Search Query"),
|
| 109 |
+
gr.Number(label="Number of Spotify Playlists", value=50, minimum=1, maximum=1000)
|
| 110 |
+
],
|
| 111 |
+
outputs=[gr.Dataframe(), gr.File(label="Download Excel")],
|
| 112 |
+
title="Spotify Playlist Fetcher",
|
| 113 |
+
description="Enter a search query to fetch playlists and their songs from Spotify. Client credentials are pre-configured."
|
| 114 |
+
)
|
| 115 |
+
iface.launch()
|