-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhackernews.checker.js
62 lines (54 loc) · 2.16 KB
/
hackernews.checker.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
const domcheck = require('./domcheck');
const axios = require('axios');
domcheck({
/**
* name [required] Name of the checker.
*/
name: 'hackernews',
/**
* url [required] The URL of the website to scrap using [Puppeeter](https://developers.google.com/web/tools/puppeteer)
*/
url: 'https://news.ycombinator.com/',
/**
* history [optional] The path to the history file that records DOM node values,
* this file is checked everytime the code runs to compare the current value against old values,
* and notify in case it has changed.
* This file is store in the `historyDir` which have `history` as default folder name,
* you can override it by setting the `historyDir: 'data'` property.
*/
history: 'hackernews.csv',
/**
* waitForSelector [required] DOM selector to wait for to load before scraping.
*/
waitForSelector: '.itemlist tr:first-child .title a',
/**
* onDocument [optional] Function that specifies how to get the DOM data from the url,
* by default it will just query the `waitForSelector`.
*
* @param {string} selector This the same as the `waitForSelector`.
*/
onDocument: (selector) => {
const nodeList = document.querySelectorAll(selector);
return nodeList[0] && nodeList[0].innerText.trim();
},
/**
* notify [required] Function that defines how you will get notified with the result,
* below is an example using IFTTT webhook to get notified via Telegram.
*
* @param {string} name The name of this checker, same as the `name` property.
* @param {string} value The the new value of the dom node.
* @param {string} error The error message if any error happened.
*/
notify: (name, value, error) => {
const success = `✓ ${name} status upated to '${value}'`;
const failure = `𝗑 Error running checker ${name}: '${error}'`;
const message = error ? failure : success;
console.log('Notification:', message);
/*
const iftttWebhook = (event, key) => `https://maker.ifttt.com/trigger/${event}/with/key/${key}`;
const url = new URL(iftttWebhook('_EVENT_', '_KEY_'));
url.searchParams.append("value1", message);
return axios.get(url.toString());
*/
}
});