|
| 1 | +""" |
| 2 | +* EJERCICIO: |
| 3 | + * Utilizando un mecanismo de peticiones HTTP de tu lenguaje, realiza |
| 4 | + * una petición a la web que tú quieras, verifica que dicha petición |
| 5 | + * fue exitosa y muestra por consola el contenido de la web. |
| 6 | +""" |
| 7 | + |
| 8 | +from requests import get |
| 9 | +from time import sleep |
| 10 | + |
| 11 | +url = "https://retosdeprogramacion.com" |
| 12 | +response = get(url) |
| 13 | +#print(response.content.decode("utf-8").replace(">",">\n")) se podría dar formato a los bytes que recoge la llamada |
| 14 | +print(response.content) |
| 15 | + |
| 16 | +""" |
| 17 | +* DIFICULTAD EXTRA (opcional): |
| 18 | + * Utilizando la PokéAPI (https://pokeapi.co), crea un programa por |
| 19 | + * terminal al que le puedas solicitar información de un Pokémon concreto |
| 20 | + * utilizando su nombre o número. |
| 21 | + * - Muestra el nombre, id, peso, altura y tipo(s) del Pokémon |
| 22 | + * - Muestra el nombre de su cadena de evoluciones |
| 23 | + * - Muestra los juegos en los que aparece |
| 24 | + * - Controla posibles errores |
| 25 | +""" |
| 26 | + |
| 27 | +def is_there_evolutions(json): #busca las evoluciones de manera recursiva y las almacena en una lista |
| 28 | + evolutions = list() |
| 29 | + if len(json["evolves_to"])!= 0: |
| 30 | + evolutions.append(json["species"]["name"]) |
| 31 | + evolutions.extend(is_there_evolutions(json["evolves_to"][0])) |
| 32 | + else: |
| 33 | + evolutions.append(json["species"]["name"]) |
| 34 | + return evolutions |
| 35 | + |
| 36 | +def find_evolution(url): |
| 37 | + evolutions = list() |
| 38 | + response = get(url) |
| 39 | + response_json = response.json() |
| 40 | + evolutions = is_there_evolutions(response_json["chain"]) |
| 41 | + return evolutions |
| 42 | + |
| 43 | +def find_pokemon(response): |
| 44 | + response_json = response.json() |
| 45 | + response_species = get(response_json["species"]["url"]) |
| 46 | + response_species_json = response_species.json() |
| 47 | + response_evolution_url = response_species_json["evolution_chain"]["url"] |
| 48 | + evolutions = find_evolution(response_evolution_url) |
| 49 | + name = response_json["forms"][0]["name"] |
| 50 | + id = response_json["id"] |
| 51 | + weight = response_json["weight"] |
| 52 | + height = response_json["height"] |
| 53 | + types = list() |
| 54 | + for element in response_json["types"]: |
| 55 | + types.append(element["type"]["name"]) |
| 56 | + games = list() |
| 57 | + for element in response_json["game_indices"]: |
| 58 | + games.append(element["version"]["name"]) |
| 59 | + return name,id,height,weight,types,evolutions,games |
| 60 | + |
| 61 | +def make_http_request(data): |
| 62 | + url = f"https://pokeapi.co/api/v2/pokemon/{data}" |
| 63 | + response = get(url) |
| 64 | + return response |
| 65 | + |
| 66 | +def pokemon(): |
| 67 | + print("\nTE DOY LA BIENVENIDA AL SISTEMA DE BÚSQUEDA DE POKEMONS - basado en la PokeAPI®") |
| 68 | + while True: |
| 69 | + data = input("\nDime el nombre del Pokemon que buscas o su número: ") |
| 70 | + try: |
| 71 | + pokemon = int(data) |
| 72 | + except ValueError: |
| 73 | + pokemon = data |
| 74 | + response = make_http_request(pokemon) |
| 75 | + if response.status_code == 200: |
| 76 | + name,id,height,weight,types,evolutions,games = find_pokemon(response) |
| 77 | + print(f"\nNombre: {name.title()}") |
| 78 | + print(f"Número: {id}") |
| 79 | + print(f"Altura: {float(height/10)} m") |
| 80 | + print(f"Peso: {float(weight/10)} kg\n") |
| 81 | + print("Elementos:") |
| 82 | + for type in types: |
| 83 | + print(f" - {type.title()}") |
| 84 | + print("\nCadena de Evolución: ") |
| 85 | + for evolution in evolutions: |
| 86 | + print(f" - {evolution.title()}") |
| 87 | + print(f"\nJuegos en los que aparece el pokemon {name.title()}") |
| 88 | + if len(games) == 0: |
| 89 | + print(f"Vaya parece que no tenemos registrado ningún juego en el que aparezca {name.title()}") |
| 90 | + else: |
| 91 | + for game in games: |
| 92 | + print(f" - {game.title()}") |
| 93 | + else: |
| 94 | + print("No hay resultados para tu búsqueda") |
| 95 | + sleep(1) |
| 96 | + print("\n") |
| 97 | + option = input("¿Quieres buscar más pokemon?\n - Si(S)\n - No(N)\n---> ").lower() |
| 98 | + if option == "n": |
| 99 | + print("Gracias por usar este sistema de búsqueda. Hasta Pronto") |
| 100 | + break |
| 101 | + |
| 102 | +pokemon() |
0 commit comments