-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
112 lines (97 loc) · 3.24 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
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
111
112
'use strict';
const puppeteer = require('puppeteer');
const req = require('request');
const fs = require('fs');
function readLog() {
return JSON.parse(fs.readFileSync('log.json', 'utf8'))
}
function writeLog(obj) {
return fs.writeFileSync('log.json', JSON.stringify(obj))
}
function postSlack(text) {
const options = httpOption(text)
req.post(options, function(error, response, body){});
}
function httpOption(text) {
return {
uri: "https://hooks.slack.com/services/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",
headers: {
"Content-type": "application/json",
},
json: {
"username": "PortalBot",
"text": text
}
};
}
(async() => {
try {
const browser = await puppeteer.launch({
ignoreSSL: true,
headless: true
//headless: false
});
const page = await browser.newPage();
await page.goto('http://www.portal.oit.ac.jp/portal/top.do');
await page.type('#userId', process.env.OIT_USERNAME);
await page.type('#password', process.env.OIT_PASSWD);
await page.click('#loginButton');
await page.waitFor(10000);
const personal_inf = await page.evaluate(() => {
let html = document.querySelector("div.personal_inf");
html = Array.from(html.querySelectorAll("div.details"));
return html.map(e => {
let date = e.querySelector("div.date > font").innerText;
let text = e.querySelector("div.text > font > a").innerText;
return date + ': ' + text;
});
});
const public_inf = await page.evaluate(() => {
let html = document.querySelector("div.public_inf");
html = Array.from(html.querySelectorAll("div.details"));
return html.map(e => {
let date = e.querySelector("div.date > font").innerText;
let text = e.querySelector("div.text > font > a").innerText;
return date + ': ' + text;
});
});
browser.close()
const log = readLog()
let new_personal_inf = []
let new_public_inf = []
personal_inf.forEach (e => {
let new_arrayvals = true
log.personal_inf.forEach (le => {
if (e == le) new_arrayvals = false
})
if (new_arrayvals) new_personal_inf.push(e)
})
public_inf.forEach (e => {
let new_arrayvals = true
log.public_inf.forEach (le => {
if (e == le) new_arrayvals = false
})
if (new_arrayvals) new_public_inf.push(e)
})
writeLog({personal_inf: personal_inf, public_inf: public_inf})
if (new_personal_inf.length > 0 || new_public_inf.length > 0) {
let text = "新しい通知を検知:\n"
if (new_personal_inf.length >= 0) {
text += "個人向けの連絡(eliza0x)\n"
text += "==========\n"
new_personal_inf.forEach(e => text+=e+"\n")
text += "\n"
}
if (new_public_inf.length >= 0) {
text += "全体向けの連絡\n"
text += "==========\n"
new_public_inf.forEach(e => text+=e+"\n")
text += "\n"
}
postSlack(text)
}
} catch (e) {
postSlack("情報の取得に失敗\n==========\n" + e.toString())
}
})();
// curl -X POST --data-urlencode 'payload={"username": "PortalBot", "text": "いまbot作ってる"}' https://hooks.slack.com/services/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