def add_type(name, generation_name): generation = Generation.get_or_none(Generation.name == generation_name) if generation is None: generation = Generation.create(name=generation_name) new_type = Type.create(name=name, generation=generation) return new_type
def load_type_from_api(name): request = requests.get(f'https://pokeapi.co/api/v2/type/{name}') data = request.json() generation = Generation.get_or_none(name=data['generation']['name']) if generation is None: generation = Generation.create(name=data['generation']['name']) type = Type.get_or_none(name=data['name']) if type is None: type = Type.create(name=data['name'], generation=generation) return type
def load_pokemon_types_from_api(name): request = requests.get(f'https://pokeapi.co/api/v2/pokemon/{name}') pokemon_data = request.json() pokemon = get_pokemon_by_name(name) PokemonTypes.delete().where(PokemonTypes.pokemon == pokemon).execute() types = [] for api_type in pokemon_data['types']: type_name = api_type['type']['name'] type = Type.get_or_none(name=type_name) if type is None: type = Type.create(name=type_name, url=api_type['type']['url']) pokemon_type = PokemonTypes.create(pokemon=pokemon, type=type, slot=api_type['slot']) types.append(pokemon_type) return types