Skip to content

Commit 43055d7

Browse files
author
raulG91
committed
#20 - Python
1 parent adfc34a commit 43055d7

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import requests
2+
3+
url = "https://httpbin.org/get"
4+
5+
r = requests.get(url)
6+
7+
8+
if r.status_code == 200:
9+
print("Get executed")
10+
print(r.json())
11+
elif r.status_code == 401:
12+
print("Unauthorized")
13+
elif r.status_code == 403:
14+
print("Forbidden")
15+
else:
16+
print(f'Error executing get request with status code {r.status_code}')
17+
18+
#Extra
19+
20+
uri = "https://pokeapi.co/api/v2/"
21+
22+
while(True):
23+
print("Enter 1 to search pokemon by name or ID")
24+
print("Enter 2 to exit")
25+
value = int(input())
26+
27+
if value == 1:
28+
pokemon = input("Enter name or id for pokemon ")
29+
pokemon = pokemon.lower()
30+
url_final = uri +"pokemon/"+pokemon
31+
32+
request = requests.get(url_final)
33+
34+
if request.status_code == 200 or request.status_code == 201:
35+
36+
#Get JSON data
37+
data = request.json()
38+
#Extract data from the response
39+
name = data["name"]
40+
id = data["id"]
41+
weight = data["weight"]
42+
height = data["height"]
43+
#Get type
44+
pokemon_type = []
45+
for type in data["types"]:
46+
pokemon_type.append(type["type"]["name"])
47+
#Get game list
48+
game_list = []
49+
for game in data["game_indices"]:
50+
game_list.append(game["version"]["name"])
51+
52+
#Get evolution list
53+
54+
#First get speies URL
55+
species_url = data["species"]["url"]
56+
request_evo = requests.get(species_url)
57+
if request_evo.status_code == 200 or request_evo.status_code == 201:
58+
data_evo = request_evo.json()
59+
#if Success get URL for evolution chain
60+
evo_chain = data_evo["evolution_chain"]["url"]
61+
#Now we have URL to get evolutions
62+
request_evo = requests.get(evo_chain)
63+
if request_evo.status_code == 200 or request_evo.status_code == 201:
64+
evo_details = request_evo.json()
65+
evolution_chain = []
66+
current_evo = evo_details["chain"]
67+
#Iterate over evolutions
68+
while current_evo:
69+
evolution_chain.append(current_evo["species"]["name"])
70+
if current_evo['evolves_to']:
71+
#If there are more evolutions, check it
72+
current_evo = current_evo['evolves_to'][0]
73+
else:
74+
current_evo = None
75+
else:
76+
print(f'Error executing {evo_chain} with status code {request_evo.status_code}')
77+
else:
78+
print(f'Error executing {species_url} with status code {request_evo.status_code}')
79+
80+
print(f'Name: {name}, id: {id}, Weight: {weight}, Height: {height}, Pokemon type: {pokemon_type}, Evoloves: {evolution_chain},Game list: {game_list}')
81+
else:
82+
print(f'Error executing API status code: {request.status_code}')
83+
elif value == 2:
84+
break
85+
86+

0 commit comments

Comments
 (0)