-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfetchAlts.js
262 lines (247 loc) · 7.86 KB
/
fetchAlts.js
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
const DataPoint = require('data-point');
const XmlToJson = require('xml2js').parseString;
const APIS = ['macys', 'jcpenney', 'walmart', 'supermarket'];
const KEYS = {
macys: '9m5dm82faq6s4t76sqxvd9k3',
walmart: { apiKey: '5rjw69f2qeuvmcyr94tptvsd', lsPublisherId: 'li*LWdY/zkM' },
supermarket: 'be0c879624'
};
const LOCATION = { city: 'New York', state: 'NY' };
const HABITS = ["premium", "alcohol", "vgaming", "fdout", "tobacco"];
function genMapKeys() {
let json = {};
for (let api of APIS)
json[api] = '$..locals.apis[' + api + '] | control:' + api
return { mapKeys: json };
}
function injectControlEntities(source) {
for (let api of APIS) {
source['control:' + api] = {
select: [{
case: '$use',
do: 'transform:' + api
},{
default: () => null
}]
}
}
return source;
}
function genLocals(map) {
let output = {};
for (let api of APIS) {
output[api] = {
id: api,
use: !!map[api]
};
}
return output;
}
function attrToApis(attributes) {
var map = {};
if (~attributes.indexOf('grocery')) {
map['walmart'] = true;
map['supermarket'] = true;
}
if (~attributes.indexOf('clothing')) {
map['macys'] = true;
map['walmart'] = true;
map['jcpenney'] = true;
}
if (~attributes.indexOf('travel')) {
//TODO: Travel
}
return map;
}
function xmlRedux(acc) {
return new Promise(function(resolve,reject) {
XmlToJson(acc.value, (err, result) => {
if (err) reject(err);
resolve(result);
});
});
}
const dataPoint = DataPoint.create({
entities: injectControlEntities({
'hash:GetApis': genMapKeys(),
'transform:xml': xmlRedux,
'transform:macys': ['request:macys', 'transform:normalizeMacys'],
'request:macys': {
url: 'https://api.macys.com/v4/catalog/search?searchphrase={locals.item}',
options: { headers: { 'x-macys-webservice-client-id': KEYS.macys } }
},
'transform:normalizeMacys': (acc) => {
let products = acc.value.searchresultgroups[0].products.product;
products.forEach(product => {
if (product.finalPrice) product.actualCost = product.finalPrice.finalPrice;
else if (product.price.sale) product.actualCost = product.price.sale.value;
else if (product.price.current) product.actualCost = product.price.current.value;
else product.actualCost = product.price.regular.value;
});
products = products.filter(product => product.summary.available && product.actualCost < acc.locals.priceMax);
return products.map(product => { return {
type: 'cheaper',
link: product.summary.producturl,
name: product.summary.name,
company: 'Macy\'s',
price: product.actualCost,
image: product.image[0].imageurl,
}});
},
'transform:jcpenney': ['request:jcpenney', 'transform:normalizeJcpenny'],
'request:jcpenney': {
url: 'http://api.jcpenney.com/v2/search?q={locals.item}&page=1',
options: { headers: { 'X-Currency': 'USD' } }
},
'transform:normalizeJcpenny': (acc) => {
let products = acc.value.products;
products.forEach(product => {
product.actualCost = Infinity;
for (let price of product.prices) {
let avgPrice = (price.max + price.min)/2;
product.actualCost = Math.min(product.actualCost, avgPrice);
}
});
products = products.filter(product => product.actualCost < acc.locals.priceMax);
return products.map(product => { return {
type: 'cheaper',
link: product.links[0].href,
name: product.name,
company: 'JCPenney',
price: product.actualCost,
image: product.images[0].url
}});
},
'transform:walmart': ['request:walmart', 'transform:normalizeWalmart'],
'request:walmart': {
url: "http://api.walmartlabs.com/v1/search?query={locals.item}",
options: { qs: {
apiKey: KEYS.walmart.apiKey,
lsPublisherId: KEYS.walmart.lsPublisherId,
}}
},
'transform:normalizeWalmart': (acc) => {
let products = acc.value.items;
products = products.filter(product => product.salePrice < acc.locals.priceMax);
return products.map(product => { return {
type: 'cheaper',
link: product.productUrl,
name: product.name,
company: 'Walmart',
price: product.salePrice,
image: product.thumbnailImage
}});
},
'transform:supermarket': [
'request:supermarketStores', 'transform:xml', 'transform:getStores',
'request:supermarketSearch[]', 'transform:xml[]', 'transform:normalizeSupermarket'
],
'request:supermarketStores': {
url: "http://www.supermarketapi.com/api.asmx/StoresByCityState",
options: { qs: {
APIKEY: KEYS.supermarket,
SelectedState: LOCATION.state,
SelectedCity: LOCATION.city
}}
},
'transform:getStores': (acc) => {
let stores = acc.value.ArrayOfStore.Store;
stores = stores.filter((item, pos) => {
return stores.findIndex(x => x.Storename[0] == item.Storename[0]) == pos;
}).slice(0, 1);
acc.locals.addressMap = {};
let storeIds = [];
for (let store of stores) {
const id = store.StoreId[0];
storeIds.push(id);
const name = store.Storename[0];
let address = '';
address += store.Address[0] + ',\n';
address += store.City[0] + ', ' + store.State;
acc.locals.addressMap[id] = { name: name, address: address };
}
return storeIds;
},
'request:supermarketSearch': {
url: "http://www.supermarketapi.com/api.asmx/COMMERCIAL_SearchForItem?StoreId={value}&ItemName={locals.item}",
options: { qs: {
APIKEY: KEYS.supermarket,
} },
after: (acc) => {
return acc.value.replace(/<Product_Commercial>/g, '<Product_Commercial><StoreID>' + acc.initialValue + '</StoreID>')
}
},
'transform:normalizeSupermarket': (acc) => {
let products = [];
for (var store of acc.value)
products = products.concat(store.ArrayOfProduct_Commercial.Product_Commercial);
products.forEach(product => {
const storeInfo = acc.locals.addressMap[product.StoreID[0]];
product.storeName = storeInfo.name.trim();
product.address = storeInfo.address;
product.price = Number(product.Pricing[0]);
});
products = products.filter(product => product.price < acc.locals.priceMax);
return products.map(product => { return {
type: 'localcheaper',
name: product.Itemname[0],
company: product.storeName,
image: product.ItemImage[0],
price: product.price,
address: product.address
}});
}
})
})
let proc = (input) => {
if (input.attributes.some(n => !!~HABITS.indexOf(n))) {
return new Promise(done => {
done({
item: input.item,
price: input.price,
type: 'habit'
//image:
});
});
}
return dataPoint.transform('hash:GetApis', {}, {
locals: {
apis: genLocals(attrToApis(input.attributes)),
priceMax: input.price,
item: input.item
}
}).then(acc => {
let finalData = [];
for (let store of Object.keys(acc.value)) {
if (acc.value[store]) finalData = finalData.concat(acc.value[store]);
}
finalData.sort((a, b) => a.price - b.price);
return new Promise(done => {
done(finalData.length === 0 ? undefined : {
item: input.item,
price: input.price,
type: finalData[0].type,
substitutes: finalData
});
});
});
};
let multiproc = (inputs) => {
return Promise.all(inputs.map(input => proc(input)));
};
/* EXAMPLE
multiproc([{
item: 'Whole Milk',
price: 2.79,
attributes: ['grocery']
},{
item: 'Black Bralette',
price: 10,
attributes: ['clothing']
},{
item: 'Beer',
price: 20,
attributes: ['alcohol']
}]).then(x => console.dir(x, { depth: null }));
*/
module.exports = multiproc;