Spaces:
Sleeping
Sleeping
File size: 6,320 Bytes
681ea07 1bcc1a1 681ea07 1bcc1a1 681ea07 1bcc1a1 681ea07 1bcc1a1 681ea07 1bcc1a1 681ea07 1bcc1a1 681ea07 1bcc1a1 681ea07 1bcc1a1 681ea07 1bcc1a1 681ea07 1bcc1a1 681ea07 1bcc1a1 681ea07 1bcc1a1 681ea07 1bcc1a1 681ea07 1bcc1a1 681ea07 1bcc1a1 681ea07 1bcc1a1 681ea07 1bcc1a1 681ea07 1bcc1a1 681ea07 1bcc1a1 681ea07 1bcc1a1 681ea07 1bcc1a1 681ea07 1bcc1a1 681ea07 1bcc1a1 681ea07 1bcc1a1 681ea07 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | import requests
import json
BASE_URL = "https://pokeapi.co/api/v2"
def _get(endpoint: str, name_or_id: str) -> str:
"""
Internal helper to fetch data from PokéAPI and handle errors.
Args:
endpoint (str): API endpoint (e.g., 'pokemon', 'item')
name_or_id (str): Resource name or ID
Returns:
str: JSON string response from the API
Raises:
ValueError: If the resource is not found or an error occurs
"""
url = f"{BASE_URL}/{endpoint}/{str(name_or_id).lower()}"
headers = {
"Accept": "application/json",
"Content-Type": "application/json"
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
return json.dumps(response.json())
raise ValueError(f"❌ {endpoint} '{name_or_id}' not found (HTTP {response.status_code})")
def get_pokemon(name_or_id):
"""
Get full data for a Pokémon (stats, abilities, types, sprites, etc.).
Args:
name_or_id (str or int): Pokémon name or Pokédex ID
Returns:
str: Pokémon data
"""
return _get("pokemon", name_or_id)
def get_pokemon_species(name_or_id):
"""
Get species-level data for a Pokémon (evolution chain, flavor text, gender ratio, etc.).
Args:
name_or_id (str or int): Pokémon name or ID
Returns:
str: Pokémon species data
"""
return _get("pokemon-species", name_or_id)
def get_ability(name_or_id):
"""
Get details about a specific Pokémon ability (effects, Pokémon with this ability, etc.).
Args:
name_or_id (str or int): Ability name or ID
Returns:
str: Ability data
"""
return _get("ability", name_or_id)
def get_type(name_or_id):
"""
Get data for a specific Pokémon type (damage relations, Pokémon by type, etc.).
Args:
name_or_id (str or int): Type name or ID
Returns:
str: Type data
"""
return _get("type", name_or_id)
def get_item(name_or_id):
"""
Get information on an item (e.g., Potion, Berry, Stone).
Args:
name_or_id (str or int): Item name or ID
Returns:
str: Item data
"""
return _get("item", name_or_id)
def get_berry(name_or_id):
"""
Get data about a specific berry (flavor, firmness, effects, etc.).
Args:
name_or_id (str or int): Berry name or ID
Returns:
str: Berry data
"""
return _get("berry", name_or_id)
def get_evolution_chain(chain_id):
"""
Get full data for an evolution chain (species involved, stages, etc.).
Args:
chain_id (int): Evolution chain ID (not Pokémon ID)
Returns:
str: Evolution chain data
"""
return _get("evolution-chain", chain_id)
def get_location(name_or_id):
"""
Get information on a specific location in the Pokémon world.
Args:
name_or_id (str or int): Location name or ID
Returns:
str: Location data
"""
return _get("location", name_or_id)
def get_move(name_or_id):
"""
Get detailed data for a specific move (power, accuracy, type, effects).
Args:
name_or_id (str or int): Move name or ID
Returns:
str: Move data
"""
return _get("move", name_or_id)
def get_stat(name_or_id):
"""
Get metadata about a base stat (e.g., HP, Attack).
Args:
name_or_id (str or int): Stat name or ID
Returns:
str: Stat data
"""
return _get("stat", name_or_id)
def get_egg_group(name_or_id):
"""
Get information about an egg group (used for Pokémon breeding compatibility).
Args:
name_or_id (str or int): Egg group name or ID
Returns:
str: Egg group data
"""
return _get("egg-group", name_or_id)
def get_pokemon_form(name_or_id):
"""
Get data about a specific form of a Pokémon (e.g., Alolan, Galarian forms).
Args:
name_or_id (str or int): Form name or ID
Returns:
str: Pokémon form data
"""
return _get("pokemon-form", name_or_id)
def get_pokemon_habitat(name_or_id):
"""
Get information about the habitat where a Pokémon is commonly found.
Args:
name_or_id (str or int): Habitat name or ID
Returns:
str: Habitat data
"""
return _get("pokemon-habitat", name_or_id)
def get_pokemon_color(name_or_id):
"""
Get Pokémon categorized by their color (used mainly for Pokédex sorting).
Args:
name_or_id (str or int): Color name or ID
Returns:
str: Color group data
"""
return _get("pokemon-color", name_or_id)
def get_pokemon_shape(name_or_id):
"""
Get Pokémon categorized by shape (used in evolution and form grouping).
Args:
name_or_id (str or int): Shape name or ID
Returns:
str: Shape group data
"""
return _get("pokemon-shape", name_or_id)
def get_generation(name_or_id):
"""
Get data about a Pokémon generation (introduced species, moves, types, etc.).
Args:
name_or_id (str or int): Generation name or ID (e.g., 'generation-i')
Returns:
str: Generation data
"""
return _get("generation", name_or_id)
def get_version(name_or_id):
"""
Get information about a game version (e.g., Red, Blue, Diamond, etc.).
Args:
name_or_id (str or int): Version name or ID
Returns:
str: Version data
"""
return _get("version", name_or_id)
def get_version_group(name_or_id):
"""
Get groupings of game versions (used for shared move sets, regions, etc.).
Args:
name_or_id (str or int): Version group name or ID
Returns:
str: Version group data
"""
return _get("version-group", name_or_id)
def get_pokedex(name_or_id):
"""
Get data for a specific Pokédex (regional or national).
Args:
name_or_id (str or int): Pokédex name or ID (e.g., 'kanto', 'national')
Returns:
str: Pokédex data
"""
return _get("pokedex", name_or_id)
def get_region(name_or_id):
"""
Get details about a region in the Pokémon world (e.g., Kanto, Sinnoh).
Args:
name_or_id (str or int): Region name or ID
Returns:
str: Region data
"""
return _get("region", name_or_id)
|