From 3a90ab0dbf5826bc7476971cd163c9a080d2fb2f Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 8 Mar 2019 22:30:36 +0100 Subject: Digikey: extracting http caching into its own class. --- src/ee/digikey/__init__.py | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) (limited to 'src/ee/digikey') 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 -- cgit v1.2.3