-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
131 lines (57 loc) · 2.44 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from flask import Flask, request, render_template
import requests
app = Flask(__name__)
@app.route('/pokemon_name', methods=['GET', 'POST'])
# FUNCTION to retrieve Pokémon data
def get_pokemon_data(pokemon_name):
# URL for the specific Pokémon
# DEFINE base URL for PokeAPI
base_url = "https://pokeapi.co/api/v2/"
url = base_url + f"pokemon/{pokemon_name}/"
# GET request to PokeAPI
response = requests.get(url)
# RETRIEVE JSON data from response
data = response.json()
# EXTRACT pokemon name, ability name, and base experience
name = data['name']
ability = data['abilities'][0]['ability']['name']
base_experience = data['base_experience']
# Return as DICT
return {'name': name, 'ability': ability, 'base_experience': base_experience}
# Step 4: CREATE a LIST to store the pokemon data
pokemon_data = []
# Step 5: RETRIEVE data for five pokemon
pokemon_list = ['bulbasaur', 'charmander', 'squirtle', 'pikachu', 'jigglypuff']
for pokemon in pokemon_list:
# CALL the " get_pokemon_data " FUNCTION and APPEND the result to the LIST
pokemon_data.append(get_pokemon_data(pokemon)) #FIX HERE???????????????
# FUNCTION to retrieve the front shiny sprite URL
def get_front_shiny_sprite_url(pokemon_name):
# CONSTRUCT the URL for the specific pokemon
base_url = "https://pokeapi.co/api/v2/"
url = base_url + f"pokemon/{pokemon_name}/"
# Send a GET request to PokeAPI
response = requests.get(url)
# Retrieve the JSON data from the response
data = response.json()
# EXTRACT the URL for the front shiny sprite
sprite_url = data['sprites']['front_shiny']
# Return the sprite URL
return sprite_url
# Step 7: CREATE a LIST to store the sprite URLs
sprite_urls = []
# Step 8: RETRIEVE the front shiny sprite URL for each stored pokemon
for pokemon in pokemon_data:
# Call the " get_front_shiny_sprite_url " FUNCTION and APPEND the result to the list
sprite_urls.append(get_front_shiny_sprite_url(pokemon['name']))
# PRINT the retrieved data for each pokemon
for pokemon in pokemon_data:
print("Name:", pokemon['name'])
print("Ability:", pokemon['ability'])
print("Base Experience:", pokemon['base_experience'])
print()
# Print sprite URLs for each pokemon
for i, sprite_url in enumerate(sprite_urls):
print("Sprite URL for Pokemon", i+1)
print(sprite_url)
print()