-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscrape-camel-price.js
56 lines (46 loc) · 1.69 KB
/
scrape-camel-price.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
var request = require('request');
var cheerio = require('cheerio');
var rp = require('request-promise');
var resultObject;
// scrapes price of given product name
// if model number result is null, trys to search with product name
function getCamelPrice(productModel, productName) {
var queryName = productName.replace(" ", "+");
rp("https://ca.camelcamelcamel.com/search?sq=" + productModel).then(function($) {
$("#products_list").find("tbody>tr").each(function() {
var amazonPrice = $(this).find("td.current_price.last>div.price_amazon").text().trim();
var thirdPartyNewPrice = $(this).find("td.current_price.last>div.price_new").text().trim();
resultObject = {
"lowestPrice": lowestPrice(amazonPrice, thirdPartyNewPrice)
}
});
if (resultObject !== undefined) {
return resultObject;
} else {
rp("https://ca.camelcamelcamel.com/search?sq=" + queryName).then(function($) {
$("#watchforms>table").find("tbody>tr>td").each(function() {
var amazonPrice = $(this).find("span.black").text().trim();
var thirdPartyNewPrice = $(this).find("td.white-space:nowrap>span.black").text().trim();
var lowestPrice = lowestPrice(amazonPrice, thirdPartyNewPrice)
return lowestPrice;
});
})
}
})
}
// helper function to calculate the lowest price
function lowestPrice(price1, price2) {
if (price1 === "Not in Stock" && price2 !== "Not in Stock") {
return price2;
}
if (price1 !== "Not in Stock" && price2 === "Not in Stock") {
return price1;
}
if (price1 < price2) {
return price1;
} else if (price2 < price1) {
return price2;
} else if (price1 === price2) {
return price1;
}
}