# pokedex.py
import requests
-import json
import sys
API_SERVER = "https://www.ida.liu.se/~TDDE44/pokeapi/"
def main():
+ """Mainloop
+
+ Raises:
+ ValueError: Script is not run with mandatory pokemon argument
+ """
data = get_json_as_dict(API_SERVER + "api/v2/pokemon/")
pokemons = data.get("results")
try:
def get_json_as_dict(url):
+ """Fetch data from url in JSON-format
+
+ Args:
+ url (str): API url
+
+ Raises:
+ requests.RequestException: data request unsuccessful
+
+ Returns:
+ dict: data in JSON-format
+ """
r = requests.get(url)
if r.status_code != 200:
raise requests.RequestException("Request unsuccessful")
def get_abilities(pokemon, pokemon_data_list):
+ """Get abilities of named pokemon from lookup list
+
+ Args:
+ pokemon (str): Requested pokemon
+ pokemon_data_list (list): lookup list of all pokemons
+
+ Raises:
+ ValueError: Pokemon requested not found in the given list
+
+ Returns:
+ list: Abilities of pokemon
+ """
+ # Linear search for the requested pokemon and get the pokemon-url
poke_url = None
for poke in pokemon_data_list:
if poke.get("name") == pokemon:
raise ValueError(f"No pokemon named {pokemon}")
pokemon_data = get_json_as_dict(API_SERVER + poke_url)
-
return pokemon_data.get("abilities")
def print_info(name, abilities, language="en"):
+ """Print the name and abilities of the pokemon
+
+ Args:
+ name (str): Name of pokemon
+ abilities (list): list of abilities of the pokemon
+ language (str, optional): Language to print in. Defaults to "en".
+ """
print(f"{name} has {len(abilities)}.\n")
+ # Repeat for every ability
for ability in abilities:
abil = ability.get("ability")
abil_url = abil.get("url")