-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
89 lines (78 loc) · 2.58 KB
/
main.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
// Libraries
const axios = require('axios');
const cheerio = require('cheerio');
const dotenv = require('dotenv');
const cron = require('node-cron');
const Push = require( 'pushover-notifications' );
const OneSignal = require( 'onesignal-node' );
const LocalStorage = require( 'node-localstorage' ).LocalStorage;
// Configure DotEnv
dotenv.config();
// Constants
const edpUrlCheck = 'https://www.dealabs.com/discussions/le-topic-des-erreurs-de-prix-1056379?page=9999999#thread-comments';
const localStorage = new LocalStorage('./data');
const pusher = new Push( {
user: process.env.USER_KEY,
token: process.env.API_TOKEN,
});
const oneSignalClient = new OneSignal.Client({
userAuthKey: process.env.AUTH_KEY,
app: {
appAuthKey: process.env.APP_AUTH_KEY,
appId: process.env.APP_ID
}
});
// Run the fetcher once it started
fetcher();
// Setup crontab every 5min
cron.schedule('*/5 * * * *', () => {
console.log('Running fetcher !');
fetcher();
});
function fetcher() {
const latestSavedCommentId = localStorage.getItem('latestSavedCommentId');
axios(edpUrlCheck)
.then(response => {
const html = response.data;
const $ = cheerio.load(html);
const statsTable = $('.comments-list--paginated > article');
var currentLatestCommentHtml = statsTable[statsTable.length - 1];
// Get last comment data
const currentLatestComment = {
id: $(currentLatestCommentHtml).attr('id'),
posterName: $(currentLatestCommentHtml).find('.user > span').text(),
time: $(currentLatestCommentHtml).find('.lbox--v-3 > time').text(),
comment: $(currentLatestCommentHtml).find('.userHtml').text()
}
// Check if it's a new deal or not :)
if (latestSavedCommentId != currentLatestComment.id) {
// It's a new deal !, let's create a push notification
/* PushOver config
var msg = {
message: currentLatestComment.comment,
title: 'New EDP Deal !',
sound: 'magic',
priority: 1
}
// Send the push !
pusher.send(msg);
*/
// OneSignal config
var msg = new OneSignal.Notification({
headings: {
en: 'New EDP Deal !'
},
contents: {
en: currentLatestComment.comment
},
included_segments: [ 'Active Users' ],
excluded_segments: [ 'Banned Users' ],
});
oneSignalClient.sendNotification(msg)
.finally(()=> {
// Then save it for later use...
localStorage.setItem('latestSavedCommentId', currentLatestComment.id);
});
}
})
}