diff --git a/hearthstone/cardxml.py b/hearthstone/cardxml.py index 77a707b..f3b96cc 100644 --- a/hearthstone/cardxml.py +++ b/hearthstone/cardxml.py @@ -397,7 +397,10 @@ def is_functional_duplicate_of(self, other): XML_URL = "https://api.hearthstonejson.com/v1/latest/CardDefs.xml" -def _bootstrap_from_web(url: str, parse: Callable[[Iterator[Tuple[str, Any]]], None]): +def _bootstrap_from_web(parse: Callable[[Iterator[Tuple[str, Any]]], None], url=None): + if url is None: + url = XML_URL + with tempfile.TemporaryFile(mode="rb+") as fp: if download_to_tempfile_retry(url, fp): fp.flush() @@ -446,7 +449,7 @@ def parse(context: Iterator[Tuple[str, Any]]): has_lib = False if not has_lib: - _bootstrap_from_web(url or XML_URL, parse) + _bootstrap_from_web(parse, url=url) if not db: _bootstrap_from_library(parse, path=path) diff --git a/hearthstone/mercenaryxml.py b/hearthstone/mercenaryxml.py index c2dae32..ce86230 100644 --- a/hearthstone/mercenaryxml.py +++ b/hearthstone/mercenaryxml.py @@ -191,9 +191,12 @@ def to_xml(self): XML_URL = "https://api.hearthstonejson.com/v1/latest/MercenaryDefs.xml" -def _bootstrap_from_web(parse: Callable[[Iterator[Tuple[str, Any]]], None]): +def _bootstrap_from_web(parse: Callable[[Iterator[Tuple[str, Any]]], None], url=None): + if url is None: + url = XML_URL + with tempfile.TemporaryFile(mode="rb+") as fp: - if download_to_tempfile_retry(XML_URL, fp): + if download_to_tempfile_retry(url, fp): fp.flush() fp.seek(0) @@ -210,7 +213,7 @@ def _bootstrap_from_library(parse: Callable[[Iterator[Tuple[str, Any]]], None], parse(ElementTree.iterparse(f, events=("start", "end",))) -def load(path=None, locale="enUS"): +def load(locale="enUS", path=None, url=None): cache_key = (path, locale) if cache_key not in mercenary_cache: db = {} @@ -232,7 +235,7 @@ def parse(context: Iterator[Tuple[str, Any]]): root.clear() # type: ignore if path is None: - _bootstrap_from_web(parse) + _bootstrap_from_web(parse, url=url) if not db: _bootstrap_from_library(parse, path=path)