Skip to content

Commit bbb5edc

Browse files
authored
Merge pull request mouredev#6288 from raynerpv2022/main
#20 Python and Go
2 parents 35ff2c1 + 4048baf commit bbb5edc

File tree

2 files changed

+325
-0
lines changed

2 files changed

+325
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
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+
* DIFICULTAD EXTRA (opcional):
8+
* Utilizando la PokéAPI (https://pokeapi.co), crea un programa por
9+
* terminal al que le puedas solicitar información de un Pokémon concreto
10+
* utilizando su nombre o número.
11+
* - Muestra el nombre, id, peso, altura y tipo(s) del Pokémon
12+
* - Muestra el nombre de su cadena de evoluciones
13+
* - Muestra los juegos en los que aparece
14+
* - Controla posibles errores
15+
*/
16+
17+
package main
18+
19+
import (
20+
"encoding/json"
21+
"fmt"
22+
"io"
23+
24+
"net/http"
25+
)
26+
27+
func exercice(url string) {
28+
resp, err := http.Get(url)
29+
30+
if err != nil {
31+
32+
fmt.Printf("Error readinng web site %v\n", err)
33+
return
34+
}
35+
defer resp.Body.Close()
36+
data, err := io.ReadAll(resp.Body)
37+
38+
if err != nil {
39+
fmt.Printf("Error readinng data %v", err)
40+
}
41+
if resp.StatusCode == 200 {
42+
fmt.Println("Conexion Exitosa. Code :", resp.StatusCode)
43+
}
44+
fmt.Println(string(data))
45+
46+
}
47+
48+
func getPokeUrl(url string) *http.Response {
49+
response, e := http.Get(url)
50+
51+
if e != nil {
52+
53+
fmt.Printf("Error de Red %v \n", e)
54+
return nil
55+
}
56+
57+
if response.StatusCode == http.StatusNotFound {
58+
fmt.Println("Pokemmon no encontrado ")
59+
return nil
60+
}
61+
62+
return response
63+
}
64+
65+
func extra() {
66+
67+
var PokemonName string
68+
fmt.Println("Nombre o NUmero del Pokemon ")
69+
fmt.Scanln(&PokemonName)
70+
url := fmt.Sprintf("https://pokeapi.co/api/v2/pokemon/%v", PokemonName)
71+
fmt.Println(url)
72+
73+
response := getPokeUrl(url)
74+
if response == nil {
75+
return
76+
}
77+
// response, e := http.Get(url)
78+
79+
// if e != nil {
80+
81+
// fmt.Printf("Error de Red %v \n", e)
82+
// return
83+
// }
84+
85+
// if response.StatusCode == http.StatusNotFound {
86+
// fmt.Println("Pokemmon no encontrado ")
87+
// return
88+
// }
89+
90+
defer response.Body.Close()
91+
92+
data, e := io.ReadAll(response.Body)
93+
if e != nil {
94+
fmt.Printf("Error reading Data %v \n", e)
95+
return
96+
}
97+
98+
var poke Pokemon
99+
e = json.Unmarshal(data, &poke)
100+
if e != nil {
101+
fmt.Printf("Error json data %v\n", e)
102+
return
103+
}
104+
105+
fmt.Printf("Name : %v\t", poke.Name)
106+
fmt.Printf("ID : %v\t", poke.Id)
107+
fmt.Printf("Weight : %v\t", poke.Weight)
108+
fmt.Printf("Height : %v\t\n", poke.Height)
109+
110+
fmt.Println("tIpOs")
111+
112+
for _, v := range poke.Types {
113+
fmt.Printf(" \t Name %v URL %v \n", v.Type.Name, v.Type.Url)
114+
115+
}
116+
fmt.Println("Games")
117+
for _, v := range poke.GameIndices {
118+
fmt.Printf(" \t Index %v Name %v URL %v \n", v.GameIndex, v.Version.Name, v.Version.Url)
119+
}
120+
121+
specie_url := fmt.Sprintf("https://pokeapi.co/api/v2/pokemon-species//%v", PokemonName)
122+
123+
Respspecie := getPokeUrl(specie_url)
124+
125+
if Respspecie == nil {
126+
127+
return
128+
}
129+
130+
defer Respspecie.Body.Close()
131+
132+
data, e = io.ReadAll(Respspecie.Body)
133+
134+
if e != nil {
135+
fmt.Printf("Error reading Data %v \n", e)
136+
return
137+
}
138+
139+
var evolutionChain EvolutionChainUrl
140+
e = json.Unmarshal(data, &evolutionChain)
141+
if e != nil {
142+
fmt.Printf("Error json data %v\n", e)
143+
return
144+
}
145+
146+
fmt.Println("URL", evolutionChain.Evolution_chain.Url)
147+
148+
evolution_chain := getPokeUrl(evolutionChain.Evolution_chain.Url)
149+
if evolution_chain == nil {
150+
151+
return
152+
}
153+
defer evolution_chain.Body.Close()
154+
155+
var evolution_to eEvo
156+
respEvolutio_to, _ := io.ReadAll(evolution_chain.Body)
157+
_ = json.Unmarshal(respEvolutio_to, &evolution_to)
158+
159+
fmt.Println("EStado Original : ", evolution_to.Chain.Species.Name)
160+
fmt.Println("Cadena de Evolution: ")
161+
printEvo(evolution_to.Chain)
162+
163+
}
164+
165+
func printEvo(dd Ggg) {
166+
167+
for _, evo := range dd.Evolve_to {
168+
printEvo(evo)
169+
fmt.Println(evo.Species.Name)
170+
171+
}
172+
173+
}
174+
175+
func GetPokemon(url string) *http.Response {
176+
response, e := http.Get(url)
177+
178+
if e != nil {
179+
180+
fmt.Printf("Error de Red %v \n", e)
181+
return nil
182+
}
183+
184+
if response.StatusCode == http.StatusNotFound {
185+
fmt.Println("Pokemmon no encontrado ")
186+
return nil
187+
}
188+
189+
defer response.Body.Close()
190+
return response
191+
192+
}
193+
194+
type eEvo struct {
195+
Chain Ggg `json:"chain"`
196+
}
197+
198+
type Ggg struct {
199+
Evolve_to []Ggg `json:"evolves_to"`
200+
201+
Species struct {
202+
Name string `json:"name"`
203+
} `json:"species"`
204+
}
205+
206+
type EvolutionChainUrl struct {
207+
Evolution_chain struct {
208+
Url string `json:"url"`
209+
} `json:"evolution_chain"`
210+
}
211+
212+
type Pokemon struct {
213+
Id int `json:"id"`
214+
Name string `json:"name"`
215+
Weight int `json:"weight"`
216+
Height int `json:"height"`
217+
218+
GameIndices []struct {
219+
GameIndex int `json:"game_index"`
220+
Version struct {
221+
Name string `json:"name"`
222+
Url string `json:"url"`
223+
} `json:"version"`
224+
} `json:"game_indices"`
225+
226+
Types []struct {
227+
Slot int `json:"slot"`
228+
Type struct {
229+
Name string `json:"name"`
230+
Url string `json:"url"`
231+
} `json:"type"`
232+
} `json:"types"`
233+
}
234+
235+
func main() {
236+
// exercice("https://mouredev.pro")
237+
extra()
238+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
# * DIFICULTAD EXTRA (opcional):
8+
# * Utilizando la PokéAPI (https://pokeapi.co), crea un programa por
9+
# * terminal al que le puedas solicitar información de un Pokémon concreto
10+
# * utilizando su nombre o número.
11+
# * - Muestra el nombre, id, peso, altura y tipo(s) del Pokémon
12+
# * - Muestra el nombre de su cadena de evoluciones
13+
# * - Muestra los juegos en los que aparece
14+
# * - Controla posibles errores
15+
# */
16+
17+
import requests
18+
19+
20+
def execrcice(url: str):
21+
try:
22+
response = requests.get(url)
23+
response.raise_for_status()
24+
except requests.exceptions.RequestException as e:
25+
26+
print("Error Conexion Lost Code :", e)
27+
28+
else :
29+
print("Conexion Succesfully, Code :", response.status_code)
30+
print("Data")
31+
print()
32+
print(response.text)
33+
34+
def extra():
35+
value = input("Numero o Nombredel pOkEmOn:")
36+
url = f"https://pokeapi.co/api/v2/pokemon/{value}"
37+
38+
poke = get_Poke_url(url)
39+
if not poke :
40+
return
41+
42+
print(f' Nombre {poke["name"]}' )
43+
print(f' ID {poke["id"]}' )
44+
print(f' Weight {poke["weight"]}' )
45+
print(f' Height {poke["height"]}' )
46+
print("Tipos ")
47+
for t in poke["types"] :
48+
print(f"\tNOmbre: {t['type']['name']} URL: {t['type']['url']}")
49+
print()
50+
51+
print("GAmes ")
52+
for g in poke["game_indices"]:
53+
print(f" \t {g['version']['name']} *** url: {g['version']['url']}")
54+
55+
56+
specie_url = f"https://pokeapi.co/api/v2/pokemon-species/{value}"
57+
specie_json = get_Poke_url(specie_url)
58+
if not specie_json:
59+
return
60+
61+
evolution_chain_json = get_Poke_url(specie_json["evolution_chain"]["url"])
62+
if not evolution_chain_json:
63+
return
64+
65+
print("EVOLUTION CHAIN")
66+
get_evolution(evolution_chain_json["chain"])
67+
68+
def get_evolution(evolution):
69+
70+
if "evolves_to" in evolution:
71+
for i in evolution["evolves_to"]:
72+
73+
get_evolution(i)
74+
print(f'\t {evolution["species"]["name"]}')
75+
76+
def get_Poke_url(url):
77+
response = requests.get(url)
78+
status_code = response.status_code
79+
if status_code >= 300 or status_code < 200:
80+
print(f"Pokemon No encontrado, Error : {status_code}")
81+
return False
82+
return response.json()
83+
84+
85+
86+
# execrcice("https://mouredev.pro/")
87+
extra()

0 commit comments

Comments
 (0)