-
Notifications
You must be signed in to change notification settings - Fork 1
/
puppeteer-version.js
57 lines (49 loc) · 1.83 KB
/
puppeteer-version.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
const puppeteer = require('puppeteer');
const settings = require('./settings.json');
const moment = require('moment');
const ics = require('ics');
const fs = require('fs');
const he = require('html-entities');
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function pressEnter(page) {
await page.keyboard.press(String.fromCharCode(13));
}
async function type(page, id, text) {
await page.focus(id);
await page.keyboard.type(text);
}
function parseDesc(text) {
text = he.decode(text.replace(/\r\n/g, "").replace(/<br \/>/g, "\n")).split("\n");
return [
[text[0]], text[1], text.slice(2).join('\n')
]
}
(async() => {
const browser = await puppeteer.launch({
headless: true,
});
const page = await browser.newPage();
await page.goto(settings.login_url);
await type(page, settings.login_id, settings.login);
await type(page, settings.passw_id, settings.password);
await pressEnter(page);
page.on('response', async(res) => {
if (res.url().startsWith(settings.response_url)) {
let data = (await res.json()).map(x => { return { "start": moment(x.start).format('YYYY-M-D-H-m').split("-"), "end": moment(x.end).format('YYYY-M-D-H-m').split("-"), "description": parseDesc(x.description)[2], "title": parseDesc(x.description)[1], "categories": parseDesc(x.description)[0], "startOutputType": "local" } });
ics.createEvents(data, async(e, v) => {
if (!e) {
try {
await fs.writeFileSync(settings.ics_path, v);
} catch (error) {
console.log(error);
}
} else {
console.log(e);
}
await browser.close();
})
}
})
})();