-
Notifications
You must be signed in to change notification settings - Fork 6
/
android-web-messages-brac-bank-parser.js
236 lines (183 loc) · 7.48 KB
/
android-web-messages-brac-bank-parser.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
(() => {
let intervalCount = 0;
function scrollToTop() {
return new Promise((res, rej) => {
let interval = setInterval(function() {
console.log("scrolling up", intervalCount)
const scrollElems = document.querySelectorAll(".tombstone-timestamp-split.ng-trigger.ng-trigger-incomingMessage.ng-tns-c25-3.ng-star-inserted");
if(scrollElems && scrollElems[0]){
scrollElems[0].scrollIntoView();
}
intervalCount = intervalCount + 1;
if(intervalCount > 5) {
clearInterval(interval);
intervalCount = 0;
res();
}
}, 1500);
})
}
function getNumberCommaSanitizedString(str) {
const numberCommaIndex = str.search(/\d,\d/);
if (numberCommaIndex < 0) return str;
const middlePoint = numberCommaIndex + 1;
return `${str.substring(0, middlePoint)}${str.substring(middlePoint + 1, str.length)}`;
}
function getDateObjectFromText(dateText) {
let [day, month, year] = dateText.split("/");
return new Date(
parseInt(`20${year}`),
parseInt(month) - 1,
parseInt(day)
);
}
function getBills(smsList) {
const bills = smsList.filter(s => s.includes("Your bill"));
return bills.map(billText => {
const minimumDue = parseInt(
billText.match(/min due bdt (\d,?\d+);/i)[1].replace(",", "")
);
const dueAmount = billText.match(/total due bdt (\d+,?\d+);/i)[1].replace(",", "");
const dueDate = billText.match(/please pay (by ?)(.+)\./i);
const isUrgent = billText.includes("immediately");
return {
minimumDue,
dueAmount,
dueDate: dueDate ? getDateObjectFromText(dueDate[2]) : null,
isUrgent
}
});
}
function _getTransactionsForTransacted(smsList) {
const transactionTexts = smsList.filter(s => s.includes("transacted"));
const transactions = transactionTexts.map(t => {
const sanitizedStr = getNumberCommaSanitizedString(t);
const [, name, dateString] = sanitizedStr.match(/at (.+) on (\d\d\/\d\d\/\d\d)/);
const amount = Number(sanitizedStr.match(/BDT (.+) transacted/)[1]);
const dateObject = getDateObjectFromText(dateString);
return {
name,
amount: parseFloat(amount),
date: dateObject
}
});
return transactions;
}
function _getTransactionsForTrnx(smsList) {
const transactionTexts = smsList.filter(s => s.includes("Trnx"));
const transactions = transactionTexts.map(t => {
const numberCommaSanitizedString = getNumberCommaSanitizedString(t);
const [name, amountString, dateText] = numberCommaSanitizedString.split(",");
const amount = amountString.match(/(BDT|USD)(\d+)/)[2];
const dateString = dateText.match(/\d\d\/\d\d\/\d\d/)[0];
const dateObject = getDateObjectFromText(dateString);
return {
name,
amount: parseFloat(amount),
date: dateObject
}
});
return transactions;
}
function getTransactions(smsList) {
const transactions = [
..._getTransactionsForTrnx(smsList),
..._getTransactionsForTransacted(smsList)
]
return transactions;
}
function groupTransactionsByPeriod(transactions, periodDate = 18) {
const groups = {};
transactions.forEach(t => {
const month = t.date.getMonth() + 1;
const year = t.date.getFullYear();
const day = t.date.getDate();
let start_year = year;
let end_year = year;
let start_month = month;
let end_month = month;
if(day => periodDate) {
if(month + 1 > 12) {
end_month = 1
end_year = year + 1
} else {
end_month = month + 1
}
} else {
if(month - 1 === 0) {
start_year-= 1;
start_month = 12
} else {
end_month = 1
}
}
start_month = `${start_month}`.padStart(2, 0);
end_month = `${end_month}`.padStart(2, 0);
const start_key = `${start_year}-${start_month}`;
const end_key = `${end_year}-${end_month}`;
const dateKey = `${start_key}_${end_key}`;
if (!groups[dateKey]) groups[dateKey] = [];
groups[dateKey].push(t);
});
const monthlyGroups = Object.keys(groups).map((key) => {
const [start, end] = key.split("_");
return {
startDate: `${start}-18`,
endDate: `${end}-17`,
transactions: groups[key],
total: groups[key].reduce((sum, t) => sum + t.amount, 0).toFixed(2)
}
})
return monthlyGroups
.sort((a,b) => new Date(b.startDate) - new Date(a.startDate))
}
function scrollToBottom() {
const messages = [...document.querySelectorAll(".tombstone-timestamp-split.ng-trigger.ng-trigger-incomingMessage.ng-tns-c25-3.ng-star-inserted")]
messages.pop().scrollIntoView()
}
function appendToView({
monthlyTransactions
}) {
const nodeString = `<table style="padding: 10px">
<thead>
<tr>
<th style="padding: 10px">StartDate</th>
<th style="padding: 10px">EndDate</th>
<th style="padding: 10px">Total Amount</th>
<th style="padding: 10px">Number of transactions</th>
</tr>
</thead>
<tbody>
${
monthlyTransactions.map(t => `
<tr>
<td style="padding: 10px">${t.startDate}</td>
<td style="padding: 10px">${t.endDate}</td>
<td style="padding: 10px">${t.total}</td>
<td style="padding: 10px">${t.transactions.length}</td>
</tr>
`).join("")
}
</tbody>
</table>`
const node = document.createElement("div")
node.setAttribute("id", "myTable")
node.innerHTML = nodeString
const tableElement = document.querySelector(".compose-readonly") || document.querySelector('#myTable')
tableElement.replaceWith(node)
}
async function run () {
// await scrollToTop()
// scrollToBottom()
const textMessages = [...document.querySelectorAll(".text-msg")].map(r => r.innerText)
const transactions = getTransactions(textMessages);
const monthlyTransactions = groupTransactionsByPeriod(transactions);
const averageExpense = (monthlyTransactions.reduce((sum, m) => sum + m.total, 0)) / monthlyTransactions.length;
appendToView({
monthlyTransactions,
averageExpense
})
console.log(monthlyTransactions);
}
run()
})()