diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2017-09-10 12:32:49 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2017-09-10 12:32:49 +0200 |
commit | 090a2703703877bd150aae637031c5d7dcba2df4 (patch) | |
tree | 3d498820503e1ab3c7186c2839a661a76083550b /src/ee/digikey | |
parent | c96bc5755a68d8e317d96be31b330cc0b626c194 (diff) | |
download | ee-python-090a2703703877bd150aae637031c5d7dcba2df4.tar.gz ee-python-090a2703703877bd150aae637031c5d7dcba2df4.tar.bz2 ee-python-090a2703703877bd150aae637031c5d7dcba2df4.tar.xz ee-python-090a2703703877bd150aae637031c5d7dcba2df4.zip |
setup.py: Adding install_requires.
digikey: Updating tests. Making sure the directory exist before writing facts.
Stop recursing into new searches when a search returns multiple hits. Let the frontends do that.
Diffstat (limited to 'src/ee/digikey')
-rw-r--r-- | src/ee/digikey/__init__.py | 36 |
1 files changed, 18 insertions, 18 deletions
diff --git a/src/ee/digikey/__init__.py b/src/ee/digikey/__init__.py index bd9a86b..64d8943 100644 --- a/src/ee/digikey/__init__.py +++ b/src/ee/digikey/__init__.py @@ -66,11 +66,11 @@ class Digikey(object): @total_ordering class DigikeyProduct(object): - def __init__(self, part_number, mpn, attributes, categories): + def __init__(self, part_number, mpn, attributes=None, categories=None): self.part_number = _clean(part_number) self.mpn = _clean(mpn) - self.attributes = attributes - self.categories = categories + self.attributes = attributes or [] + self.categories = categories or [] self.quantity_available = None self.description = None @@ -152,10 +152,10 @@ class DigikeySearchResponse(object): self.count = count self.response_type = response_type - self.products = set() + self.products = list() def append(self, product): - self.products.add(product) + self.products.append(product) class DigikeyClient(object): @@ -173,7 +173,7 @@ class DigikeyClient(object): # self.sess.mount('http://', adapter) # self.sess.mount('https://', adapter) - def req(self, url, params=None): + def _req(self, url, params=None): if not url.startswith("http"): url = "https://www.digikey.com" + url s = "" if not params else "?" + urllib.parse.urlencode(params) @@ -221,24 +221,24 @@ class DigikeyClient(object): return None def _search_process_multiple_results(self, tree: html, res: DigikeySearchResponse): - product_ids = [e.get("content").strip().replace('sku:', '') for e in - tree.xpath("//*[@itemprop='productid' and @content]")] + products = tree.xpath("//*[@itemtype='http://schema.org/Product']") - for product_id in product_ids: - tmp = self.search(product_id) - if isinstance(tmp, DigikeyProduct): - res.append(tmp) - else: - [res.append(p) for p in tmp.products] + for product in products: + part_number = _first(product.xpath("//*[@itemprop='productid' and @content]")) + mpn = _first(product.xpath("//*[@itemprop='name']")) - return len(product_ids) + if part_number is not None and mpn is not None: + res.append(DigikeyProduct( + part_number.get("content").strip().replace('sku:', ''), + mpn.text)) - def search(self, query: str) -> DigikeySearchResponse: - page_size = 10 + return len(products) + def search(self, query: str, page_size=10) -> DigikeySearchResponse: # http://www.digikey.com/products/en?x=0&y=0&lang=en&site=us&keywords=553-2320-1-ND + params = {'lang': 'en', 'site': 'us', 'keywords': query, 'pageSize': str(page_size), 'x': 0, 'y': 0} params = {'lang': 'en', 'site': 'us', 'keywords': query, 'pageSize': str(page_size)} - page = self.req("https://www.digikey.com/products/en", params=params) + page = self._req("https://www.digikey.com/products/en", params=params) # print("page: ") # print(page.content) |