|
| 1 | +import java.io.IOException; |
| 2 | +import java.net.URI; |
| 3 | +import java.net.http.HttpClient; |
| 4 | +import java.net.http.HttpRequest; |
| 5 | +import java.net.http.HttpResponse; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.Comparator; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import com.fasterxml.jackson.databind.JsonNode; |
| 12 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 13 | + |
| 14 | +public class MohamedElderkaoui { |
| 15 | + |
| 16 | + private static final String CLIENT_ID = System.getenv("TWITCH_CLIENT_ID"); // Reemplaza con tu Client ID |
| 17 | + private static final String CLIENT_SECRET = System.getenv("TWITCH_CLIENT_SECRET"); // Reemplaza con tu Client Secret |
| 18 | + private static final String TOKEN_URL = "https://id.twitch.tv/oauth2/token"; |
| 19 | + private static final String TWITCH_API_URL = "https://api.twitch.tv/helix/users"; |
| 20 | + private static String accessToken = ""; |
| 21 | + |
| 22 | + public static void main(String[] args) throws IOException, InterruptedException { |
| 23 | + // Lista de participantes |
| 24 | + List<String> participants = Arrays.asList( |
| 25 | + "ABBY", "ACHE", "ADRI CONTRERAS", "AGUSTIN", "ALEXBY", "AMPETER", "ANDER", "ARI GAMEPLAYS", |
| 26 | + "ARIGELI", "AURONPLAY", "AXOZER", "BENIJU", "BY CALITOS", "BYVIRUZZ", "CARRERA", "CELOPAN", |
| 27 | + "CHEETO", "CRYSTALMOLLY", "DARIO EMEHACHE", "DHEYLO", "DJMARIO", "DOBLE", "ELVISA", "ELYAS360", |
| 28 | + "FOLAGOR", "GREFG", "GUANYAR", "HIKA", "HIPER", "IBAI", "IBELKY", "ILLOJUAN", "IMANTADO", |
| 29 | + "IRINA ISSAIA", "JESSKIU", "JOPA", "JORDIWILD", "KENAI SOUZA", "KERORO", "KIDD KEO", "KIKO RIVERA", |
| 30 | + "KNEKRO", "KOKO", "KRONNOZOMBER", "LEVIATHAN", "LIT KILLAH", "LOLA LOLITA", "LOLITO", "LUH", "LUZU", |
| 31 | + "MANGEL", "MAYICHI", "MELO", "MISSASINOFIA", "MIXWELL", "MR.JAGGER", "NATE GENTILE", "NEXXUZ", |
| 32 | + "NIA", "NIL OJEDA", "NISSAXTER", "OLLIE", "ORSLOK", "OUTCONSUMER", "PAPI GAVI", "PARCETAMOR", |
| 33 | + "PATICA", "PAULA GONU", "PAUSENPAII", "PERXITAA", "PLEX", "POLISPOL", "QUACKITY", "RECUERDOP", |
| 34 | + "REVEN", "RIVERS", "ROBERTPG", "ROIER", "ROJUU", "RUBIUS", "SHADOUNE", "SILITHUR", "SPOKSPONHA", |
| 35 | + "SPREEN", "SPURSITO", "STAXX", "SUZYROXX", "VICENS", "VITUBER", "WERLYB", "XAVI", "XCRY", "XOKAS", |
| 36 | + "ZARCORT", "ZELING", "ZORMAN" |
| 37 | + ); |
| 38 | + |
| 39 | + // Obtener el token de acceso |
| 40 | + obtainAccessToken(); |
| 41 | + |
| 42 | + List<TwitchUser> users = new ArrayList<>(); |
| 43 | + |
| 44 | + for (String username : participants) { |
| 45 | + TwitchUser user = fetchTwitchUserInfo(username); |
| 46 | + if (user != null) { |
| 47 | + users.add(user); |
| 48 | + } else { |
| 49 | + System.out.println("El usuario " + username + " no tiene cuenta en Twitch."); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + // Ordenar por número de seguidores |
| 54 | + users.sort(Comparator.comparingInt(TwitchUser::getFollowers).reversed()); |
| 55 | + System.out.println("Ranking por número de seguidores:"); |
| 56 | + users.forEach(System.out::println); |
| 57 | + |
| 58 | + // Ordenar por antigüedad (fecha de creación) |
| 59 | + users.sort(Comparator.comparing(TwitchUser::getCreatedAt)); |
| 60 | + System.out.println("\nRanking por antigüedad:"); |
| 61 | + users.forEach(System.out::println); |
| 62 | + } |
| 63 | + |
| 64 | + // Obtener el token de acceso para la API de Twitch |
| 65 | + private static void obtainAccessToken() throws IOException, InterruptedException { |
| 66 | + HttpClient client = HttpClient.newHttpClient(); |
| 67 | + HttpRequest request = HttpRequest.newBuilder() |
| 68 | + .uri(URI.create(TOKEN_URL + "?client_id=" + CLIENT_ID + "&client_secret=" + CLIENT_SECRET + "&grant_type=client_credentials")) |
| 69 | + .POST(HttpRequest.BodyPublishers.noBody()) |
| 70 | + .header("Content-Type", "application/x-www-form-urlencoded") |
| 71 | + .build(); |
| 72 | + |
| 73 | + HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 74 | + ObjectMapper mapper = new ObjectMapper(); |
| 75 | + JsonNode jsonResponse = mapper.readTree(response.body()); |
| 76 | + accessToken = jsonResponse.get("access_token").asText(); |
| 77 | + } |
| 78 | + |
| 79 | + // Obtener información del usuario de Twitch |
| 80 | + private static TwitchUser fetchTwitchUserInfo(String username) throws IOException, InterruptedException { |
| 81 | + HttpClient client = HttpClient.newHttpClient(); |
| 82 | + HttpRequest request = HttpRequest.newBuilder() |
| 83 | + .uri(URI.create(TWITCH_API_URL + "?login=" + username)) |
| 84 | + .header("Authorization", "Bearer " + accessToken) |
| 85 | + .header("Client-Id", CLIENT_ID) |
| 86 | + .build(); |
| 87 | + |
| 88 | + HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 89 | + |
| 90 | + if (response.statusCode() == 200) { |
| 91 | + ObjectMapper mapper = new ObjectMapper(); |
| 92 | + JsonNode jsonResponse = mapper.readTree(response.body()); |
| 93 | + JsonNode userData = jsonResponse.get("data").get(0); |
| 94 | + |
| 95 | + if (userData != null) { |
| 96 | + String displayName = userData.get("display_name").asText(); |
| 97 | + int followers = fetchFollowersCount(userData.get("id").asText()); |
| 98 | + String createdAt = userData.get("created_at").asText(); |
| 99 | + |
| 100 | + return new TwitchUser(displayName, followers, createdAt); |
| 101 | + } |
| 102 | + } |
| 103 | + return null; // Usuario no encontrado |
| 104 | + } |
| 105 | + |
| 106 | + // Obtener número de seguidores del usuario |
| 107 | + private static int fetchFollowersCount(String userId) throws IOException, InterruptedException { |
| 108 | + HttpClient client = HttpClient.newHttpClient(); |
| 109 | + HttpRequest request = HttpRequest.newBuilder() |
| 110 | + .uri(URI.create(TWITCH_API_URL + "/follows?to_id=" + userId)) |
| 111 | + .header("Authorization", "Bearer " + accessToken) |
| 112 | + .header("Client-Id", CLIENT_ID) |
| 113 | + .build(); |
| 114 | + |
| 115 | + HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 116 | + |
| 117 | + if (response.statusCode() == 200) { |
| 118 | + ObjectMapper mapper = new ObjectMapper(); |
| 119 | + JsonNode jsonResponse = mapper.readTree(response.body()); |
| 120 | + return jsonResponse.get("total").asInt(); |
| 121 | + } |
| 122 | + return 0; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// Clase que representa a un usuario de Twitch |
| 127 | +class TwitchUser { |
| 128 | + private String username; |
| 129 | + private int followers; |
| 130 | + private String createdAt; |
| 131 | + |
| 132 | + public TwitchUser(String username, int followers, String createdAt) { |
| 133 | + this.username = username; |
| 134 | + this.followers = followers; |
| 135 | + this.createdAt = createdAt; |
| 136 | + } |
| 137 | + |
| 138 | + public String getUsername() { |
| 139 | + return username; |
| 140 | + } |
| 141 | + |
| 142 | + public int getFollowers() { |
| 143 | + return followers; |
| 144 | + } |
| 145 | + |
| 146 | + public String getCreatedAt() { |
| 147 | + return createdAt; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public String toString() { |
| 152 | + return "Usuario: " + username + ", Seguidores: " + followers + ", Fecha de creación: " + createdAt; |
| 153 | + } |
| 154 | +} |
0 commit comments