|
| 1 | +import java.io.BufferedReader; |
| 2 | +import java.io.InputStreamReader; |
| 3 | +import java.net.HttpURLConnection; |
| 4 | +import java.net.URL; |
| 5 | +import java.util.Base64; |
| 6 | +import java.util.Scanner; |
| 7 | +import org.json.JSONArray; |
| 8 | +import org.json.JSONObject; |
| 9 | + |
| 10 | +public class SpotifyComparison { |
| 11 | + |
| 12 | + private static final String CLIENT_ID = "YOUR_CLIENT_ID"; |
| 13 | + private static final String CLIENT_SECRET = "YOUR_CLIENT_SECRET"; |
| 14 | + |
| 15 | + public static void main(String[] args) throws Exception { |
| 16 | + Scanner scanner = new Scanner(System.in); |
| 17 | + |
| 18 | + System.out.print("Introduce el nombre del primer grupo: "); |
| 19 | + String band1 = scanner.nextLine(); |
| 20 | + System.out.print("Introduce el nombre del segundo grupo: "); |
| 21 | + String band2 = scanner.nextLine(); |
| 22 | + |
| 23 | + String token = getAccessToken(); |
| 24 | + |
| 25 | + JSONObject band1Data = getArtistData(token, band1); |
| 26 | + JSONObject band2Data = getArtistData(token, band2); |
| 27 | + |
| 28 | + JSONObject band1Stats = getArtistStats(band1Data.getString("id"), token); |
| 29 | + JSONObject band2Stats = getArtistStats(band2Data.getString("id"), token); |
| 30 | + |
| 31 | + JSONObject band1TopTrack = getArtistTopTrack(band1Data.getString("id"), token); |
| 32 | + JSONObject band2TopTrack = getArtistTopTrack(band2Data.getString("id"), token); |
| 33 | + |
| 34 | + System.out.println("\n" + band1 + " Stats: " + band1Stats); |
| 35 | + System.out.println("Canción más popular de " + band1 + ": " + band1TopTrack); |
| 36 | + |
| 37 | + System.out.println("\n" + band2 + " Stats: " + band2Stats); |
| 38 | + System.out.println("Canción más popular de " + band2 + ": " + band2TopTrack); |
| 39 | + |
| 40 | + String moreFollowers = band1Stats.getInt("followers") > band2Stats.getInt("followers") ? band1 : band2; |
| 41 | + String morePopularTrack = band1TopTrack.getInt("playCount") > band2TopTrack.getInt("playCount") ? band1 : band2; |
| 42 | + |
| 43 | + System.out.println("\nLa banda con más seguidores es: " + moreFollowers); |
| 44 | + System.out.println("La banda con la canción más popular es: " + morePopularTrack); |
| 45 | + } |
| 46 | + |
| 47 | + private static String getAccessToken() throws Exception { |
| 48 | + String auth = CLIENT_ID + ":" + CLIENT_SECRET; |
| 49 | + String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes()); |
| 50 | + |
| 51 | + URL url = new URL("https://accounts.spotify.com/api/token"); |
| 52 | + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 53 | + conn.setRequestMethod("POST"); |
| 54 | + conn.setRequestProperty("Authorization", "Basic " + encodedAuth); |
| 55 | + conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); |
| 56 | + conn.setDoOutput(true); |
| 57 | + |
| 58 | + String data = "grant_type=client_credentials"; |
| 59 | + conn.getOutputStream().write(data.getBytes()); |
| 60 | + |
| 61 | + BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); |
| 62 | + String inputLine; |
| 63 | + StringBuilder response = new StringBuilder(); |
| 64 | + |
| 65 | + while ((inputLine = in.readLine()) != null) { |
| 66 | + response.append(inputLine); |
| 67 | + } |
| 68 | + in.close(); |
| 69 | + |
| 70 | + JSONObject json = new JSONObject(response.toString()); |
| 71 | + return json.getString("access_token"); |
| 72 | + } |
| 73 | + |
| 74 | + private static JSONObject getArtistData(String token, String artistName) throws Exception { |
| 75 | + URL url = new URL("https://api.spotify.com/v1/search?q=" + artistName + "&type=artist"); |
| 76 | + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 77 | + conn.setRequestMethod("GET"); |
| 78 | + conn.setRequestProperty("Authorization", "Bearer " + token); |
| 79 | + |
| 80 | + BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); |
| 81 | + String inputLine; |
| 82 | + StringBuilder response = new StringBuilder(); |
| 83 | + |
| 84 | + while ((inputLine = in.readLine()) != null) { |
| 85 | + response.append(inputLine); |
| 86 | + } |
| 87 | + in.close(); |
| 88 | + |
| 89 | + JSONObject json = new JSONObject(response.toString()); |
| 90 | + JSONArray items = json.getJSONObject("artists").getJSONArray("items"); |
| 91 | + return items.getJSONObject(0); |
| 92 | + } |
| 93 | + |
| 94 | + private static JSONObject getArtistStats(String artistId, String token) throws Exception { |
| 95 | + URL url = new URL("https://api.spotify.com/v1/artists/" + artistId); |
| 96 | + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 97 | + conn.setRequestMethod("GET"); |
| 98 | + conn.setRequestProperty("Authorization", "Bearer " + token); |
| 99 | + |
| 100 | + BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); |
| 101 | + String inputLine; |
| 102 | + StringBuilder response = new StringBuilder(); |
| 103 | + |
| 104 | + while ((inputLine = in.readLine()) != null) { |
| 105 | + response.append(inputLine); |
| 106 | + } |
| 107 | + in.close(); |
| 108 | + |
| 109 | + return new JSONObject(response.toString()); |
| 110 | + } |
| 111 | + |
| 112 | + private static JSONObject getArtistTopTrack(String artistId, String token) throws Exception { |
| 113 | + URL url = new URL("https://api.spotify.com/v1/artists/" + artistId + "/top-tracks?market=US"); |
| 114 | + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
| 115 | + conn.setRequestMethod("GET"); |
| 116 | + conn.setRequestProperty("Authorization", "Bearer " + token); |
| 117 | + |
| 118 | + BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); |
| 119 | + String inputLine; |
| 120 | + StringBuilder response = new StringBuilder(); |
| 121 | + |
| 122 | + while ((inputLine = in.readLine()) != null) { |
| 123 | + response.append(inputLine); |
| 124 | + } |
| 125 | + in.close(); |
| 126 | + |
| 127 | + JSONObject json = new JSONObject(response.toString()); |
| 128 | + JSONArray tracks = json.getJSONArray("tracks"); |
| 129 | + return tracks.getJSONObject(0); |
| 130 | + } |
| 131 | +} |
0 commit comments