Add reference data mappings and auto-download for PokeDB import tool
Add mappings module with pokemon form, location area, encounter method, and version mappings. Auto-download PokeDB JSON exports from CDN on first run, caching in .pokedb_cache/. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -4,17 +4,20 @@ from __future__ import annotations
|
||||
|
||||
import json
|
||||
import sys
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
REQUIRED_FILES = [
|
||||
"encounters.json",
|
||||
"locations.json",
|
||||
"location_areas.json",
|
||||
"encounter_methods.json",
|
||||
"versions.json",
|
||||
"pokemon_forms.json",
|
||||
]
|
||||
_CDN_BASE = "https://cdn.pokedb.org"
|
||||
|
||||
REQUIRED_FILES: dict[str, str] = {
|
||||
"encounters.json": f"{_CDN_BASE}/data_export_encounters_json",
|
||||
"locations.json": f"{_CDN_BASE}/data_export_locations_json",
|
||||
"location_areas.json": f"{_CDN_BASE}/data_export_location_areas_json",
|
||||
"encounter_methods.json": f"{_CDN_BASE}/data_export_encounter_methods_json",
|
||||
"versions.json": f"{_CDN_BASE}/data_export_versions_json",
|
||||
"pokemon_forms.json": f"{_CDN_BASE}/data_export_pokemon_forms_json",
|
||||
}
|
||||
|
||||
|
||||
class PokeDBData:
|
||||
@@ -48,23 +51,38 @@ class PokeDBData:
|
||||
)
|
||||
|
||||
|
||||
def download_pokedb_data(data_dir: Path) -> None:
|
||||
"""Download missing PokeDB JSON export files into data_dir."""
|
||||
data_dir.mkdir(parents=True, exist_ok=True)
|
||||
missing = {f: url for f, url in REQUIRED_FILES.items() if not (data_dir / f).exists()}
|
||||
if not missing:
|
||||
return
|
||||
|
||||
print(f"Downloading {len(missing)} PokeDB file(s) to {data_dir}...")
|
||||
for filename, url in missing.items():
|
||||
dest = data_dir / filename
|
||||
print(f" {filename}...", end="", flush=True)
|
||||
try:
|
||||
urllib.request.urlretrieve(url, dest)
|
||||
size_mb = dest.stat().st_size / (1024 * 1024)
|
||||
print(f" {size_mb:.1f} MB")
|
||||
except Exception as e:
|
||||
print(f" FAILED", file=sys.stderr)
|
||||
print(f"Error downloading {url}: {e}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
print()
|
||||
|
||||
|
||||
def load_pokedb_data(data_dir: Path) -> PokeDBData:
|
||||
"""Load all PokeDB JSON export files from a directory.
|
||||
|
||||
Exits with an error message if any required files are missing or unparseable.
|
||||
Downloads any missing files automatically, then validates and loads them.
|
||||
"""
|
||||
download_pokedb_data(data_dir)
|
||||
|
||||
missing = [f for f in REQUIRED_FILES if not (data_dir / f).exists()]
|
||||
if missing:
|
||||
print(
|
||||
f"Error: Missing required PokeDB files in {data_dir}:",
|
||||
file=sys.stderr,
|
||||
)
|
||||
for f in missing:
|
||||
print(f" - {f}", file=sys.stderr)
|
||||
print(
|
||||
"\nDownload the JSON export from https://pokedb.org/data-export",
|
||||
file=sys.stderr,
|
||||
)
|
||||
print(f"Error: Still missing files after download: {missing}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
def _load(filename: str) -> list[dict[str, Any]]:
|
||||
|
||||
Reference in New Issue
Block a user