-
Notifications
You must be signed in to change notification settings - Fork 2
/
APICall.js
executable file
·346 lines (258 loc) · 11.8 KB
/
APICall.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
const fetch = require('node-fetch');
const cheerio = require('cheerio');
const PRODUCT_NAME = "name";
const PRODUCT_IMAGE_URL = "image_url";
const PRODUCT_URL = "url";
const PRODUCT_ORIGINAL_PRICE = "original_price";
const PRODUCT_DISCOUNTED_PRICE = "discounted_price";
const PRODUCT_ADDITIONAL_PROMO = "additional_promo";
const PRODUCT_PROMO_EXPIRY = "expiry";
const PRODUCT_STORE = "store";
// Websites
const fairpriceSearchURL = "https://www.fairprice.com.sg/searchterm/";
const coldStorageSearchURL = "https://coldstorage.com.sg/search?q=";
const coldStorageURL = "https://coldstorage.com.sg";
const shengSiongSearchURL = "https://allforyou.sg/search?q=";
const giantSearchURL = "https://giantonline.com.sg/search?q=";
const giantProductURL = "https://giantonline.com.sg/product";
const giantURL = "https://giantonline.com.sg";
async function getPromo() {
const url = "https://khubite.000webhostapp.com/app/sg_supermarket/data_v1.json";
const response = await fetch(url, { method: 'get' });
const jsonData = await response.json();
if (jsonData && jsonData.length > 0) {
// console.log("json " + jsonData)
return jsonData;
} else {
return "Error";
}
};
async function getNTUCProduct(name) {
const productList = [];
const url = fairpriceSearchURL + name;
const response = await fetch(url, { method: 'get' });
const htmlData = await response.text();
const $ = cheerio.load(htmlData);
const productsData = $('.product');
for (let i = 0; i < productsData.length; i += 1) {
const productData = $(productsData[i]);
const product = {};
const productName = productData.find('.pdt_img div a').attr('title');
product[PRODUCT_NAME] = productName;
const offer = productData.find('.offer').text().trim();
const productURL = productData.find('.pdt_name a').attr('href');
const productImgURL = productData.find('.pdt_img img').attr('src');
product[PRODUCT_URL] = productURL;
product[PRODUCT_IMAGE_URL] = productImgURL;
// If there is offer
var isCustomOfferDate = false;
if (offer !== "") {
const specialOffer = productData.find('.pdt_promo a').text();
// console.log(i + ":" + productName + "\t\t" + offer + "--" + specialOffer + "-" + data.find('.pdt_price .pdt_C_price').text().trim())
product[PRODUCT_ADDITIONAL_PROMO] = specialOffer;
const currentPrice = productData.find('.pdt_price .pdt_C_price').text().trim()
product[PRODUCT_DISCOUNTED_PRICE] = currentPrice;
// But no special offer, so get the discount price
if (specialOffer === "") {
isCustomOfferDate = true;
const originalPrice = productData.find('.pdt_price .pdt_O_price').text().trim()
product[PRODUCT_ORIGINAL_PRICE] = originalPrice;
} else {
product[PRODUCT_ORIGINAL_PRICE] = "";
}
if (offer == "PWP")
isCustomOfferDate = false;
// Get expiry date
const expiry = await getNTUCExpiry(productURL, isCustomOfferDate)
product[PRODUCT_PROMO_EXPIRY] = expiry;
productList.push(product);
}
}
return productList;
}
async function getColdStorageProduct(name) {
const productList = [];
const url = coldStorageSearchURL + name;
const response = await fetch(url, { method: 'get' });
const htmlData = await response.text();
const $ = cheerio.load(htmlData);
const productsData = $('.items li');
for (let i = 0; i < productsData.length; i += 1) {
const productData = $(productsData[i]);
const product = {};
const productName = productData.find('.product-link').text();
product[PRODUCT_NAME] = productName;
const offer = productData.find('.product-discount-label').text();
const productURL = productData.find('.product-link').attr('href');
let productImgURL = productData.find('.img img').attr('src');
if (productImgURL.indexOf("http") < 0)
productImgURL = coldStorageURL + productImgURL
product[PRODUCT_URL] = coldStorageURL + productURL;
product[PRODUCT_IMAGE_URL] = productImgURL;
// If there is offer
if (offer !== "") {
product[PRODUCT_ADDITIONAL_PROMO] = offer;
const currentPrice = productData.find('.content_price .product-price').text().trim();
// Special Offer
if (offer.includes("BUY")) {
product[PRODUCT_ORIGINAL_PRICE] = currentPrice;
} else {
product[PRODUCT_DISCOUNTED_PRICE] = currentPrice;
const originalPrice = productData.find('.content_info span').text().trim()
product[PRODUCT_ORIGINAL_PRICE] = originalPrice;
}
product[PRODUCT_PROMO_EXPIRY] = "";
productList.push(product);
}
}
return productList;
}
async function getShengSiongProduct(name) {
const productList = [];
const url = shengSiongSearchURL + name;
const response = await fetch(url, { method: 'get' });
const htmlData = await response.text();
const $ = cheerio.load(htmlData);
const productsData = $('.productbox-span');
for (let i = 0; i < productsData.length; i += 1) {
const productData = $(productsData[i]);
const product = {};
const productName = productData.find('.prodname span').text().trim();
product[PRODUCT_NAME] = productName;
const hiddenData = productData.find('.prod-data');
// var productImgURL = productData.find('#productImageThumb').attr('style');
var productImgURL = hiddenData.attr('data-imgurl');
product[PRODUCT_URL] = encodeURI(shengSiongSearchURL + productName); // Encode the string to URI
product[PRODUCT_IMAGE_URL] = productImgURL;
const hasOffer = hiddenData.attr('data-hasoffers');
// If there is offer
if (hasOffer === "True") {
const offerName = hiddenData.attr('data-offername');
product[PRODUCT_ADDITIONAL_PROMO] = offerName;
const currentPrice = hiddenData.attr('data-price');
product[PRODUCT_DISCOUNTED_PRICE] = currentPrice;
const originalPrice = hiddenData.attr('data-oldprice');
product[PRODUCT_ORIGINAL_PRICE] = originalPrice
product[PRODUCT_PROMO_EXPIRY] = "";
productList.push(product);
}
}
return productList;
}
async function getGiantProduct(name) {
const productList = [];
const url = giantSearchURL + name;
const response = await fetch(url, { method: 'get' });
const htmlData = await response.text();
const $ = cheerio.load(htmlData);
const productsData = $('.items li');
for (let i = 0; i < productsData.length; i += 1) {
const productData = $(productsData[i]);
const product = {};
const productName = productData.find('.product-name a').text();
product[PRODUCT_NAME] = productName;
const offer = productData.find('.product-discount-label').text();
const productURL = productData.attr('data-url');
let productImgURL = productData.find('.img img').attr('src');
if (productImgURL.indexOf("http") < 0)
productImgURL = giantURL + productImgURL
product[PRODUCT_URL] = giantProductURL + productURL;
product[PRODUCT_IMAGE_URL] = productImgURL;
// If there is offer
if (offer !== "") {
product[PRODUCT_ADDITIONAL_PROMO] = offer;
const currentPrice = productData.find('.content_price .product-price').text().trim();
// Special Offer
if (offer.includes("BUY")) {
product[PRODUCT_ORIGINAL_PRICE] = currentPrice;
} else {
product[PRODUCT_DISCOUNTED_PRICE] = currentPrice;
const originalPrice = productData.find('.content_info span').text();
product[PRODUCT_ORIGINAL_PRICE] = originalPrice;
}
product[PRODUCT_PROMO_EXPIRY] = "";
productList.push(product);
}
}
return productList;
}
// async function getNTUCProduct(name, res) {
// const productList = [];
// const url = fairpriceSearchURL + name;
// const response = await fetch(url, { method: 'get' });
// const htmlData = response.text();
// const $ = cheerio.load(body);
// return await fetch(url, {
// method: 'get'
// })
// .then(res => res.text())
// .then(async (body) => {
// const $ = cheerio.load(body);
// // Get each product
// await $('.product').each(async function (i, elem) {
// var data = $(this);
// var product = {};
// var productName = data.find('.pdt_img div a').attr('title');
// product[PRODUCT_NAME] = productName;
// //https://stackoverflow.com/questions/154059/how-do-you-check-for-an-empty-string-in-javascript
// var offer = data.find('.offer').text().trim();
// var productURL = data.find('.pdt_name a').attr('href');
// var productImgURL = data.find('.pdt_img img').attr('src');
// product[PRODUCT_URL] = productURL;
// product[PRODUCT_IMAGE_URL] = productImgURL;
// // If there is offer
// if (offer !== "") {
// var specialOffer = data.find('.pdt_promo a').text();
// // console.log(i + ":" + productName + "\t\t" + offer + "--" + specialOffer + "-" + data.find('.pdt_price .pdt_C_price').text().trim())
// product[PRODUCT_ADDITIONAL_PROMO] = specialOffer;
// // But no special offer, so get the discount price
// if (specialOffer === "") {
// var currentPrice = data.find('.pdt_price .pdt_C_price').text().trim()
// var originalPrice = data.find('.pdt_price .pdt_O_price').text().trim()
// console.log(i + ":" + productName + "\t\t" + offer + "--" + specialOffer + "-" + currentPrice + "---" + originalPrice)
// product[PRODUCT_ORIGINAL_PRICE] = originalPrice;
// product[PRODUCT_DISCOUNTED_PRICE] = currentPrice;
// }
// // Get expiry date
// var expiry = await getNTUCExpiry(productURL)
// product[PRODUCT_PROMO_EXPIRY] = expiry;
// console.log(product)
// productList.push(product);
// }
// })
// console.log(productList)
// return await productList;
// })
// };
async function getNTUCExpiry(url, isCustomOfferDate) {
return await fetch(url, {
method: 'get'
})
.then(res => res.text())
.then(async body => {
const $ = cheerio.load(body);
if (isCustomOfferDate)
var offerDate = $('.custom_offerDate div').text().trim().replace("Till", "");
else
var offerDate = $('.ofr_text').text().trim().replace("Till", "");
return offerDate;
})
}
module.exports.getPromo = getPromo;
module.exports.getNTUCProduct = getNTUCProduct;
module.exports.getColdStorageProduct = getColdStorageProduct;
module.exports.getShengSiongProduct = getShengSiongProduct;
module.exports.getGiantProduct = getGiantProduct;
module.exports.resultKey = {
PRODUCT_ADDITIONAL_PROMO,
PRODUCT_DISCOUNTED_PRICE,
PRODUCT_IMAGE_URL,
PRODUCT_NAME,
PRODUCT_ORIGINAL_PRICE,
PRODUCT_PROMO_EXPIRY,
PRODUCT_STORE,
PRODUCT_URL
}
// getPromo("https://khubite.000webhostapp.com/app/sg_supermarket/data_v2.json").then(data => {
// console.log(data);
// });