-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarousell.js
159 lines (135 loc) · 4.69 KB
/
carousell.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
require("dotenv").config();
const puppeteer = require("puppeteer");
let prevListings = [];
const resellers = process.env.RESELLERS.split(", ");
let context;
const CronJob = require("cron").CronJob;
const job = new CronJob({
cronTime: process.env.SLEEP_TIME,
onTick: loadPage,
});
async function loadPage(){
var link = "https://sg.carousell.com/search/" + encodeURIComponent(process.env.ITEM) + "?sort_by=time_created%2Cdescending"
var page = await context.newPage();
await page.setUserAgent(
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3419.0 Safari/537.36"
);
await page.setCacheEnabled(false);
await page.setRequestInterception(true);
page.on("request", (req) => {
if (req.resourceType() == "document") req.continue();
else req.abort();
});
await page.goto(link, { waitUntil: "load", timeout: 0 });
var data = await page.evaluate(function () {
return window.initialState;
});
await page.close();
let listings = [];
data.SearchListing.listingCards.forEach((element) => {
const name = element.belowFold[0].stringContent;
const price = element.belowFold[1].stringContent;
const condition = element.belowFold[3].stringContent;
const listingID = element.listingID;
const thumbnailURL = element.thumbnailURL;
const seller_username =
data.Listing.listingsMap[element.listingID].seller.username;
const itemURL = ("https://sg.carousell.com/p/" + name.replace(/[^a-zA-Z ]/g, "-") + "-" + listingID).replace(/ /g, "-");
const isBumper = element.aboveFold[0].component === "active_bump" // Lightning icons - Most resellers will not have active bumps
const isSpotlighter = element.hasOwnProperty('promoted') // Purple promoted icons - Most resellers will not have spotlight
listing = {
name: name,
price: price,
condition: condition,
listingID: listingID,
thumbnailURL: thumbnailURL,
seller_username: seller_username,
itemURL: itemURL
};
if(isBumper || isSpotlighter)
console.log("Excluding bumper and spotlighter: " + seller_username)
else {
if(!resellers.includes(seller_username))
listings.push(listing)
}
});
var asiaTime = new Date().toLocaleString("en-US", {
timeZone: "Asia/Shanghai",
});
dateTime = new Date(asiaTime);
if (prevListings.length == 0)
console.log("Script starting... we populate the listings!");
else {
diffListings = compareListings(prevListings, listings);
if (diffListings.length == 0)
console.log(dateTime + "\t There is no update... :(");
else {
console.log(dateTime + "\t There is an update!! :)");
messages = createListingsStr(diffListings);
telegram_bot_sendtext(messages);
}
}
// Save for comparison later
prevListings = listings;
}
job.start();
// Message to send to Telegram
function telegram_bot_sendtext(bot_message_array) {
const axios = require("axios");
bot_token = process.env.BOT_TOKEN;
bot_chatID = process.env.BOT_CHATID;
bot_message_array.forEach((bot_message) => {
const send_text =
"https://api.telegram.org/bot" +
bot_token +
"/sendMessage?chat_id=" +
bot_chatID +
"&parse_mode=html&text=" +
encodeURI(bot_message);
axios
.get(send_text)
.then(function (response) {
// handle success
console.log(response.data.result.text + "\n");
})
.catch(function (error) {
// handle error
console.log(error.config.url);
})
.then(function () {
// always executed
});
});
}
// Compare listings
function compareListings(array1, array2) {
ids = new Set(array1.map(({ listingID }) => listingID));
array2 = array2.filter(({ listingID }) => !ids.has(listingID));
return array2;
}
// Prepare listing string to send to telegram.
// Splitting things up properly because TG cannot handle long messages
function createListingsStr(listings) {
splitMessages = [];
let message = "";
for (var i = 0; i < listings.length; i++) {
message += "Name: " + listings[i]["name"] + "\n";
message += "Price: " + listings[i]["price"] + "\n";
message += "Condition: " + listings[i]["condition"] + "\n";
message += "Seller Username: " + listings[i]["seller_username"] + "\n";
message += "Thumbnail: " + listings[i]["thumbnailURL"] + "\n";
message += "Item Link: " + listings[i]["itemURL"] + "\n";
splitMessages.push(message);
message = "";
}
return splitMessages;
}
async function createBrowser(cb) {
const browser = await puppeteer.launch({
headless: true,
args: ["--no-sandbox", "--incognito"],
});
context = await browser.createIncognitoBrowserContext();
cb()
}
createBrowser(loadPage);