diff options
Diffstat (limited to 'src/ee/digikey')
-rw-r--r-- | src/ee/digikey/__init__.py | 19 |
1 files changed, 5 insertions, 14 deletions
diff --git a/src/ee/digikey/__init__.py b/src/ee/digikey/__init__.py index 32308e5..6baae84 100644 --- a/src/ee/digikey/__init__.py +++ b/src/ee/digikey/__init__.py @@ -213,7 +213,7 @@ class DigikeyClient(object): def __init__(self, cache_dir: Path = None, on_download=None): self.on_download = on_download or self.__nop - self.cache_dir = cache_dir or Path() + self.cache = ee._utils.maybe_cache(cache_dir) self.driver: webdriver.Chrome = None def search(self, query: str, page_size=10) -> str: @@ -231,14 +231,10 @@ class DigikeyClient(object): url = "https://www.digikey.com" + url url = url + ("" if not params else "?" + urllib.parse.urlencode(params)) - cache_path: Optional[Path] = None - if self.cache_dir: - cache_path = self.cache_dir / "{}.html".format(cache_key) - - if cache_path.exists(): + cached = self.cache.lookup(cache_key) + if cached: self.on_download("Using cached {}".format(url)) - with open(str(cache_path), "r") as f: - return f.read() + return cached self.on_download("Downloading {}".format(url)) @@ -249,12 +245,7 @@ class DigikeyClient(object): self.driver.get(url) src = self.driver.page_source - if cache_path: - cache_path.parent.mkdir(parents=True, exist_ok=True) - - with open(str(cache_path), "w") as f: - f.write(src) - assert self.cache_dir.stat().st_size > 0 + self.cache.save(cache_key, src) return src |