diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2019-03-14 06:27:16 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2019-03-15 08:22:02 +0100 |
commit | 8d17fb5bc4b0dae0758e01a44d77d87acf2e686a (patch) | |
tree | a13ed043962b8a8da355bba956dc9e77b7fb217e /src/ee/element14 | |
parent | 2315d1a34cb777f1731368619a4ca14f46125bb4 (diff) | |
download | ee-python-8d17fb5bc4b0dae0758e01a44d77d87acf2e686a.tar.gz ee-python-8d17fb5bc4b0dae0758e01a44d77d87acf2e686a.tar.bz2 ee-python-8d17fb5bc4b0dae0758e01a44d77d87acf2e686a.tar.xz ee-python-8d17fb5bc4b0dae0758e01a44d77d87acf2e686a.zip |
o Adding module for searching on element14.
o Starting on functionality create orders. Very WIP.
o Adding a concept of an "ee project". Can load a gitconfig-like config
file.
o Adding a tool to import a yaml file into a part xml file.
Diffstat (limited to 'src/ee/element14')
-rw-r--r-- | src/ee/element14/__init__.py | 63 | ||||
-rw-r--r-- | src/ee/element14/search_parts.py | 31 |
2 files changed, 94 insertions, 0 deletions
diff --git a/src/ee/element14/__init__.py b/src/ee/element14/__init__.py new file mode 100644 index 0000000..17a8825 --- /dev/null +++ b/src/ee/element14/__init__.py @@ -0,0 +1,63 @@ +import json +from pathlib import Path +from typing import Optional +from urllib.parse import urlencode +from urllib.request import urlopen + +import ee._utils + +__all__ = [ + "Element14Config", + "Element14Client", +] + + +class Element14Config(object): + def __init__(self, store: Optional[str], api_key: Optional[str]): + self.store = store + self.api_key = api_key + + +class Element14Client(object): + def __init__(self, config: Element14Config, cache_dir: Path): + self.config = config + self.cache = ee._utils.maybe_cache(cache_dir) + + def search(self, term: Optional[str] = None): + url = "https://api.element14.com/catalog/products" + + kv = { + "callInfo.responseDataFormat": "XML", + } + + if self.config.api_key: + kv["callInfo.apiKey"] = self.config.api_key + + if self.config.store: + kv["storeInfo.id"] = self.config.store + + if term: + kv["term"] = term + + kv["resultsSettings.offset"] = "0" + kv["resultsSettings.numberOfResults"] = "100" + + print("params: {}".format(kv)) + + # &resultsSettings.refinements.filters={rohsCompliant,inStock} + # &resultsSettings.responseGroup={none,small,medium,large, Prices, Inventory} + + url = url + "?" + urlencode(kv) + + print("url={}".format(url)) + + data = "wat!!" + data = urlopen(url).read() + + print("-----------") + print(data) + print("-----------") + + search_response = json.loads(data) + + return diff --git a/src/ee/element14/search_parts.py b/src/ee/element14/search_parts.py new file mode 100644 index 0000000..724485c --- /dev/null +++ b/src/ee/element14/search_parts.py @@ -0,0 +1,31 @@ +from pathlib import Path + +from ee.element14 import * +from ee.part import PartDb, load_db, save_db +from ee.xml import bom_file_utils, bomFile + +__all__ = ["search_parts"] + + +def search_parts(in_dir: Path, out_dir: Path, cache_dir: Path, config: Element14Config): + in_db = load_db(in_dir) + out_parts = PartDb() + + client = Element14Client(config, cache_dir) + + for part in in_db.iterparts(): + mpn = bom_file_utils.find_pn(part) + + query = mpn # TODO: suppor dpn + + out_id = query + + client.search(term="manuPartNum:" + query) + + out_part = bomFile.Part(id=out_id, + distributor_info=bomFile.DistributorInfo(), + part_numbers=part.part_numbersProp) + di = out_part.distributor_infoProp + + print("Saving {} work parts".format(out_parts.size())) + save_db(out_dir, out_parts) |