aboutsummaryrefslogtreecommitdiff
path: root/src/ee/digikey/__init__.py
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2019-05-28 09:46:41 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2019-05-28 09:46:41 +0200
commit2e2956823c9cd02c766b296cbcbea9130bd07b36 (patch)
tree29be58764f33374cd768a5151182d1a2aa75b041 /src/ee/digikey/__init__.py
parentd108963a31f726ec8e1f471695addbbabb0db312 (diff)
downloadee-python-2e2956823c9cd02c766b296cbcbea9130bd07b36.tar.gz
ee-python-2e2956823c9cd02c766b296cbcbea9130bd07b36.tar.bz2
ee-python-2e2956823c9cd02c766b296cbcbea9130bd07b36.tar.xz
ee-python-2e2956823c9cd02c766b296cbcbea9130bd07b36.zip
digikey: Better search when getting multiple results back. Instead of
doing a new search with the selected digikey part number, do a direct lookup with the product's URL instead. This ensures that we always get a match and don't get confused when multiple part numbers are returned.
Diffstat (limited to 'src/ee/digikey/__init__.py')
-rw-r--r--src/ee/digikey/__init__.py33
1 files changed, 19 insertions, 14 deletions
diff --git a/src/ee/digikey/__init__.py b/src/ee/digikey/__init__.py
index 31d5c41..031faff 100644
--- a/src/ee/digikey/__init__.py
+++ b/src/ee/digikey/__init__.py
@@ -252,9 +252,9 @@ class DigikeyProductCategory(object):
class SearchResponseTypes(enum.Enum):
- MANY = 1
- SINGLE = 2
- TOO_MANY = 3
+ MANY = 1 # A product table was returned.
+ SINGLE = 2 # A product page was returned
+ TOO_MANY = 3 # A listing of categories was given, the user is expected to narrow down the search
NO_MATCHES = 4
@@ -310,16 +310,23 @@ class DigikeyClient(object):
return src
+ def get_for_product_url(self, url, product_number):
+ return self._req(url, "product-{}".format(product_number))
+
+ def get(self, url, cache_key, params=None):
+ return self._req(url, cache_key, params)
+
class DigikeyParser(object):
def __init__(self, digikey: Digikey):
self.digikey = digikey or Digikey()
- def _search_process_single_result(self, tree: html) -> Optional[DigikeyProduct]:
+ def _search_process_single_result(self, origin_url, tree: html) -> Optional[DigikeyProduct]:
attributes = []
categories = []
url = _first((link.get("href") for link in tree.xpath("/html/head/link[@rel='canonical' and @href]")))
+ url = self.ensure_absolute_url(origin_url, url)
part_number = mpn = None
for n in tree.xpath("//*[@itemprop='productID' and @content]"):
@@ -402,7 +409,6 @@ class DigikeyParser(object):
docs = []
for row in tree.xpath("//*[@class='product-details-documents-media product-details-section']//tr"):
- # print("row={}".format(row))
kind: str = _first(row.xpath(".//th/text()"))
if not kind:
@@ -411,7 +417,6 @@ class DigikeyParser(object):
kind = kind.strip()
for a in row.xpath(".//td//a[not(contains(@class, '-expander-toggle'))]"):
- # print("a={}".format(a))
title = a.text
if not title:
continue
@@ -429,12 +434,12 @@ class DigikeyParser(object):
return docs
- @staticmethod
- def _handle_product_table(tree: html, res: DigikeySearchResponse):
+ def _handle_product_table(self, origin_url, tree: html, res: DigikeySearchResponse):
products = tree.xpath("//*[@itemtype='http://schema.org/Product']")
for product in products:
url = _first((a.get("href") for a in product.xpath(".//*[@class='tr-image']//a[@href]")))
+ url = self.ensure_absolute_url(origin_url, url)
part_number = _first(product.xpath(".//*[@itemprop='productid' and @content]"))
mpn = _first(product.xpath(".//*[@itemprop='name']"))
@@ -445,8 +450,7 @@ class DigikeyParser(object):
return len(products)
- @staticmethod
- def _handle_exact_part_list(tree: html, res: DigikeySearchResponse):
+ def _handle_exact_part_list(self, origin_url, tree: html, res: DigikeySearchResponse):
products = tree.xpath(".//tr[@class='exactPart']")
for product in products:
@@ -455,12 +459,13 @@ class DigikeyParser(object):
if a is not None and part_number is not None:
url = a.get("href")
+ url = self.ensure_absolute_url(origin_url, url)
mpn = a.text
res.append(DigikeyProduct(part_number.text, mpn, url))
return len(products)
- def parse_string(self, page_content: str):
+ def parse_string(self, origin_url, page_content: str):
tree = html.fromstring(page_content)
count = _first([_parse_int(e.text) for e in tree.xpath("//span[@id='matching-records-count']") if e.text])
@@ -471,11 +476,11 @@ class DigikeyParser(object):
if product_table is not None:
res = DigikeySearchResponse(count, SearchResponseTypes.MANY)
- self._handle_product_table(product_table, res)
+ self._handle_product_table(origin_url, product_table, res)
return res
elif exact_part_list is not None:
res = DigikeySearchResponse(count, SearchResponseTypes.MANY)
- self._handle_exact_part_list(exact_part_list, res)
+ self._handle_exact_part_list(origin_url, exact_part_list, res)
return res
else:
# If the search matches multiple product categories the user has to select the appropriate category
@@ -483,7 +488,7 @@ class DigikeyParser(object):
return DigikeySearchResponse(count, SearchResponseTypes.TOO_MANY)
else:
- p = self._search_process_single_result(tree)
+ p = self._search_process_single_result(origin_url, tree)
if p:
res = DigikeySearchResponse(1, SearchResponseTypes.SINGLE)
res.append(p)