From 689508e07dba890fff8d1cd06e0029eca1c30994 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 23 Mar 2019 07:29:49 +0100 Subject: xsd: o Adding list on . For media, datasheets etc. digikey: o Parsing out media. PartDb: o starting on a more generic object db with indexes. order: o Using the new object db for building data while resolving parts. o Creating a report from the order. --- src/ee/digikey/__init__.py | 41 +++++++++++++++++++++++++++++++++++++++++ src/ee/digikey/search_parts.py | 13 ++++++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) (limited to 'src/ee/digikey') diff --git a/src/ee/digikey/__init__.py b/src/ee/digikey/__init__.py index d0405f1..2fe7443 100644 --- a/src/ee/digikey/__init__.py +++ b/src/ee/digikey/__init__.py @@ -79,6 +79,13 @@ class PriceBreak(object): self.per_quantity_price = per_quantity_price +class Document(object): + def __init__(self, kind: str, title: str, url: str): + self.kind = kind + self.title = title + self.url = url + + @total_ordering class DigikeyProduct(object): def __init__(self, part_number, mpn, url, attributes: List["DigikeyAttributeValue"] = None, categories=None): @@ -90,6 +97,7 @@ class DigikeyProduct(object): self.quantity_available = None self.description = None self.price_breaks: List[PriceBreak] = [] + self.documents: List[Document] = [] assert self.part_number assert self.mpn @@ -303,6 +311,7 @@ class DigikeyParser(object): if part_number and mpn: p = DigikeyProduct(part_number, mpn, url, attributes, categories) p.price_breaks = self._parse_price_breaks(tree) + p.documents = self._parse_documents(tree) for n in tree.xpath("//*[@itemprop='description']"): p.description = _to_string(n) return p @@ -347,6 +356,38 @@ class DigikeyParser(object): return price_breaks if ok else [] + @staticmethod + def _parse_documents(tree: html) -> List[Document]: + 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: + continue + + 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 + title = title.strip() + + href = a.get("href") + if not href: + continue + + href = href.strip() + if href.startswith("//"): + href = "https:" + href + + docs.append(Document(kind, title, href)) + + return docs + @staticmethod def _handle_product_table(tree: html, res: DigikeySearchResponse): products = tree.xpath("//*[@itemtype='http://schema.org/Product']") diff --git a/src/ee/digikey/search_parts.py b/src/ee/digikey/search_parts.py index 3d271d5..358266a 100644 --- a/src/ee/digikey/search_parts.py +++ b/src/ee/digikey/search_parts.py @@ -13,9 +13,19 @@ def resolved(p: DigikeyProduct) -> types.Part: # TODO: fix uri part = types.Part(uri="https://digikey.com/pn#{}".format(p.part_number), distributor_info=types.DistributorInfo(), + links=types.LinkList(), facts=types.FactList(), references=types.ReferencesList()) part.distributor_infoProp.stateProp = "resolved" + links = part.linksProp.link + + if p.url: + links.append(types.Link(url=p.url, relation="canonical", media_type="text/html")) + + for d in p.documents: + title = "{}: {}".format(d.kind, d.title) + links.append(types.Link(url=d.url, relation="http://purl.org/ee/link-relation#documentation", + media_type="text/html", title=title)) supplier_part_numbers = part.referencesProp.supplier_part_numberProp supplier_part_numbers.append(types.SupplierPartNumber(value=p.part_number, supplier=DIGIKEY_URI)) @@ -48,7 +58,8 @@ def search_parts(in_path: Path, out_path: Path, cache_dir: Path): client = DigikeyClient(cache_dir) for part in in_db.iterparts(): - dpn = next((p.valueProp for p in bom_file_utils.supplier_part_numbers(part) if p.supplierProp == DIGIKEY_URI), None) + dpn = next((p.valueProp for p in bom_file_utils.supplier_part_numbers(part) + if p.supplierProp == DIGIKEY_URI), None) mpn = next((p.valueProp for p in bom_file_utils.part_numbers(part)), None) is_mpn = query = None -- cgit v1.2.3