-
Notifications
You must be signed in to change notification settings - Fork 14
/
octopart.gs
288 lines (216 loc) · 8.17 KB
/
octopart.gs
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
function toBytes(str) {
return Utilities.newBlob(str).getBytes();
}
function Octopart() {
this._api = "http://octopart.com/api/v3";
this._apikey = "a76a384c";
this.request = function (endpoint, args, includes) {
var url = this._api + endpoint + "?apikey=" + this._apikey;
// add userkey if available
var user_props = PropertiesService.getUserProperties();
var user_key = user_props.getProperty("user_key");
if (user_key)
url += "&userkey=" + encodeURIComponent(JSON.stringify(user_key));
for (var key in args)
url += "&" + key + "=" + encodeURIComponent(JSON.stringify(args[key]));
if (includes) {
for (var i = 0; i < includes.length; i++)
url += "&" + includes[i];
}
// we cache requests publicaly so all users will be able to benefit from it.
var cache = CacheService.getPublicCache();
var cached = cache.get(url);
if (cached != null)
return JSON.parse(cached);
var lock = LockService.getPublicLock();
var locked = lock.tryLock(30000);
if (!locked)
throw "too many concurrent users";
var response = UrlFetchApp.fetch(url, {method: 'get', muteHttpExceptions: true});
// rate limiter?
if (response.getResponseCode() == 429) {
// wait and try again, we hold the lock, so everyone is waiting, be quick!
Utilities.sleep(1000);
response = UrlFetchApp.fetch(url, {method: 'get', muteHttpExceptions: true});
}
if (response.getResponseCode() != 200) {
lock.releaseLock();
throw "something wrong... (HTTP " + response.getResponseCode() + ")";
}
cache.put(url, response, 60 * 60);
lock.releaseLock();
Utilities.sleep(400);
return JSON.parse(response.getContentText());
};
this.match = function(mpn_or_sku, manuf, includes) {
if (typeof mpn_or_sku === "undefined")
throw "mpn or sku is required";
manuf = typeof manuf !== "undefined"? manuf: null;
var args = {};
args.queries = [{"mpn_or_sku": String(mpn_or_sku)}];
if (manuf)
args.queries[0]["brand"] = String(manuf);
return new PartsMatchResponse(this.request("/parts/match", args, includes));
};
this.upload = function() {
var url = "https://octopart.com/bom-lookup/upload";
var boundary = "15b909e62efccd31a1b0098eae5dba3db361743f";
var boundaryFull = "\r\n--" + boundary + "\r\n";
// start multipart bounday
var payload = toBytes(boundaryFull);
payload = payload.concat(toBytes("Content-Disposition: form-data; name=\"datafile\"; filename=\"filename.csv\"\r\n\r\n"));
// spreadsheet data
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getDataRange();
var data = range.getValues();
for (var i = 0; i < data.length; i++)
payload = payload.concat(toBytes(data[i].toString() + "\r\n"));
// end multipart boundary
payload = payload.concat(toBytes(boundaryFull));
var options = {
method: 'post',
payload: payload,
contentType: "multipart/form-data; boundary=" + boundary,
followRedirects: false,
muteHttpExceptions: true
};
var response = UrlFetchApp.fetch(url, options);
if (response.getResponseCode() == 302)
return response.getHeaders()["Location"];
else
throw "There was an error while uploading your BOM. Please try again later.";
};
}
function PartsMatchResponse(response) {
this._response = response;
this.getResult = function(index) {
return new PartsMatchResult(this._response.results[index]);
}
};
function PartsMatchResult(result) {
this._result = result;
this.getPart = function(index) {
if (index >= this._result.hits)
throw "No parts found.";
return new Part(this._result.items[index]);
}
};
function Part(part) {
this._part = part;
this.getAveragePrice = function(qty, currency) {
var sum = 0;
var sellers = 0;
qty = typeof qty !== "undefined"? qty: 1;
currency = typeof currency !== "undefined"? currency: "USD";
for (var i = 0; i < this._part.offers.length; i++) {
var offer = new PartOffer(this._part.offers[i]);
var price = offer.getPrice(qty, currency);
if (!isNaN(price)) {
sum += price;
sellers += 1;
}
}
return sellers > 0? sum/sellers: 0;
}
this.getOffer = function(distributor, qty, currency) {
qty = typeof qty !== "undefined"? qty: 1;
currency = typeof currency !== "undefined"? currency: "USD";
if (distributor) {
for (var i = 0; i < this._part.offers.length; i++) {
var offer = new PartOffer(this._part.offers[i]);
if (offer.hasPriceInCurrency(currency) && offer.getSellerName() == distributor)
return offer;
}
} else {
var lowest_offer = null;
for (var i = 0; i < this._part.offers.length; i++) {
var new_offer = new PartOffer(this._part.offers[i]);
if (!lowest_offer && new_offer.hasPriceInCurrency(currency)) {
var new_offer_price = new_offer.getPrice(qty, currency);
if (!isNaN(new_offer_price))
lowest_offer = new_offer;
} else {
var new_offer_price = new_offer.getPrice(qty, currency);
if (!isNaN(new_offer_price))
lowest_offer = new_offer_price < lowest_offer.getPrice(qty, currency)? new_offer: lowest_offer;
}
}
if (lowest_offer != null)
return lowest_offer;
else
throw "No offers found.";
}
throw "No offers found.";
};
this.getOctopartUrl = function() {
return this._part.octopart_url;
}
this.getDescription = function() {
return this._part.short_description || '(no description retrieved)';
}
this.getDatasheetUrl = function(index) {
if (!"datasheets" in this._part || this._part.datasheets.length <= index)
throw "No datasheets found.";
return this._part.datasheets[0].url;
}
this.getImageUrl = function(size) {
size = typeof size !== "undefined"? size: "small";
for(var i = 0; i < this._part.imagesets.length; i++) {
if (size === "large" && this._part.imagesets[i].large_image !== null)
return this._part.imagesets[i].large_image.url;
if (size === "medium" && this._part.imagesets[i].medium_image !== null)
return this._part.imagesets[i].medium_image.url;
if (size === "small" && this._part.imagesets[i].small_image !== null)
return this._part.imagesets[i].small_image.url;
if (size === "swatch" && this._part.imagesets[i].swatch_image !== null)
return this._part.imagesets[i].swatch_image.url;
}
throw "No image found."
}
};
function PartOffer(offer) {
this._offer = offer;
this.getPrice = function(qty, currency) {
qty = typeof qty !== "undefined"? qty: 1;
currency = typeof currency !== "undefined"? currency: "USD";
if (!this.hasPriceInCurrency(currency))
return NaN;
var price = NaN;
for (var i = 0; i < this._offer.prices[currency].length; i++) {
if (this._offer.prices[currency][i][0] > qty)
return parseFloat(price);
price = this._offer.prices[currency][i][1];
}
if (isNaN(price))
throw "No price found for this quantity/currency";
return parseFloat(price);
}
this.hasPriceInCurrency = function(currency) {
currency = typeof currency !== "undefined"? currency: "USD";
return currency in this._offer.prices;
}
this.getInStockQuantity = function() {
return this._offer.in_stock_quantity !== null? parseInt(this._offer.in_stock_quantity): "unknown";
}
this.getSellerName = function() {
return this._offer.seller.name;
}
this.getSellerUrl = function() {
return this._offer.product_url;
}
this.getMoq = function() {
return this._offer.moq !== null? parseInt(this._offer.moq): "unknown";
}
this.getPackaging = function() {
return this._offer.packaging !== null? this._offer.packaging: "unknown";
}
this.getFactoryLeadTime = function() {
return this._offer.factory_lead_days !== null? parseInt(this._offer.factory_lead_days): "unknown";
}
this.getOrderMultiple = function() {
return this._offer.order_multiple !== null? parseInt(this._offer.order_multiple): "unknown";
}
this.getSku = function() {
return this._offer.sku !== null? this._offer.sku: "unknown";
}
};