diff options
Diffstat (limited to 'octopart/part_search.py')
-rw-r--r-- | octopart/part_search.py | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/octopart/part_search.py b/octopart/part_search.py new file mode 100644 index 0000000..438cb67 --- /dev/null +++ b/octopart/part_search.py @@ -0,0 +1,83 @@ +from octopart import enum +import octopart + +def part_search(q, start = 0, limit = False, fields={}, include=[]): + p = {'q': q, 'start': 0} + if limit: + p['limit'] = limit + + for field, data in fields.iteritems(): + if data is not None and data['q']: + p['filter[fields][' + field + '][]'] = data['q'] +# key = 'filter[fields][' + field + '][]' +# if not key in p: +# p[key] = arr = [] +# else: +# arr = p[key] +# arr.append(data['q']) + pass + + if len(include) > 0: + p['include[]'] = include + + +# p['spec_drilldown[include]'] = 'true' + + json = octopart.get('/parts/search', p) + return octopart.SearchResponse(json) + +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 + + |