-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
172 lines (144 loc) · 5.17 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import dotenv from "dotenv";
import puppeteer from "puppeteer";
import currency from "currency.js";
import { GoogleSpreadsheet } from "google-spreadsheet";
import { JWT } from "google-auth-library";
dotenv.config();
(async () => {
// Navigate to the Scottish Widows website:
const browser = await puppeteer.launch({ headless: "new" });
const page = await browser.newPage();
await page.goto(process.env.PENSION_URL);
await page.waitForSelector("h1");
await page.waitForSelector("#lbganalyticsCookies");
const title = await page.$("h1").then((e) => e.evaluate((t) => t.innerText));
if (
!title.includes("MAINTENANCE") &&
!title.includes("something went wrong")
) {
// Log into Scottish Widows:
await page.click("#accept");
await fillInputField(page, "email", process.env.PENSION_EMAIL);
await fillInputField(page, "password", process.env.PENSION_PASSWORD);
await Promise.all([page.click("#button-submit"), page.waitForNavigation()]);
// Scrape values:
await page.waitForSelector("[data-selector=sub-policy-select-link]");
await page.click("[data-selector=sub-policy-select-link]");
await page.waitForSelector("[data-selector=policy-valuation-date]");
const dateText = await getTextFromDataSelector(
page,
"policy-valuation-date"
);
const newBalanceText = await getTextFromDataSelector(page, "policy-total");
const premiumText = await getText(
page,
"[data-selector=payment-history-table] tbody tr"
);
// Process values:
const date = getDateStr(dateText);
const newBalance = currency(newBalanceText);
const premium = getPremium(premiumText);
// Record data in Google Sheet:
addDataToSheet(date, newBalance, premium);
} else {
console.log("site is unavailable. try again later.");
}
await browser.close();
})();
async function addDataToSheet(date, newBalance, premium) {
// Load Google Sheet and authenticate:
const SCOPES = [
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/drive.file",
];
const jwt = new JWT({
email: process.env.CLIENT_EMAIL,
key: process.env.PRIVATE_KEY,
scopes: SCOPES,
});
const doc = new GoogleSpreadsheet(process.env.GOOGLE_SPREADSHEET_ID, jwt);
await doc.loadInfo();
// Find empty row to fill:
const sheet = doc.sheetsByIndex[0];
const rows = await sheet.getRows({ limit: 15 });
// assuming not -1, based on my existing spreadsheet
const lastRecordIndex = rows.findIndex((row) => row.get("date"));
let lastRow = rows[lastRecordIndex];
let oldBalance = currency(lastRow.get("value"));
function isRowRecorded(oldDate, date, balance) {
return oldDate === date && oldBalance.value === balance.value;
}
if (rows.length && isRowRecorded(lastRow.get("date"), date, newBalance)) {
// Todo: uncomment when testing
// console.log(`row ${date}: ${newBalance} already recorded. abort.`);
} else {
let newRow = rows[lastRecordIndex - 1];
const today = new Date();
const time = `${today.getHours()}:${today.getMinutes()}:${today.getSeconds()}`;
let totalPaid = currency(lastRow.get("total payments"));
// Record if new premium payment was made:
if (getDateFromStr(lastRow.get("date")) < getDateFromStr(premium.date)) {
totalPaid = totalPaid.add(premium.value);
const totalGain = currency(lastRow.get("total gain"));
const intermediateBalance = oldBalance.add(premium.value);
const paymentRowData = {
time,
date: premium.date,
value: intermediateBalance,
change: premium.value,
payment: true,
"total payments": totalPaid,
"total gain": totalGain,
"rate of return": totalGain.value / totalPaid.value,
};
saveSheetRow(newRow, paymentRowData);
oldBalance = intermediateBalance;
lastRow = newRow;
newRow = rows[lastRecordIndex - 2];
}
// Record new balance:
const change = newBalance.subtract(oldBalance);
const totalGain = newBalance.subtract(totalPaid);
const newRowData = {
time,
date,
value: newBalance,
change,
"total payments": lastRow.get("total payments"),
"total gain": totalGain,
"rate of return": totalGain.value / totalPaid.value,
};
saveSheetRow(newRow, newRowData);
}
}
async function fillInputField(page, fieldName, input) {
await page.type(`input[name=${fieldName}]`, input);
}
async function getText(page, selector) {
const element = await page.$(selector);
return await element.evaluate((element) => element.innerText);
}
async function getTextFromDataSelector(page, dataSelector) {
return getText(page, `[data-selector=${dataSelector}]`);
}
async function saveSheetRow(newRow, newRowData) {
newRow.assign(newRowData);
await newRow.save();
}
function getDateFromStr(dateStr) {
const parts = dateStr.split("/");
return new Date(
parseInt(parts[2], 10),
parseInt(parts[1], 10) - 1,
parseInt(parts[0], 10)
);
}
function getDateStr(dateText) {
return new Date(dateText).toLocaleDateString("en-GB");
}
function getPremium(text) {
const comps = text.split("\t");
const value = currency(comps[2]);
const date = getDateStr(comps[0]);
return { value, date };
}