-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (52 loc) · 1.75 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
var config = require('./config.js');
var mailgun = require('mailgun-js')(config.mailgun);
var xray = require('x-ray')();
var schedule = require('node-schedule');
console.log('Starting Scraper Watcher...');
schedule.scheduleJob(config.schedule, function() {
console.log(`Scraper Watching is checking ${config.url}...`);
xray(config.url, config.selector)(function (err, contents) {
if(err) {
console.error(err);
sendNotification({
subject: 'Scraper Watcher error',
text: err,
}, true);
}
if(config.searchStrings.indexOf(contents) > -1) {
console.log(`A match was found in ${contents} at ${new Date()}!`);
sendNotification({
subject: 'A match has been found!',
text: `Scraper Watcher has found a match in: ${contents}`,
});
} else {
console.log(`No match found in ${contents} at ${new Date()}`);
sendNotification({
subject: 'No match found',
text: `Scraper Watcher has not found a match in: ${contents}`,
}, true);
}
});
});
/**
* Send a notification using Mailgun.
* Logs errors to the console.
*
* @param {Object} options
* @param {string} options.subject Email subject line.
* @param {string} options.text Email message.
* @param {boolean} [debug] If set to true, the notification will only be sent
* if `config.debug === true` as well.
*/
function sendNotification(options, debug) {
if(debug && !config.debug) return;
var text = debug ? options.text + '\nTo stop these messages, set debug: false in the configuration.' : options.text;
mailgun.messages().send({
from: config.notifyFrom,
to: config.notifyEmail,
subject: options.subject,
text: text,
}, function(err, body) {
if(err) console.error(err);
});
}