From e30b81a822d56a47e7b6ebb618a746534b9d9992 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Mon, 28 Sep 2015 08:27:09 +0200 Subject: wip --- octopart/core.py | 46 ++++++++++++++++++++++------------ octopart/part_search.py | 65 ++++++++++++++++++++++++++++++++----------------- 2 files changed, 74 insertions(+), 37 deletions(-) (limited to 'octopart') diff --git a/octopart/core.py b/octopart/core.py index 8278b29..fd0cc75 100644 --- a/octopart/core.py +++ b/octopart/core.py @@ -34,7 +34,7 @@ def extract_date(json, key): except KeyError: return None -class Category(): +class Category(object): def __init__(self, json): self.uid = json['uid'] self.name = json['name'] @@ -48,16 +48,19 @@ def map_price(json): print 'price: ' + str(json) return json -class Price(): +class Price(object): def __init__(self, currency, quantity, amount): self.currency = currency self.quantity = quantity self.amount = amount -class PartOffer(): + def __str__(self): + return str(self.quantity) + '=' + str(self.amount) + '@' + self.currency + +class PartOffer(object): def __init__(self, json): self.sku = json['sku'] -# self.seller = Seller(json['seller']) + self.seller = Seller(json['seller']) self.eligible_region = json['eligible_region'] self.product_url = json['product_url'] self.octopart_rfq_url = json['octopart_rfq_url'] @@ -76,15 +79,15 @@ class PartOffer(): self.moq = extract_int(json, 'moq') self.last_updated = extract_date(json, 'last_updated') -class Part(): +class Part(object): def __init__(self, json): self.uid = json['uid'] self.mpn = json['mpn'] + self.octopart_url = json['octopart_url'] self.offers = map(PartOffer, json['offers']) - -class SearchResult(): +class SearchResult(object): def __init__(self, json): self.item = Part(json['item']) @@ -92,17 +95,32 @@ class SearchResult(): self.snippet = json['snippet'] pass -class SearchResponse(): +class SearchResponse(object): def __init__(self, json): self.results = map(SearchResult, json['results']) - self.hits = json['hits'] + self.hits = int(json['hits']) + + def filter_seller(self, seller): + for r in self.results: + os = filter(lambda o: o.seller.name == seller, r.item.offers) + if len(os) > 0: + r.item.offers = os + else: + r.item.offers = [] + + @staticmethod + def empty(): + return SearchResponse({'results': [], 'hits': '0'}) def params(p): p['apikey'] = apikey return p -def get(path, p): - url = base_url + path +def get(path, p = {}): + if not path.startswith('http'): + url = base_url + path + else: + url = path # print('path: {}, params: {}'.format(path, p)) print('path: {}'.format(path)) for k, v in p.iteritems(): @@ -114,9 +132,10 @@ def get(path, p): print(json.dumps(j, indent=2, sort_keys=True)) return j -class Seller(): +class Seller(object): def __init__(self, json): self.uid = json['uid'] + self.id = json['id'] self.name = json['name'] def seller_search_raw(q): @@ -144,6 +163,3 @@ def category_search(q): item = result['item'] categories.append(Category(item)) return categories - -# ############################################################################# -# Part Search diff --git a/octopart/part_search.py b/octopart/part_search.py index 438cb67..08834fe 100644 --- a/octopart/part_search.py +++ b/octopart/part_search.py @@ -1,7 +1,7 @@ from octopart import enum import octopart -def part_search(q, start = 0, limit = False, fields={}, include=[]): +def part_search(q, start = 0, limit = False, fields={}, includes=[]): p = {'q': q, 'start': 0} if limit: p['limit'] = limit @@ -17,9 +17,8 @@ def part_search(q, start = 0, limit = False, fields={}, include=[]): # arr.append(data['q']) pass - if len(include) > 0: - p['include[]'] = include - + if len(includes) > 0: + p['include[]'] = includes # p['spec_drilldown[include]'] = 'true' @@ -31,21 +30,34 @@ PackageType = enum('through_hole', 'smd') class ResistorSearch(): def __init__(self): self.params = {} - self.fields = ['package_type', 'case', 'resistance', 'tolerance', 'seller'] + self.fields = ['package_type', 'case', 'resistance', 'tolerance', 'seller', 'power_rating'] + + def has_value(self, field): + self.assert_has_field(field) + try: + return self.params[field] is not None + except KeyError: + return False - def has_key(self, key): + def has_field(self, field): try: - self.fields.index(key) + self.fields.index(field) + return True except ValueError: - raise Exception('Invalid key for search: ' + key) + return False + + def assert_has_field(self, field): + if not self.has_field(field): + raise Exception('Missing parameter for search: ' + field) - def __setitem__(self, key, item): - self.has_key(key) - self.params[key] = item + def __setitem__(self, field, item): + self.assert_has_field(field) + if field is not None: + self.params[field] = item - def __getitem__(self, key): - self.has_key(key) - return self.params[key] + def __getitem__(self, field): + self.assert_has_field(field) + return self.params[field] # lifecycle_status # specs.resistance_tolerance.value @@ -64,20 +76,29 @@ def resistor_search(search): 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 search.has_value('case'): + q += ' ' + search['case'] + + if search.has_value('power_rating'): + fields['specs.power_rating.value'] = {'q': '[' + str(search['power_rating']) + ' TO 2500]'} # 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']) + resolved_seller = None + if search.has_value('seller'): + seller = search['seller'] + sellers = octopart.seller_search(seller) if len(sellers) == 0: raise Exception('Could not find seller: ' + seller) - fields['offers.seller.name'] = {'q': sellers[0].name} + resolved_seller = sellers[0].name + fields['offers.seller.name'] = {'q': resolved_seller} - res = octopart.part_search(q, limit=100, fields=fields) - return res + includes = [] +# includes.append('specs') + res = octopart.part_search(q, limit=100, fields=fields, includes=includes) + if resolved_seller: + res.filter_seller(resolved_seller) + return res -- cgit v1.2.3