diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-09-25 15:17:05 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-09-25 15:17:05 +0200 |
commit | a75ab11f03a45bc5f5e82b079d79b2cf423ac47c (patch) | |
tree | f4fda38928800a25797c4ede96aed53677e4282a /part_search.py | |
download | octopart-stuff-a75ab11f03a45bc5f5e82b079d79b2cf423ac47c.tar.gz octopart-stuff-a75ab11f03a45bc5f5e82b079d79b2cf423ac47c.tar.bz2 octopart-stuff-a75ab11f03a45bc5f5e82b079d79b2cf423ac47c.tar.xz octopart-stuff-a75ab11f03a45bc5f5e82b079d79b2cf423ac47c.zip |
wip
Diffstat (limited to 'part_search.py')
-rw-r--r-- | part_search.py | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/part_search.py b/part_search.py new file mode 100644 index 0000000..faa3f03 --- /dev/null +++ b/part_search.py @@ -0,0 +1,83 @@ +import octopart + +def enum(*sequential, **named): + enums = dict(zip(sequential, range(len(sequential))), **named) + return type('Enum', (), enums) + +PackageType = enum('through_hole', 'smd') + +class ResistorSearch(): + def __init__(self): + self.params = {} + self.fields = ['package_type', 'case', 'resistance', 'tolerance', 'seller'] + + def has_key(self, key): + try: + self.fields.index(key) + except ValueError: + raise Exception('Invalid key for search: ' + key) + + def __setitem__(self, key, item): + self.has_key(key) + self.params[key] = item + + def __getitem__(self, key): + self.has_key(key) + return self.params[key] + +# lifecycle_status +# specs.resistance_tolerance.value +def resistor_search(search): + q = '' + + categories = octopart.category_search('Resistors') + categories = filter(lambda c: c.name == 'Resistors', categories) + if len(categories) == 0: + raise Exception("Could not find category for resistors") + category = categories[0] + + fields = {} +# 'specs.case_package.value': {'q': '1206'}, + fields['category_uids'] = {'q': category.uid} + fields['specs.resistance.value'] = {'q': search['resistance']} + + # case_package is broken (always returns 0 hits), but it is usually in the description + if search['case'] is not None: + q += ' ' + search['case'] + +# if package_type == PackageType.smd: +# fields['specs.case_package.value'] = {'q': '1210'} + + seller = search['seller'] + if seller is not None: + sellers = octopart.seller_search(search['seller']) + if len(sellers) == 0: + raise Exception('Could not find seller: ' + seller) + fields['offers.seller.name'] = {'q': sellers[0].name} + + res = octopart.part_search(q, limit=100, fields=fields) + return res + +#search_response = resistor_search(1000, PackageType.smd, '1206') +search = ResistorSearch() +search['resistance'] = 1000 +search['package_type'] = PackageType.smd +search['case'] = '1206' +search['seller'] = 'farnell' + +search_response = resistor_search(search) + +print "Hits: {}".format(search_response.hits) + +for search_result in search_response.results: + p = search_result.item + + print "{}: Snippet: {}".format(p.uid, search_result.snippet) +# print "Uid: {}".format(p.uid) +# print "Offers: " +# for o in p.offers: +# print " SKU: {}".format(o.sku) +# for (currency, prices) in o.prices.iteritems(): +# for price in prices: +# print " {}: {}@{}".format(currency, price.amount, price.quantity) +# pass |