|
| 1 | +using System; |
| 2 | +using System.Net.Http; |
| 3 | +using System.Net.Http.Headers; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using Newtonsoft.Json.Linq; |
| 7 | +using System.Configuration; |
| 8 | + |
| 9 | +/* |
| 10 | + * EJERCICIO: |
| 11 | + * ¡Dos de las bandas más grandes de la historia están de vuelta! |
| 12 | + * Oasis y Linkin Park han anunciado nueva gira, pero, ¿quién es más popular? |
| 13 | + * Desarrolla un programa que se conecte al API de Spotify y los compare. |
| 14 | + * Requisitos: |
| 15 | + * 1. Crea una cuenta de desarrollo en https://developer.spotify.com. |
| 16 | + * 2. Conéctate al API utilizando tu lenguaje de programación. |
| 17 | + * 3. Recupera datos de los endpoint que tú quieras. |
| 18 | + * Acciones: |
| 19 | + * 1. Accede a las estadísticas de las dos bandas. |
| 20 | + * Por ejemplo: número total de seguidores, escuchas mensuales, |
| 21 | + * canción con más reproducciones... |
| 22 | + * 2. Compara los resultados de, por lo menos, 3 endpoint. |
| 23 | + * 3. Muestra todos los resultados por consola para notificar al usuario. |
| 24 | + * 4. Desarrolla un criterio para seleccionar qué banda es más popular. |
| 25 | + */ |
| 26 | + |
| 27 | +namespace Reto_37 |
| 28 | +{ |
| 29 | + internal class deathwing696 |
| 30 | + { |
| 31 | + public class Spotify |
| 32 | + { |
| 33 | + private string urlAPI = "https://api.spotify.com/v1/"; |
| 34 | + private string urlApiToken = "https://accounts.spotify.com/api/token"; |
| 35 | + private static string clientId = ConfigurationManager.AppSettings["SpotifyClientId"]; |
| 36 | + private static string clientSecret = ConfigurationManager.AppSettings["SpotifyClientSecret"]; |
| 37 | + private string artist1; |
| 38 | + private string artist2; |
| 39 | + private int artist1Points = 0; |
| 40 | + private int artist2Points = 0; |
| 41 | + |
| 42 | + public Spotify(string artist1, string artist2) |
| 43 | + { |
| 44 | + this.artist1 = artist1; |
| 45 | + this.artist2 = artist2; |
| 46 | + } |
| 47 | + |
| 48 | + internal async Task<string> GetSpotifyAccessToken() |
| 49 | + { |
| 50 | + using (var client = new HttpClient()) |
| 51 | + { |
| 52 | + var request = new HttpRequestMessage(HttpMethod.Post, urlApiToken); |
| 53 | + var clientCredentials = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{clientId}:{clientSecret}")); |
| 54 | + |
| 55 | + request.Headers.Authorization = new AuthenticationHeaderValue("Basic", clientCredentials); |
| 56 | + request.Content = new StringContent("grant_type=client_credentials", Encoding.UTF8, "application/x-www-form-urlencoded"); |
| 57 | + |
| 58 | + var response = await client.SendAsync(request); |
| 59 | + response.EnsureSuccessStatusCode(); |
| 60 | + |
| 61 | + var responseBody = await response.Content.ReadAsStringAsync(); |
| 62 | + var json = JObject.Parse(responseBody); |
| 63 | + |
| 64 | + return json["access_token"].ToString(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + internal async Task CompareArtists(string token) |
| 69 | + { |
| 70 | + var artist1Data = await GetArtistData(token, artist1); |
| 71 | + var artist2Data = await GetArtistData(token, artist2); |
| 72 | + |
| 73 | + Console.WriteLine($"Artista: {artist1Data.Name}"); |
| 74 | + Console.WriteLine($"Seguidores: {artist1Data.Followers}"); |
| 75 | + Console.WriteLine($"Popularidad: {artist1Data.Popularity}"); |
| 76 | + Console.WriteLine($"Canción más popular: {artist1Data.TopTrack} con {artist1Data.TopTrackPopularity} de popularidad"); |
| 77 | + Console.WriteLine(); |
| 78 | + |
| 79 | + Console.WriteLine($"Artista: {artist2Data.Name}"); |
| 80 | + Console.WriteLine($"Seguidores: {artist2Data.Followers}"); |
| 81 | + Console.WriteLine($"Popularidad: {artist2Data.Popularity}"); |
| 82 | + Console.WriteLine($"Canción más popular: {artist2Data.TopTrack} con {artist2Data.TopTrackPopularity} de popularidad"); |
| 83 | + Console.WriteLine(); |
| 84 | + |
| 85 | + // Comparación |
| 86 | + Console.WriteLine("Comparación:"); |
| 87 | + string moreFollowers; |
| 88 | + |
| 89 | + if (artist1Data.Followers > artist2Data.Followers) |
| 90 | + { |
| 91 | + moreFollowers = artist1Data.Name; |
| 92 | + artist1Points++; |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + moreFollowers = artist2Data.Name; |
| 97 | + artist2Points++; |
| 98 | + } |
| 99 | + Console.WriteLine($"Artista con más seguidores: {moreFollowers}"); |
| 100 | + |
| 101 | + string morePopular; |
| 102 | + if (artist1Data.Popularity > artist2Data.Popularity) |
| 103 | + { |
| 104 | + morePopular = artist1Data.Name; |
| 105 | + artist1Points++; |
| 106 | + } |
| 107 | + else |
| 108 | + { |
| 109 | + morePopular = artist2Data.Name; |
| 110 | + artist2Points++; |
| 111 | + } |
| 112 | + Console.WriteLine($"Artista más popular: {morePopular}"); |
| 113 | + |
| 114 | + string topTrack, artistTopTrack; |
| 115 | + if (artist1Data.TopTrackPopularity > artist2Data.TopTrackPopularity) |
| 116 | + { |
| 117 | + topTrack = artist1Data.TopTrack; |
| 118 | + artistTopTrack = artist1Data.Name; |
| 119 | + artist1Points++; |
| 120 | + } |
| 121 | + else |
| 122 | + { |
| 123 | + topTrack = artist2Data.TopTrack; |
| 124 | + artistTopTrack = artist2Data.Name; |
| 125 | + artist2Points++; |
| 126 | + } |
| 127 | + Console.WriteLine($"Canción con más popularidad: {topTrack} de {artistTopTrack}"); |
| 128 | + |
| 129 | + Console.WriteLine("PUNTUACIÓN FINAL:"); |
| 130 | + |
| 131 | + if (artist1Points > artist2Points) |
| 132 | + { |
| 133 | + Console.WriteLine($"El ganador de este duelo es {artist1Data.Name} con {artist1Points} puntos"); |
| 134 | + } |
| 135 | + else if (artist2Points > artist1Points) |
| 136 | + { |
| 137 | + Console.WriteLine($"El ganador de este duelo es {artist2Data.Name} con {artist2Points} puntos"); |
| 138 | + } |
| 139 | + else |
| 140 | + { |
| 141 | + Console.WriteLine($"Ha habido un empate en el duelo entre {artist1Data.Name} y {artist2Data.Name} con {artist1Points} puntos"); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private async Task<(string Name, int Followers, int Popularity, string TopTrack, int TopTrackPopularity)> GetArtistData(string token, string artistName) |
| 146 | + { |
| 147 | + using (var client = new HttpClient()) |
| 148 | + { |
| 149 | + client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); |
| 150 | + |
| 151 | + // Buscar al artista |
| 152 | + var searchResponse = await client.GetAsync($"{urlAPI}search?q={Uri.EscapeDataString(artistName)}&type=artist"); |
| 153 | + searchResponse.EnsureSuccessStatusCode(); |
| 154 | + var searchResult = JObject.Parse(await searchResponse.Content.ReadAsStringAsync()); |
| 155 | + |
| 156 | + var artist = searchResult["artists"]["items"].First; |
| 157 | + string artistId = artist["id"].ToString(); |
| 158 | + string name = artist["name"].ToString(); |
| 159 | + int followers = (int)artist["followers"]["total"]; |
| 160 | + int popularity = (int)artist["popularity"]; |
| 161 | + |
| 162 | + // Obtener las canciones más populares |
| 163 | + var tracksResponse = await client.GetAsync($"{urlAPI}artists/{artistId}/top-tracks?market=US"); |
| 164 | + tracksResponse.EnsureSuccessStatusCode(); |
| 165 | + var tracksResult = JObject.Parse(await tracksResponse.Content.ReadAsStringAsync()); |
| 166 | + |
| 167 | + var topTrack = tracksResult["tracks"].First; |
| 168 | + string topTrackName = topTrack["name"].ToString(); |
| 169 | + int topTrackPopularity = (int)topTrack["popularity"]; |
| 170 | + |
| 171 | + return (name, followers, popularity, topTrackName, topTrackPopularity); |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + static async Task Main(string[] args) |
| 177 | + { |
| 178 | + Spotify spotify = new Spotify("Oasis", "Linkin Park"); |
| 179 | + |
| 180 | + string token = await spotify.GetSpotifyAccessToken(); |
| 181 | + |
| 182 | + await spotify.CompareArtists(token); |
| 183 | + |
| 184 | + Console.ReadKey(); |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments