Skip to content

static_data

static_data

Static game data (troops, spells, heroes, pets, equipment, leagues, ...).

The official API serves no static game data, so this module loads a JSON file in the format published by the ClashKing project (https://github.com/ClashKingInc/clashy.py, coc/static/static_data.json, MIT). Nothing is bundled: pass a path, or call :meth:StaticData.download to fetch and cache it (explicit opt-in network access).

Example::

from cocapi import StaticData

sd = StaticData.download()            # cached at ~/.cocapi/static_data.json
sd.by_name("troops", "Barbarian")
sd.by_id("heroes", 28000000)
sd.names("equipment")

StaticData

StaticData(data: dict[str, Any])

Name/ID lookups over a static game data JSON document.

Source code in cocapi/static_data.py
34
35
36
37
def __init__(self, data: dict[str, Any]):
    self.data = data
    self._by_name: dict[str, dict[str, dict[str, Any]]] = {}
    self._by_id: dict[str, dict[int, dict[str, Any]]] = {}

load classmethod

load(path: str | Path | None = None) -> StaticData

Load from a local JSON file (default ~/.cocapi/static_data.json).

Source code in cocapi/static_data.py
39
40
41
42
43
44
@classmethod
def load(cls, path: str | Path | None = None) -> "StaticData":
    """Load from a local JSON file (default ``~/.cocapi/static_data.json``)."""
    p = Path(path) if path else DEFAULT_PATH
    with open(p, encoding="utf-8") as f:
        return cls(json.load(f))

download classmethod

download(
    path: str | Path | None = None,
    url: str = DEFAULT_URL,
    refresh: bool = False,
    timeout: float = 60.0,
) -> StaticData

Download the data (unless already cached) and load it.

PARAMETER DESCRIPTION
path

Where to cache the file (default ~/.cocapi/static_data.json).

TYPE: str | Path | None DEFAULT: None

url

Source URL.

TYPE: str DEFAULT: DEFAULT_URL

refresh

Re-download even if the cache file exists.

TYPE: bool DEFAULT: False

Source code in cocapi/static_data.py
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
@classmethod
def download(
    cls,
    path: str | Path | None = None,
    url: str = DEFAULT_URL,
    refresh: bool = False,
    timeout: float = 60.0,
) -> "StaticData":
    """Download the data (unless already cached) and load it.

    Args:
        path: Where to cache the file (default ``~/.cocapi/static_data.json``).
        url: Source URL.
        refresh: Re-download even if the cache file exists.
    """
    import httpx

    p = Path(path) if path else DEFAULT_PATH
    if refresh or not p.exists():
        resp = httpx.get(url, timeout=timeout, follow_redirects=True)
        resp.raise_for_status()
        json.loads(resp.content)  # validate before writing
        p.parent.mkdir(parents=True, exist_ok=True)
        p.write_bytes(resp.content)
    return cls.load(p)

by_name

by_name(category: str, name: str) -> dict[str, Any] | None

Case-insensitive lookup by name.

Source code in cocapi/static_data.py
82
83
84
85
86
87
88
def by_name(self, category: str, name: str) -> dict[str, Any] | None:
    """Case-insensitive lookup by name."""
    index = self._by_name.get(category)
    if index is None:
        index = {i["name"].lower(): i for i in self.items(category) if "name" in i}
        self._by_name[category] = index
    return index.get(name.lower())

max_level

max_level(
    category: str, name: str, townhall: int | None = None
) -> int | None

Highest level of an item (optionally limited by Town Hall level).

Source code in cocapi/static_data.py
113
114
115
116
117
118
119
120
121
122
123
124
125
def max_level(
    self, category: str, name: str, townhall: int | None = None
) -> int | None:
    """Highest level of an item (optionally limited by Town Hall level)."""
    item = self.by_name(category, name)
    if not item:
        return None
    levels = item.get("levels", [])
    if townhall is not None:
        levels = [
            lv for lv in levels if (lv.get("required_townhall") or 0) <= townhall
        ]
    return max((lv["level"] for lv in levels), default=None)