diff options
Diffstat (limited to 'octopart/core.py')
-rw-r--r-- | octopart/core.py | 46 |
1 files changed, 31 insertions, 15 deletions
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 |