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

    species = PokemonSpecies.get_or_none(name=name)
    if species is None:
        db_data = {
            'name': data['name'],
            'order': data['order'],
            'gender_rate': data['gender_rate'],
            'capture_rate': data['capture_rate'],
            'base_happiness': data['base_happiness'],
            'is_baby': data['is_baby']
        }
        species = PokemonSpecies.create(**db_data)

    PokemonSpeciesEggGroups.delete().where(
        PokemonSpeciesEggGroups.pokemon_species == species).execute()
    for api_egg_group in data['egg_groups']:
        egg_group = EggGroup.get_or_none(name=api_egg_group['name'])
        PokemonSpeciesEggGroups.create(pokemon_species=species,
                                       egg_group=egg_group)

    PokemonSpeciesVariety.delete().where(
        PokemonSpeciesVariety.pokemon_species == species).execute()
    for variety in data['varieties']:
        pokemon = Pokemon.get_or_none(name=variety['pokemon']['name'])
        PokemonSpeciesVariety.create(pokemon_species=species,
                                     is_default=variety['is_default'],
                                     pokemon=pokemon)

    return species
Exemplo n.º 2
0
def create_pokemon(name, hp, special_attack, defense, attack, special_defense,
                   speed):
    stats = {
        'hp': hp,
        'special_attack': special_attack,
        'defense': defense,
        'attack': attack,
        'special_defense': special_defense,
        'speed': speed
    }
    pokemon = Pokemon.get_or_none(name=name)
    if pokemon is None:
        pokemon = Pokemon.create(name=name, **stats)

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

    stats = {}
    for stat in pokemon_data['stats']:
        stat_name = stat['stat']['name'].replace('-', '_')
        stat_value = int(stat['base_stat'])

        stats[stat_name] = stat_value

    sprite_front = pokemon_data['sprites']['front_default']
    sprite_back = pokemon_data['sprites']['back_default']

    pokemon = Pokemon.get_or_none(name=name)
    data = {'sprite_front': sprite_front, 'sprite_back': sprite_back, **stats}
    if pokemon is None:
        pokemon = Pokemon.create(name=name, **data)
    else:
        update_model_from_dict(pokemon, data)
        pokemon.save()

    return pokemon
Exemplo n.º 4
0
def load_pokemon_from_api(name):
    request = requests.get(f"https://pokeapi.co/api/v2/pokemon/{name}")
    pokemon_data = request.json()

    stats = {}
    for stat in pokemon_data["stats"]:
        stat_name = stat["stat"]["name"].replace("-", "_")
        stat_value = int(stat["base_stat"])

        stats[stat_name] = stat_value

    sprite_front = pokemon_data["sprites"]["front_default"]
    sprite_back = pokemon_data["sprites"]["back_default"]

    pokemon = Pokemon.get_or_none(name=name)
    data = {"sprite_front": sprite_front, "sprite_back": sprite_back, **stats}
    if pokemon is None:
        pokemon = Pokemon.create(name=name, **data)
    else:
        update_model_from_dict(pokemon, data)
        pokemon.save()

    return pokemon
Exemplo n.º 5
0
def get_pokemon_by_name(name):
    pokemon = Pokemon.get_or_none(name=name)
    if pokemon is None:
        raise PokemonNotFoundError(name)
    return pokemon
Exemplo n.º 6
0
def get_pokemon_by_name(name):
    pokemon = Pokemon.get_or_none(name=name)
    return pokemon