-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
158 lines (131 loc) · 4.82 KB
/
index.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
const fetch = require("node-fetch");
const cron = require("node-cron");
const cheerio = require("cheerio");
const chalk = require("chalk");
const nodemailer = require("nodemailer");
const config = require("./config.json");
const transporter = nodemailer.createTransport({
host: 'live.smtp.mailtrap.io',
port: 587,
secure: false,
auth: {
user: config.email.user,
pass: config.email.pass,
}
});
const url = "https://www.andiumhomes.je/findahome/propertylettings/";
let existingProperties = [];
/**
* Scrapes properties on the Andium website and checks for changes.
*/
async function checkProperties() {
try {
console.log(chalk.gray("Checking properties..."));
const response = await fetch(url, config.headers);
const html = await response.text();
const $ = cheerio.load(html);
const properties = [];
// Loop over properties list
$("#lettingsList .container.p-0").each((i, element) => {
const property = {};
property.name = $(element).find("h3").text().trim();
property.address = $(element).find("p.mb-1").text().trim();
property.price = $(element).find("span.h3").text().trim();
properties.push(property);
});
// Find any differences between the cached properties and newly fetched properties
const changes = findArrayDifference(existingProperties, properties);
if (changes.added.length === 0 && changes.removed.length === 0) {
console.log(chalk.yellow("No changes"));
return;
}
console.log(chalk.gray(`Found ${changes.added.length} new and ${changes.removed.length} removed properties`));
let message = "";
// If new properties were added, format a message to email
if (changes.added.length !== 0) {
message += "<br><br>----------------<br>";
message += "<strong>New Properties:</strong><br>";
for (let change of changes.added) {
message += "--<br>";
message += "Name: " + change.name + "<br>";
message += "Address: " + change.address + "<br>";
message += "Price: " + change.price + "<br>";
}
message += "<br>";
}
// If properties were removed, format a message to email
if (changes.removed.length !== 0) {
message += "<br><br>----------------<br>";
message += "<strong>Removed Properties:</strong><br>";
for (let change of changes.removed) {
message += "--<br>";
message += "Name: " + change.name + "<br>";
message += "Address: " + change.address + "<br>";
message += "Price: " + change.price + "<br>";
}
message += "<br>";
}
sendUpdate(message);
existingProperties = properties;
} catch (e) {
console.error(chalk.red("Failed to check"), e);
}
}
/**
* Sends an email containing the given html.
*
* @param {String} html The html content of the email
*/
async function sendUpdate(html) {
const mailOptions = {
from: config.email.from,
to: config.email.recipients,
subject: `${config.email.subject} (${generateRandomString(6)})`,
html
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.error(chalk.red("Failed to send update"), e);
} else {
console.log(chalk.green('Email sent:', info.response));
}
});
}
/**
* Finds the differences between the two arrays.
*
* @param {Array} existingProperties The existing cached properties
* @param {Array} properties The newly fetched properties
* @returns The properties that were added or removed
*/
function findArrayDifference(existingProperties, properties) {
const existingNames = existingProperties.map(p => p.name);
const newNames = properties.map(p => p.name);
return {
added: properties.filter(p => !existingNames.includes(p.name)),
removed: existingProperties.filter(p => !newNames.includes(p.name))
}
}
/**
* Generates a random string of characters.
* https://stackoverflow.com/a/1349426
*
* @param {Number} length The length of the random string
* @returns A random string
*/
function generateRandomString(length) {
let result = "";
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const charactersLength = characters.length;
let counter = 0;
while (counter < length) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
counter += 1;
}
return result;
}
module.exports = { findArrayDifference, generateRandomString };
checkProperties();
cron.schedule(config.cronInterval, function () {
checkProperties();
});