Exemplo n.º 1
0
def load_ability_from_api(name):
    request = requests.get(f'https://pokeapi.co/api/v2/ability/{name}')
    data = request.json()

    generation = Generation.get_or_none(name=data['generation']['name'])
    if generation is None:
        generation = Generation.create(name=data['generation']['name'])

    ability = Ability.get_or_none(name=name)
    if ability is None:
        db_data = {
            'name': data['name'],
            'is_main_series': data['is_main_series'],
            'generation': generation
        }
        ability = Ability.create(**db_data)

    AbilityEffects.delete().where(AbilityEffects.ability == ability).execute()
    for effect in data['effect_entries']:
        verbose_effect = VerboseEffect.get_or_none(
            short_effect=effect['short_effect'])
        if verbose_effect is None:
            language = Language.get_or_none(name=effect['language']['name'])
            if language is None:
                language = Language.create(name=effect['language']['name'])
            verbose_effect = VerboseEffect.create(
                effect=effect['effect'],
                short_effect=effect['short_effect'],
                language=language)
        ability_effect = AbilityEffects.create(ability=ability,
                                               effect=verbose_effect)

    return ability
Exemplo n.º 2
0
def load_pokemon_abilities_from_api(name):
    request = requests.get(f'https://pokeapi.co/api/v2/pokemon/{name}')
    pokemon_data = request.json()

    pokemon = get_pokemon_by_name(name)
    PokemonAbilities.delete().where(
        PokemonAbilities.pokemon == pokemon).execute()

    abilities = []
    for api_ability in pokemon_data['abilities']:
        ability_name = api_ability['ability']['name']

        ability = Ability.get_or_none(name=ability_name)
        if ability is None:
            ability = Ability.create(name=ability_name,
                                     url=api_ability['ability']['url'])

        pokemon_ability = PokemonAbilities.create(
            pokemon=pokemon,
            ability=ability,
            is_hidden=api_ability['is_hidden'],
            slot=api_ability['slot'])

        abilities.append(pokemon_ability)

    return abilities