|
| 1 | +import base64 |
| 2 | +import requests |
| 3 | + |
| 4 | +CLIENT_ID = "Mi CLIENT_ID" |
| 5 | +CLIENT_SECRET = "Mi CLIENT_SECRET" |
| 6 | + |
| 7 | + |
| 8 | +def get_token() -> str: |
| 9 | + url = "https://accounts.spotify.com/api/token" |
| 10 | + headers = { |
| 11 | + "Authorization": "Basic " + base64.b64encode(f"{CLIENT_ID}:{CLIENT_SECRET}".encode()).decode(), |
| 12 | + "Content-Type": "application/x-www-form-urlencoded" |
| 13 | + } |
| 14 | + data = {"grant_type": "client_credentials"} |
| 15 | + |
| 16 | + response = requests.post(url, headers=headers, data=data) |
| 17 | + if response.status_code != 200: |
| 18 | + raise Exception( |
| 19 | + f"Error obteniendo el token de Spotify: {response.json()}." |
| 20 | + ) |
| 21 | + |
| 22 | + return response.json()["access_token"] |
| 23 | + |
| 24 | + |
| 25 | +def search_artist(token: str, name: str): |
| 26 | + url = f"https://api.spotify.com/v1/search?q={name}&type=artist&limit=1" |
| 27 | + headers = {"Authorization": f"Bearer {token}"} |
| 28 | + |
| 29 | + response = requests.get(url, headers=headers) |
| 30 | + if response.status_code != 200: |
| 31 | + raise Exception( |
| 32 | + f"Error obteniendo el artista: {response.json()}." |
| 33 | + ) |
| 34 | + |
| 35 | + results = response.json() |
| 36 | + if results["artists"]["items"]: |
| 37 | + return results["artists"]["items"][0]["id"] |
| 38 | + else: |
| 39 | + raise Exception( |
| 40 | + f"El artista {name} no se ha encontrado." |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +def get_artist_data(token: str, id: str): |
| 45 | + |
| 46 | + url = f"https://api.spotify.com/v1/artists/{id}" |
| 47 | + headers = {"Authorization": f"Bearer {token}"} |
| 48 | + |
| 49 | + response = requests.get(url, headers=headers) |
| 50 | + if response.status_code != 200: |
| 51 | + raise Exception( |
| 52 | + f"Error obteniendo los datos del artista: {response.json()}." |
| 53 | + ) |
| 54 | + |
| 55 | + results = response.json() |
| 56 | + return { |
| 57 | + "name": results["name"], |
| 58 | + "followers": results["followers"]["total"], |
| 59 | + "popularity": results["popularity"] |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | +def get_artist_top_track(token: str, id: str): |
| 64 | + |
| 65 | + url = f"https://api.spotify.com/v1/artists/{id}/top-tracks" |
| 66 | + headers = {"Authorization": f"Bearer {token}"} |
| 67 | + |
| 68 | + response = requests.get(url, headers=headers) |
| 69 | + if response.status_code != 200: |
| 70 | + raise Exception( |
| 71 | + f"Error obteniendo las canciones del artista: {response.json()}." |
| 72 | + ) |
| 73 | + |
| 74 | + results = response.json() |
| 75 | + |
| 76 | + top_track = max(results["tracks"], key=lambda track: track["popularity"]) |
| 77 | + |
| 78 | + return { |
| 79 | + "name": top_track["name"], |
| 80 | + "popularity": top_track["popularity"] |
| 81 | + } |
| 82 | + |
| 83 | + |
| 84 | +# 1. Token |
| 85 | +token = get_token() |
| 86 | + |
| 87 | +# 2. IDs |
| 88 | +artist_1_id = search_artist(token, "Taylor Swift") |
| 89 | +artist_2_id = search_artist(token, "Linkin Park") |
| 90 | + |
| 91 | +# 3. Data |
| 92 | + |
| 93 | +# 3.1. Seguidores y popularidad |
| 94 | +artist_1 = get_artist_data(token, artist_1_id) |
| 95 | +artist_2 = get_artist_data(token, artist_2_id) |
| 96 | + |
| 97 | +# 3.2. Canción más popular |
| 98 | +top_track_artist_1 = get_artist_top_track(token, artist_1_id) |
| 99 | +top_track_artist_2 = get_artist_top_track(token, artist_2_id) |
| 100 | + |
| 101 | +# 4. Comparativa |
| 102 | +artist_1_counter = 0 |
| 103 | +artist_2_counter = 0 |
| 104 | + |
| 105 | +print(f"\nComparación de artistas:\n") |
| 106 | +print(f"{artist_1["name"]}") |
| 107 | +print(f"{artist_2["name"]}") |
| 108 | + |
| 109 | +# 4.1. Seguidores |
| 110 | +print(f"\nComparación seguidores:\n") |
| 111 | +print(f"Seguidores {artist_1["name"]}: {artist_1["followers"]}") |
| 112 | +print(f"Seguidores {artist_2["name"]}: {artist_2["followers"]}") |
| 113 | + |
| 114 | +if artist_1["followers"] > artist_2["followers"]: |
| 115 | + print(f"{artist_1["name"]} es más popular en número de seguidores.") |
| 116 | + artist_1_counter += 1 |
| 117 | +else: |
| 118 | + print(f"{artist_2["name"]} es más popular en número de seguidores.") |
| 119 | + artist_2_counter += 1 |
| 120 | + |
| 121 | +# 4.2. Popularidad |
| 122 | +print(f"\nComparación popularidad:\n") |
| 123 | +print(f"Popularidad {artist_1["name"]}: {artist_1["popularity"]}") |
| 124 | +print(f"Popularidad {artist_2["name"]}: {artist_2["popularity"]}") |
| 125 | + |
| 126 | +if artist_1["popularity"] > artist_2["popularity"]: |
| 127 | + print(f"{artist_1["name"]} es más popular a nivel general.") |
| 128 | + artist_1_counter += 1 |
| 129 | +else: |
| 130 | + print(f"{artist_2["name"]} es más popular a nivel general.") |
| 131 | + artist_2_counter += 1 |
| 132 | + |
| 133 | +# 4.3. Canción |
| 134 | +print(f"\nComparación canción:\n") |
| 135 | +print( |
| 136 | + f"Canción {top_track_artist_1["name"]} ({artist_1["name"]}): {top_track_artist_1["popularity"]} popularidad.") |
| 137 | +print( |
| 138 | + f"Canción {top_track_artist_2["name"]} ({artist_2["name"]}): {top_track_artist_2["popularity"]} popularidad.") |
| 139 | + |
| 140 | +if top_track_artist_1["popularity"] > top_track_artist_2["popularity"]: |
| 141 | + print( |
| 142 | + f"La canción {top_track_artist_1["name"]} de {artist_1["name"]} es más popular.") |
| 143 | + artist_1_counter += 1 |
| 144 | +else: |
| 145 | + print( |
| 146 | + f"La canción {top_track_artist_2["name"]} de {artist_2["name"]} es más popular.") |
| 147 | + artist_2_counter += 1 |
| 148 | + |
| 149 | +# 5. Resultado |
| 150 | +print(f"\nResultado final:\n") |
| 151 | +print( |
| 152 | + f"{artist_1["name"] if artist_1_counter > artist_2_counter else artist_2["name"]} es más popular.") |
0 commit comments