Create a tool that pulls data of all pokemon and lets the user filter by name, type,…
- User
Create a tool that pulls data of all pokemon and lets the user filter by name, type, mínimum base stat, mínimum of sum of combinations of base stats
- Assistant
Create a tool that pulls data of all pokemon and lets the user filter by name, type, mínimum base stat, mínimum of sum of combinations of base stats
Certainly! Below is a Python script that pulls data of all Pokémon from the PokéAPI and lets the user filter by name, type, minimum base stat, and minimum sum of combinations of base stats.
import requests
def get_pokemon_list():
# Get the list of all Pokémon names and URLs
url = 'https://pokeapi.co/api/v2/pokemon?limit=1010' # Adjust the limit as necessary
response = requests.get(url)
data = response.json()
return data['results']
def get_pokemon_data(pokemon_url):
# Get detailed data for a single Pokémon
response = requests.get(pokemon_url)
return response.json()
def main():
print("Fetching Pokémon data...")
pokemon_list = get_pokemon_list()
all_pokemon_data = []
# Fetch data for each Pokémon
for pokemon in pokemon_list:
pokemon_name = pokemon['name']
pokemon_url = pokemon['url']
pokemon_data = get_pokemon_data(pokemon_url)
# Get types
types = [t['type']['name'] for t in pokemon_data['types']]
# Get base stats
stats = {stat['stat']['name']: stat['base_stat'] for stat in pokemon_data['stats']}
# Create a Pokémon entry
pokemon_entry = {
'name': pokemon_name,
'types': types,
'stats': stats
}
all_pokemon_data.append(pokemon_entry)
print(f"Total Pokémon fetched: {len(all_pokemon_data)}\n")
# User input for filters
name_filter = input("Filter by name (leave blank for no filter): ").lower().strip()
type_filter = input("Filter by type (leave blank for no filter): ").lower().strip()
min_base_stat_name = input("Minimum base stat name (e.g., 'attack', leave blank for no filter): ").lower().strip()
min_base_stat_value = None
if min_base_stat_name:
min_base_stat_value = int(input(f"Minimum value for '{min_base_stat_name}': "))
sum_stats_input = input("Enter base stats to sum separated by commas (e.g., 'attack, speed', leave blank for no filter): ").lower().strip()
min_sum_stats_value = None
sum_stats = []
if sum_stats_input:
sum_stats = [s.strip() for s in sum_stats_input.split(",")]
min_sum_stats_value = int(input("Minimum sum of the specified stats: "))
# Filter the Pokémon list
filtered_pokemon = []
for pokemon in all_pokemon_data:
# Name filter
if name_filter and name_filter not in pokemon['name']:
continue
# Type filter
if type_filter and type_filter not in pokemon['types']:
continue
# Minimum base stat filter
if min_base_stat_name:
stat_value = pokemon['stats'].get(min_base_stat_name, 0)
if stat_value < min_base_stat_value:
continue
# Minimum sum of stats filter
if sum_stats_input:
sum_of_stats = sum(pokemon['stats'].get(stat, 0) for stat in sum_stats)
if sum_of_stats < min_sum_stats_value:
continue
# If all filters pass, add to the filtered list
filtered_pokemon.append(pokemon)
# Display the filtered Pokémon
if filtered_pokemon:
print("\nFiltered Pokémon:")
for pokemon in filtered_pokemon:
print(f"Name: {pokemon['name'].title()}")
print(f"Types: {', '.join([t.title() for t in pokemon['types']])}")
print("Stats:")
for stat_name, stat_value in pokemon['stats'].items():
print(f" {stat_name.title()}: {stat_value}")
print("\n")
else:
print("\nNo Pokémon match the given filters.")
if __name__ == "__main__":
main()
Explanation:
Import Statements:
requests library is used to make HTTP requests to the PokéAPI. Ensure you have it installed (pip install requests).Functions:
get_pokemon_list(): Fetches a list of all Pokémon names and URLs.get_pokemon_data(pokemon_url): Fetches detailed data for a single Pokémon using its URL.Main Execution Flow:
Usage Notes:
Performance Consideration: Fetching data for all Pokémon can take several minutes due to the large number of API calls (one per Pokémon). To improve performance:
aiohttp for faster data fetching.API Rate Limiting: Be mindful of the PokéAPI's rate limits to avoid being blocked. The API is free to use, but excessive calls in a short period can lead to issues.
Examples:
char will find Pokémon like Charmander, Charmeleon, and Charizard.fire will list all Fire-type Pokémon.'attack' with a value of 100 will filter Pokémon with an attack stat of at least 100.'attack, speed' with a minimum sum of 200 will find Pokémon whose attack and speed stats sum to at least 200.Enhancements:
Error Handling: The script assumes valid input from the user. You may want to add error handling for non-integer inputs where numbers are expected.
User Interface: For a more interactive experience, consider developing a simple GUI using libraries like tkinter or PyQt5.
Extended Functionality: You can extend the script to include additional filters, such as:
Important: Always respect the terms of service of any API you use, and cache data responsibly to minimize unnecessary API calls.
Let me know if you need further assistance or have any questions about the script!
Not yet included in a published release.