-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·64 lines (53 loc) · 1.82 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
#!/bin/node
const puppeteer = require('puppeteer');
const fs = require('fs');
require('dotenv').config();
const args = process.argv.slice(2);
const headless = !args.includes('--disable-headless');
(async () => {
const downloadFolder = process.cwd() + '/meridian-temp-downloads';
if (fs.existsSync(downloadFolder)) {
fs.rmSync(downloadFolder, { recursive: true, force: true });
}
fs.mkdirSync(downloadFolder);
const browser = await puppeteer.launch({
headless,
args: [
'--no-sandbox',
'--disable-setuid-sandbox'
]
});
const page = await browser.newPage();
await page.goto('https://account.meridianenergy.co.nz/ssp/#/sign-in');
await page.waitForSelector('#email');
await page.type('#email', process.env.MERIDIAN_EMAIL);
await page.type('#password', process.env.MERIDIAN_PASSWORD);
await page.click('#login-button');
await page.waitForTimeout(5000);
await page.click('#usage-tab');
await page.waitForSelector('#usage_show_registers');
await page.click('#usage_show_registers');
const client = await page.target().createCDPSession();
await client.send('Page.setDownloadBehavior', {
behavior: 'allow',
downloadPath: downloadFolder
});
await page.waitForTimeout(1000);
await page.click('[value="Download"]');
let files = [];
for (let i = 0; i < 90; i++) {
files = fs.readdirSync(downloadFolder);
if (files.length) {
break;
}
await page.waitForTimeout(1000);
}
console.log(files);
await browser.close();
if (files.length) {
fs.copyFileSync(downloadFolder + '/' + files[0], './meridian-lastest.csv');
}
if (fs.existsSync(downloadFolder)) {
fs.rmSync(downloadFolder, { recursive: true, force: true });
}
})();