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 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'src/ee/digikey/__init__.py') 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']") -- cgit v1.2.3