|
| 1 | +import com.google.gson.*; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.net.HttpURLConnection; |
| 7 | +import java.net.URI; |
| 8 | +import java.net.URISyntaxException; |
| 9 | +import java.net.URL; |
| 10 | +import java.util.*; |
| 11 | +import java.util.stream.Collectors; |
| 12 | +import java.util.stream.StreamSupport; |
| 13 | + |
| 14 | +/** |
| 15 | + * #20 PETICIONES HTTP |
| 16 | + * <dependency> |
| 17 | + * <groupId>com.google.code.gson</groupId> |
| 18 | + * <artifactId>gson</artifactId> |
| 19 | + * <version>2.10.1</version> |
| 20 | + * </dependency> |
| 21 | + * |
| 22 | + * @author martinbohorquez |
| 23 | + */ |
| 24 | +public class martinbohorquez { |
| 25 | + public static void main(String[] args) throws IOException, URISyntaxException { |
| 26 | + getHttpResponse("https://www.moure.dev/"); |
| 27 | + |
| 28 | + /* |
| 29 | + * DIFICULTAD EXTRA |
| 30 | + */ |
| 31 | + System.out.print("Introduce un nombre o número de Pokémon a buscar: "); |
| 32 | + Scanner sc = new Scanner(System.in); |
| 33 | + String pokemon = sc.nextLine().toLowerCase(); |
| 34 | + |
| 35 | + pokemonInfo(pokemon); |
| 36 | + } |
| 37 | + |
| 38 | + private static HttpURLConnection getResponse(String url) throws URISyntaxException, IOException { |
| 39 | + HttpURLConnection response = null; |
| 40 | + int responseCode; |
| 41 | + |
| 42 | + while (true) { |
| 43 | + URL obj = (new URI(url)).toURL(); |
| 44 | + response = (HttpURLConnection) obj.openConnection(); |
| 45 | + response.setRequestMethod("GET"); |
| 46 | + response.setConnectTimeout(5000); |
| 47 | + response.setReadTimeout(5000); |
| 48 | + |
| 49 | + responseCode = response.getResponseCode(); |
| 50 | + |
| 51 | + if (responseCode >= 300 && responseCode < 400) |
| 52 | + System.out.printf("Redirecting to: %s%n", url = response.getHeaderField("Location")); |
| 53 | + else break; |
| 54 | + } |
| 55 | + return response; |
| 56 | + } |
| 57 | + |
| 58 | + private static void getHttpResponse(String url) throws IOException, URISyntaxException { |
| 59 | + HttpURLConnection response = getResponse(url); |
| 60 | + int responseCode = response.getResponseCode(); |
| 61 | + |
| 62 | + if (responseCode >= 200 && responseCode < 300) |
| 63 | + try (BufferedReader in = new BufferedReader(new InputStreamReader(response.getInputStream()))) { |
| 64 | + System.out.println(in.lines().collect(Collectors.joining())); |
| 65 | + } |
| 66 | + else System.out.printf("Error response código %d al realizar la petición", response.getResponseCode()); |
| 67 | + } |
| 68 | + |
| 69 | + private static void pokemonInfo(String pokemon) throws URISyntaxException, IOException { |
| 70 | + HttpURLConnection response = getResponse("https://pokeapi.co/api/v2/pokemon/" + pokemon); |
| 71 | + int responseCode = response.getResponseCode(); |
| 72 | + if (responseCode >= 200 && responseCode < 300) { |
| 73 | + try (BufferedReader in = new BufferedReader(new InputStreamReader(response.getInputStream()))) { |
| 74 | + String jsonString = in.lines().collect(Collectors.joining()); |
| 75 | + Gson gson = new Gson(); |
| 76 | + JsonObject jsonObject = gson.fromJson(jsonString, JsonObject.class); |
| 77 | + |
| 78 | + System.out.printf("Nombre: %s%n", jsonObject.get("name").getAsString()); |
| 79 | + System.out.printf("ID: %s%n", jsonObject.get("id").getAsString()); |
| 80 | + System.out.printf("Peso: %s%n", jsonObject.get("weight").getAsString()); |
| 81 | + System.out.printf("Altura: %s%n", jsonObject.get("height").getAsString()); |
| 82 | + |
| 83 | + List<String> types = StreamSupport.stream(jsonObject.getAsJsonArray("types").spliterator(), false) |
| 84 | + .map(JsonElement::getAsJsonObject) |
| 85 | + .map(typeObject -> typeObject.getAsJsonObject("type").get("name").getAsString()) |
| 86 | + .toList(); |
| 87 | + System.out.printf("Tipo(s): %s%n", types); |
| 88 | + evolutionChain(pokemon); |
| 89 | + List<String> juegos = StreamSupport.stream(jsonObject.getAsJsonArray("game_indices").spliterator(), false) |
| 90 | + .map(JsonElement::getAsJsonObject) |
| 91 | + .map(typeObject -> typeObject.getAsJsonObject("version").get("name").getAsString()) |
| 92 | + .toList(); |
| 93 | + System.out.printf("Juegos: %s%n", juegos); |
| 94 | + |
| 95 | + } |
| 96 | + } else System.out.printf("Error %d: Pokémon no encontrado!", response.getResponseCode()); |
| 97 | + } |
| 98 | + |
| 99 | + private static void evolutionChain(String pokemon) throws URISyntaxException, IOException { |
| 100 | + HttpURLConnection response = getResponse("https://pokeapi.co/api/v2/pokemon-species/" + pokemon); |
| 101 | + int responseCode = response.getResponseCode(); |
| 102 | + if (responseCode >= 200 && responseCode < 300) { |
| 103 | + try (BufferedReader in = new BufferedReader(new InputStreamReader(response.getInputStream()))) { |
| 104 | + String jsonString = in.lines().collect(Collectors.joining()); |
| 105 | + Gson gson = new Gson(); |
| 106 | + JsonObject jsonObject = gson.fromJson(jsonString, JsonObject.class); |
| 107 | + |
| 108 | + String url = jsonObject.get("evolution_chain").getAsJsonObject().get("url").getAsString(); |
| 109 | + response = getResponse(url); |
| 110 | + responseCode = response.getResponseCode(); |
| 111 | + if (responseCode >= 200 && responseCode < 300) { |
| 112 | + try (BufferedReader inS = new BufferedReader(new InputStreamReader(response.getInputStream()))) { |
| 113 | + jsonString = inS.lines().collect(Collectors.joining()); |
| 114 | + gson = new Gson(); |
| 115 | + jsonObject = gson.fromJson(jsonString, JsonObject.class); |
| 116 | + |
| 117 | + StringBuilder evolutionChain = new StringBuilder(); |
| 118 | + JsonObject chainObject = jsonObject.getAsJsonObject("chain"); |
| 119 | + |
| 120 | + System.out.println("Cadena(s) de evolución:"); |
| 121 | + Map<Integer, String> evolutionMap = new LinkedHashMap<>(); |
| 122 | + getEvolveTo(evolutionMap, evolutionChain, chainObject).values().forEach(System.out::println); |
| 123 | + } |
| 124 | + } else System.out.printf("Error %d: obteniendo evoluciones!", response.getResponseCode()); |
| 125 | + } |
| 126 | + } else System.out.printf("Error %d: obteniendo evoluciones!", response.getResponseCode()); |
| 127 | + } |
| 128 | + |
| 129 | + private static Map<Integer, String> getEvolveTo(Map<Integer, String> evolutionMap, StringBuilder evolutionChain, JsonObject chainObject) { |
| 130 | + Set<String> evolutionSet = new LinkedHashSet<>(); |
| 131 | + JsonElement element = chainObject.getAsJsonObject("species").get("name"); |
| 132 | + evolutionChain.append(element); |
| 133 | + |
| 134 | + JsonArray evolutions = chainObject.getAsJsonArray("evolves_to"); |
| 135 | + for (int i = 0; i < evolutions.size(); i++) { |
| 136 | + if (i != 0) { |
| 137 | + int indexIni = evolutionChain.reverse().indexOf(" >- "); |
| 138 | + int length = evolutionChain.reverse().length(); |
| 139 | + evolutionChain.delete(length - indexIni - 4, length); |
| 140 | + evolutionSet = new LinkedHashSet<>(); |
| 141 | + } |
| 142 | + evolutionChain.append(" -> "); |
| 143 | + getEvolveTo(evolutionMap, evolutionChain, evolutions.get(i).getAsJsonObject()); |
| 144 | + evolutionSet.add(evolutionChain.toString()); |
| 145 | + if (!evolutionMap.containsValue(evolutionSet.toString())) evolutionMap.put(i, evolutionSet.toString()); |
| 146 | + } |
| 147 | + return evolutionMap; |
| 148 | + } |
| 149 | +} |
0 commit comments