aboutsummaryrefslogtreecommitdiff
path: root/src/ee/digikey
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2019-03-08 22:30:36 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2019-03-08 22:30:36 +0100
commit3a90ab0dbf5826bc7476971cd163c9a080d2fb2f (patch)
tree6c42ad693ec8cfcec78483c45a7dce4bdc4e4724 /src/ee/digikey
parent5a6ab34565fa3d783337a68e07e3500361c8fcf5 (diff)
downloadee-python-3a90ab0dbf5826bc7476971cd163c9a080d2fb2f.tar.gz
ee-python-3a90ab0dbf5826bc7476971cd163c9a080d2fb2f.tar.bz2
ee-python-3a90ab0dbf5826bc7476971cd163c9a080d2fb2f.tar.xz
ee-python-3a90ab0dbf5826bc7476971cd163c9a080d2fb2f.zip
Digikey: extracting http caching into its own class.
Diffstat (limited to 'src/ee/digikey')
-rw-r--r--src/ee/digikey/__init__.py19
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