aboutsummaryrefslogtreecommitdiffstats
path: root/octopart.py
blob: e99295133a5ef3579d1e69f41f71b9c6b970e61f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

import json
import urllib
from decimal import Decimal

from settings import *

def fetch_bom(bom):
    # inspired by "bom_quote.py" provided at http://octopart.com/api
    reply = dict()
    queries = []
    for p in bom:
        pid = "%s|%s" % p
        queries.append({'mpn': p[1],
                        'brand': p[0],
                        'reference': pid})
        reply[pid] = None

    # do requests in batches of 20
    results = []
    for i in range(0, len(queries), OCTOPART_BATCH_SIZE):
        batched_queries = queries[i:i+OCTOPART_BATCH_SIZE]
        url = 'http://octopart.com/api/v3/parts/match?queries=%s' \
            % urllib.quote(json.dumps(batched_queries))
        url += '&apikey=%s' % OCTOPART_API_KEY
        #print url
        data = urllib.urlopen(url).read()
        response = json.loads(data)
        results.extend(response['results'])
   
    #print "len(results): %d" % len(results)

    for result in results:
        pid = result['reference']
        #print "len(items[%s]): %d" % (pid, len(result['items']))
        if len(result['items']) == 0:
            reply[pid] = None
        else:
            reply[pid] = result['items'][0]
    return reply

def price_info(item, quantity=1000):
    if not item:
        return dict(url=None, css='notfound', price='Not Found')
    info = dict(url=item['octopart_url'])
    info['css'] = 'unavailable'
    info['price'] = 'No Offers'
    for offer in item['offers']:
        if not offer['is_authorized']:
            continue
        if not offer['prices'].has_key('USD'):
            continue
        price = None
        for price_pair in offer['prices']['USD']:
            if price_pair[0] <= quantity:
                if not price or price_pair[1] < price:
                    price = price_pair[1]
        if not price:
            print "WARNING: not a price: %s" % price
            continue
        if not info['price'] or price < info['price']:
            info['price'] = Decimal(price)
            if offer['in_stock_quantity'] > 0:
                info['css'] = 'available'
            else:
                info['css'] = 'outofstock'
    return info

def url_info(item):
    if item:
        return item['octopart_url']
    else:
        return None