Skip to content

Commit a54f8e9

Browse files
authored
Merge pull request mouredev#7341 from pyramsd/reto#37
mouredev#37 - python
2 parents 6aa1482 + 70d1796 commit a54f8e9

File tree

1 file changed

+76
-0
lines changed
  • Roadmap/37 - OASIS VS LINKIN PARK/python

1 file changed

+76
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import requests
2+
import base64
3+
4+
client_id = "CLIENT_ID"
5+
client_secret = "CLIENT_SECRET"
6+
7+
auth_url = 'https://accounts.spotify.com/api/token'
8+
base_url = 'https://api.spotify.com/v1/'
9+
10+
artists = {
11+
"Oasis": "2DaxqgrOhkeH0fpeiQq2f4",
12+
"Linkin Park": "6XyY86QOPPrYVGvF9ch6wz"
13+
}
14+
15+
# Función para obtener el token de autenticación
16+
def get_token(client_id, client_secret):
17+
auth_str = f"{client_id}:{client_secret}"
18+
b64_auth_str = base64.b64encode(auth_str.encode()).decode()
19+
20+
headers = {
21+
'Authorization': f'Basic {b64_auth_str}',
22+
}
23+
data = {
24+
'grant_type': 'client_credentials'
25+
}
26+
27+
response = requests.post(auth_url, headers=headers, data=data)
28+
if response.status_code != 200:
29+
raise Exception("Error en la autenticación. Verifica tus credenciales.")
30+
31+
response_data = response.json()
32+
return response_data['access_token']
33+
34+
# Función para obtener datos del artista (Oasis y Linkin Park)
35+
def get_artist_data(artist_id, token):
36+
headers = {
37+
'Authorization': f'Bearer {token}',
38+
}
39+
try:
40+
artist_url = f'{base_url}artists/{artist_id}'
41+
artist_data = requests.get(artist_url, headers=headers).json()
42+
43+
top_tracks_url = f'{base_url}artists/{artist_id}/top-tracks?market=US'
44+
top_tracks_data = requests.get(top_tracks_url, headers=headers).json()
45+
46+
return artist_data, top_tracks_data
47+
except requests.exceptions.RequestException as e:
48+
print(f"Error al obtener datos del artista: {e}")
49+
return None, None
50+
51+
def compare_artists(artists_data):
52+
for artist, data in artists_data.items():
53+
artist_info, top_tracks = data
54+
if artist_info and top_tracks:
55+
print(f"Artista: {artist}")
56+
print(f"Seguidores: {artist_info['followers']['total']}")
57+
print(f"Popularidad: {artist_info['popularity']}")
58+
print(f"Canción más popular: {top_tracks['tracks'][0]['name']} - {top_tracks['tracks'][0]['popularity']} popularidad\n")
59+
else:
60+
print(f"No se pudo obtener datos para {artist}")
61+
62+
def main():
63+
# Obtener el token de acceso
64+
token = get_token(client_id, client_secret)
65+
66+
# Obtener datos
67+
artists_data = {}
68+
for artist_name, artist_id in artists.items():
69+
artist_info, top_tracks = get_artist_data(artist_id, token)
70+
artists_data[artist_name] = (artist_info, top_tracks)
71+
72+
compare_artists(artists_data)
73+
74+
if __name__ == "__main__":
75+
main()
76+

0 commit comments

Comments
 (0)