-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
110 lines (91 loc) · 2.85 KB
/
app.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
const Mailgun = require('mailgun-js');
const puppeteer = require('puppeteer');
const conf = require('./conf.json');
const LOGIN_PAGE = 'https://stackoverflow.com/users/login';
const PROFILE_PAGE = 'https://stackoverflow.com/users/current';
mailgun = Mailgun({
host: (conf.mailgun_eu_domain == "y") ? "api.eu.mailgun.net" : "api.mailgun.net",
apiKey: conf.mailgun_api_key,
domain: conf.mailgun_domain
});
function notify(body) {
mailgun
.messages()
.send({
from: conf.from,
to: conf.to,
subject: conf.subject,
text: body
}, (error, body) => {
if (error) {
throw error;
}
});
}
async function crawl() {
const browser = await puppeteer.launch({
headless: true,
executablePath: '/usr/bin/chromium',
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
]
});
const page = await browser.newPage();
console.log(`Loading ${LOGIN_PAGE}`);
await page.goto(LOGIN_PAGE).then(() => {
console.log(' -> success!');
});
console.log('Filling login form with credentials');
await page.click('.js-accept-cookies')
.catch(e => console.log("Could not find 'Accept Cookies' button."));
await page.type('#email', conf.login);
await page.type('#password', conf.password);
await page.waitFor(2000);
try {
await Promise.all([
page.click('#submit-button'),
page.waitForNavigation()
]).then(() => {
console.log(' -> success!');
});
} catch (error) {
console.error(error);
notify(`Login error. Could not login to StackOverflow.`);
await browser.close();
return;
}
console.log('Loading profile page');
await page.goto(PROFILE_PAGE).then(() => {
console.log(' -> success!');
});
try {
console.log('Grabbing stats');
await page.waitForSelector(statsSelector = '.js-highlight-box-badges', {timeout: 5000})
.then(() => {
console.log(' -> success!');
});
const stats = await page.evaluate(
statsSelector => document.querySelector(statsSelector).innerText,
statsSelector
);
console.log(`Sending stats: ${stats}`);
notify(stats)
} catch (e) {
console.log(' -> failed. Could not fetch the stats. Please check days left manually.');
notify(`Login succeeded. Could not fetch stats for consecutive days.`)
}
console.log('Closing browser');
await browser.close();
}
function job() {
try {
crawl();
} catch (error) {
console.error(error);
notify(`Something went wrong: ${error.toString()}`);
}
}
setInterval(job, 1000 * 60 * 60 * 22);
job();