aboutsummaryrefslogtreecommitdiff
path: root/src/ee/digikey
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2017-10-02 16:32:42 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2017-10-02 16:32:42 +0200
commitb735e67fef864b675d86a33eb8409d379bd62307 (patch)
treecf85cdcaf72dfacfa098798940b01eb7f406717b /src/ee/digikey
parent2ace57ab4fb78ce72798afd3da11c91bc3098e0d (diff)
downloadee-python-b735e67fef864b675d86a33eb8409d379bd62307.tar.gz
ee-python-b735e67fef864b675d86a33eb8409d379bd62307.tar.bz2
ee-python-b735e67fef864b675d86a33eb8409d379bd62307.tar.xz
ee-python-b735e67fef864b675d86a33eb8409d379bd62307.zip
o Sorting products before downloading to be consistent across runs.
o Storing the URL for each product.
Diffstat (limited to 'src/ee/digikey')
-rw-r--r--src/ee/digikey/__init__.py23
1 files changed, 19 insertions, 4 deletions
diff --git a/src/ee/digikey/__init__.py b/src/ee/digikey/__init__.py
index 934c0d2..957aa40 100644
--- a/src/ee/digikey/__init__.py
+++ b/src/ee/digikey/__init__.py
@@ -71,9 +71,10 @@ class Digikey(object):
@total_ordering
class DigikeyProduct(object):
- def __init__(self, part_number, mpn, attributes=None, categories=None):
+ def __init__(self, part_number, mpn, url, attributes=None, categories=None):
self.part_number = _clean(part_number)
self.mpn = _clean(mpn)
+ self.url = url
self.attributes = attributes or []
self.categories = categories or []
self.quantity_available = None
@@ -95,9 +96,14 @@ class DigikeyProduct(object):
return next((a for a in self.attributes if a.attribute_type.id == _id), None)
def to_ini(self, c: configparser.ConfigParser):
+ def set(cfg, key, value):
+ if value:
+ cfg[key] = value
+
c["overview"] = {};
overview = c["overview"]
overview["part_number"] = self.part_number
+ set(overview, "url", self.url)
if self.mpn:
overview["mpn"] = self.mpn
c["attributes"] = {};
@@ -113,13 +119,19 @@ class DigikeyProduct(object):
@staticmethod
def from_ini(digikey, c):
+ def get(c, key):
+ try:
+ return c[key]
+ except KeyError:
+ return None
+
overview = c["overview"]
attributes = []
for k, value in c.items("attributes"):
(type_id, label) = DigikeyProduct.from_ini_r.match(k).groups()
a_type = digikey.get_attribute_type(int(type_id), label)
attributes.append(DigikeyAttributeValue(value, a_type))
- return DigikeyProduct(overview["part_number"], overview["mpn"], attributes)
+ return DigikeyProduct(overview["part_number"], overview["mpn"], get(overview, "url"), attributes)
class DigikeyAttributeType(object):
@@ -211,6 +223,8 @@ class DigikeyClient(object):
attributes = []
categories = []
+ url = _first((link.get("href") for link in tree.xpath("/html/head/link[@rel='canonical' and @href]")))
+
part_number = mpn = None
for n in tree.xpath("//*[@itemprop='productID' and @content]"):
part_number = n.get("content")
@@ -241,7 +255,7 @@ class DigikeyClient(object):
attributes.append(DigikeyAttributeValue(value, a_type))
if part_number and mpn:
- p = DigikeyProduct(part_number, mpn, attributes, categories)
+ p = DigikeyProduct(part_number, mpn, url, attributes, categories)
for n in tree.xpath("//*[@itemprop='description']"):
p.description = _to_string(n)
return p
@@ -252,13 +266,14 @@ class DigikeyClient(object):
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]")))
part_number = _first(product.xpath(".//*[@itemprop='productid' and @content]"))
mpn = _first(product.xpath(".//*[@itemprop='name']"))
if part_number is not None and mpn is not None:
res.append(DigikeyProduct(
part_number.get("content").strip().replace('sku:', ''),
- mpn.text))
+ mpn.text, url))
return len(products)