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 get_type_by_name(query): if query is not None: query.lower() types = Type.select().where(query << Type.name) if len(types > 10): return (types[:10])
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 get_types(search=None, unused=False): if search is None: search = "" types = [] for type in Type.select().limit(10): if search in type.name: types.append(type) if unused: types = [type for type in types if len(type.pokemons) == 0] return types
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
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) pokemon_type = PokemonTypes.create(pokemon=pokemon, type=type, slot=api_type["slot"]) types.append(pokemon_type) return types
def get_number_of_types_by_generation(generation=None): type_of_generation = Type.select().where(Type.generation == generation) number_of_types_of_generation = type_of_generation.count() return number_of_types_of_generation
def get_type_name(name): type = Type.get_or_none(name=name) return type
def number_of_Types(generation_id): counter = len(Type.select().where(Type.generation_id == generation_id)) print(counter) return counter