-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker.js
75 lines (65 loc) · 2.38 KB
/
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
63
64
65
66
67
68
69
70
71
72
73
74
75
import cron from 'node-cron';
import fetch from 'node-fetch';
import { readIniFileSync } from 'read-ini-file';
import path from 'path';
import { parse } from 'node-html-parser';
import { fileURLToPath } from 'url';
import { google } from 'googleapis';
import authorize from './authorize.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const FUNC_MAP = {
'NOTHAS': notHas,
'HAS': has
}
authorize();
let counter = 1;
const task = cron.schedule('* * * * *', async () => {
const fixture = path.join(__dirname, 'config.ini');
const info = readIniFileSync(fixture);
const response = await fetch(info.site.href, { method: 'GET' });
const data = await response.text();
const root = parse(data);
const targets = root.querySelectorAll(info.site.target);
let contexts = Array.prototype.slice.call(targets)
.filter(e => {
const content = (e?.innerHTML || e?.innerText || '').replace(/(\ | |\r|\n)/g, '');
return content.length;
})
.map(e => e.innerHTML);
const conditions = Object.values(info.condition).map(e => e.split(' '));
conditions.forEach(con => {
const [conType, text] = con;
const filterFunction = FUNC_MAP[conType];
contexts = filterFunction?.(contexts, text) || contexts;
});
if (contexts.length) {
authorize().then((auth) => sendMail(auth, info, contexts)).catch(console.error);
task.stop();
} else {
process.stdout.write("\r(" + counter++ + ") continue to next detecting.");
}
});
async function sendMail(auth, info, append) {
const { target, subject, context } = info.email;
const totalContext = context.split('<br/>').map(e => e + "\r\n").join('');
console.log(totalContext);
const gmail = google.gmail({ version: 'v1', auth });
const res = await gmail.users.messages.send({
userId: 'me',
requestBody: {
raw: btoa(
"From: " + target + "\r\n" +
"To: " + target + "\r\n" +
"Subject: " + subject + "\r\n\r\n" +
totalContext
).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
}
});
}
function notHas(collection, text) {
return collection.filter(e => e.indexOf(text) == -1);
}
function has(collection, text) {
return collection.filter(e => e.indexOf(text) !== -1);
}