diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2015-09-25 16:52:19 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2015-09-25 16:52:19 +0200 |
commit | e91f02df33da10e7f18ac5c582f08134e9bea3c8 (patch) | |
tree | f64c120c3f44251b481630b252e487012d664bab | |
parent | a75ab11f03a45bc5f5e82b079d79b2cf423ac47c (diff) | |
download | octopart-stuff-e91f02df33da10e7f18ac5c582f08134e9bea3c8.tar.gz octopart-stuff-e91f02df33da10e7f18ac5c582f08134e9bea3c8.tar.bz2 octopart-stuff-e91f02df33da10e7f18ac5c582f08134e9bea3c8.tar.xz octopart-stuff-e91f02df33da10e7f18ac5c582f08134e9bea3c8.zip |
wip
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | category_search_cmd.py (renamed from category_search.py) | 0 | ||||
-rw-r--r-- | octopart/__init__.py | 2 | ||||
-rw-r--r-- | octopart/core.py (renamed from octopart.py) | 34 | ||||
-rw-r--r-- | octopart/part_search.py (renamed from part_search.py) | 52 | ||||
-rw-r--r-- | part_search_cmd.py | 24 |
6 files changed, 61 insertions, 52 deletions
@@ -1,2 +1,3 @@ *.sqlite *.pyc +venv diff --git a/category_search.py b/category_search_cmd.py index 680df09..680df09 100644 --- a/category_search.py +++ b/category_search_cmd.py diff --git a/octopart/__init__.py b/octopart/__init__.py new file mode 100644 index 0000000..798bcd7 --- /dev/null +++ b/octopart/__init__.py @@ -0,0 +1,2 @@ +from core import * +from part_search import * diff --git a/octopart.py b/octopart/core.py index 1d7527b..8278b29 100644 --- a/octopart.py +++ b/octopart/core.py @@ -7,9 +7,15 @@ import sys reload(sys) sys.setdefaultencoding("utf-8") +requests_cache.install_cache(cache_name='http_cache', backend='sqlite', expire_after=180) + apikey = '2452f140' base_url = "http://octopart.com/api/v3" +def enum(*sequential, **named): + enums = dict(zip(sequential, range(len(sequential))), **named) + return type('Enum', (), enums) + def extract_int(json, key): try: val = json[key] @@ -139,29 +145,5 @@ def category_search(q): categories.append(Category(item)) return categories -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 = get('/parts/search', p) - return SearchResponse(json) - -requests_cache.install_cache(cache_name='http_cache', backend='sqlite', expire_after=180) +# ############################################################################# +# Part Search diff --git a/part_search.py b/octopart/part_search.py index faa3f03..438cb67 100644 --- a/part_search.py +++ b/octopart/part_search.py @@ -1,8 +1,30 @@ +from octopart import enum import octopart -def enum(*sequential, **named): - enums = dict(zip(sequential, range(len(sequential))), **named) - return type('Enum', (), enums) +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') @@ -58,26 +80,4 @@ def resistor_search(search): 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 + diff --git a/part_search_cmd.py b/part_search_cmd.py new file mode 100644 index 0000000..76f2c28 --- /dev/null +++ b/part_search_cmd.py @@ -0,0 +1,24 @@ +import octopart + +search = octopart.ResistorSearch() +search['resistance'] = 1000 +search['package_type'] = octopart.PackageType.smd +search['case'] = '1206' +search['seller'] = 'farnell' + +search_response = octopart.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 |