import json
import sys
-API_SERVER = "https://www.ida.liu.se/~TDDE44/pokeapi/api/v2/"
+API_SERVER = "https://www.ida.liu.se/~TDDE44/pokeapi/"
+
def main():
- data = get_json_as_dict(API_SERVER + "pokemon/")
+ data = get_json_as_dict(API_SERVER + "api/v2/pokemon/")
pokemons = data.get("results")
- for pokemon in pokemons:
- print(pokemon.get("name"))
-
-
+ try:
+ request_name = sys.argv[1]
+ except IndexError:
+ raise ValueError("Must specify pokemon to request data of")
+
+ abilities = get_abilities(request_name, pokemons)
+ print_info(request_name, abilities)
+
def get_json_as_dict(url):
r = requests.get(url)
- assert r.status_code == 200, "Request unsuccessful"
+ if r.status_code != 200:
+ raise requests.RequestException("Request unsuccessful")
return r.json()
-
-
+
+
+def get_abilities(pokemon, pokemon_data_list):
+ poke_url = None
+ for poke in pokemon_data_list:
+ if poke.get("name") == pokemon:
+ poke_url = poke.get("url")
+ break
+ else:
+ 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(f"{name} has {len(abilities)}.\n")
+
+ for ability in abilities:
+ abil = ability.get("ability")
+ abil_url = abil.get("url")
+ abil_data = get_json_as_dict(API_SERVER + abil_url)
+
+ # Linear search for correct language of ability
+ abil_name = None
+ for name in abil_data.get("names"):
+ if name.get("language").get("name") == language:
+ abil_name = name.get("name")
+ break
+
+ # Linear search for correct language ft-entry
+ abil_entry = None
+ for entry in abil_data.get("flavor_text_entries"):
+ if entry.get("language").get("name") == language:
+ abil_entry = entry
+ break
+
+ print(f"Ability {abil_name}: ")
+ print(f"{abil_entry.get('flavor_text')}\n")
+
+
if __name__ == "__main__":
main()
-
\ No newline at end of file
else:
return n * fac_rec(n-1)
+
def fib(n):
if n == 0:
return 0
else:
return pascal(row-1, col) + pascal(row-1, col - 1)
+
def keep_if_even(lst):
if not lst:
return list()
ret.extend(reverse_rec(lst[:-1]))
return ret
+
def keep_if_even_all(lst):
if not lst:
return list()
even_inner = list()
if isinstance(lst[0], list):
even_inner = keep_if_even_all(lst[0])
- even_inner = [even_inner] #Ibland e man bara lat
+ even_inner = [even_inner] # Ibland e man bara lat
elif not lst[0] % 2:
ret = [lst[0]]
ret.extend(keep_if_even_all(lst[1:]))
even_inner.extend(keep_if_even_all(lst[1:]))
return even_inner
+
def reverse_rec_all(lst):
if not lst:
return []
reverse_inner.extend(reverse_rec_all(lst[:-1]))
return reverse_inner
+
def is_in_list(lst, elm):
if not lst:
return False
return list()
if isinstance(lst[0], list):
inner = subst_all(lst[0], elm, new_value)
- inner = [inner] # Pallar inte skriva om snyggt s blir lite fulkod ibland men det funkar
+ # Pallar inte skriva om snyggt s blir lite fulkod ibland men det funkar
+ inner = [inner]
elif lst[0] == elm:
inner = [new_value]
else:
inner.extend(subst_all(lst[1:], elm, new_value))
return inner
+
def linear_search(key, seq):
if not seq:
return False