Skip to content
This repository has been archived by the owner on Feb 5, 2023. It is now read-only.

Add --from and --to date range options for csv export #28

Merged
merged 2 commits into from
Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ Commands:
list List all available accounts
get_ofx <out_path> Fetch .ofx files for all accounts into out_path
csv [options] Fetch .csv files for accounts
-p, --path <path> Export path. defaults to ./export
-f, --from <dd/mm/yyyy> From date
-t, --to <dd/mm/yyyy> To date
config Set up login details
```

Expand Down
26 changes: 22 additions & 4 deletions account.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = class Account {
return ofx;
}

async statement() {
async statement(from, to) {
// Return a CSV-formatted string of the most recent account statement.
await this.select();
if (!(await this.page.$('table#filterable-ftb'))) {
Expand All @@ -57,6 +57,21 @@ module.exports = class Account {
return [];
}

if (from) {
await this.page.$eval('#searchDateFromBottom', el => el.value = from);
}
if (to) {
await this.page.$eval('#searchDateToBottom', el => el.value = to);
}

// Always perform a search to normalise the html additional-data
// (yup, the initial format is different :/

// Remove this so we can wait for it again
await this.page.$eval('table#filterable-ftb', el => el.remove());
await this.page.$eval('#searchBottom', el => el.click());
await u.wait(this.page, 'table#filterable-ftb');

// Parse the transactions in the context of the page.
let transactions = await this.page.evaluate(() => {
let txns = {};
Expand All @@ -77,7 +92,10 @@ module.exports = class Account {
txd['balance'] = row.querySelector('[headers=header-balance]').innerText.trim();
let transType = row.querySelector('.description div.additional-data div');
txd['trans-type'] = transType.innerText.trim();
let refs = transType.nextSibling.textContent.split('\n');
let refs = [];
row.querySelectorAll('.description div.additional-data p').forEach((p) => {
refs = refs.concat(p.textContent.split('\n'));
});
let refParts = [];
refs.forEach(function (ref) {
let refTrim = ref.trim();
Expand All @@ -100,8 +118,8 @@ module.exports = class Account {
return statement;
}

async statementCSV() {
let statement = await this.statement();
async statementCSV(from, to) {
let statement = await this.statement(from, to);
return this.csvLines(statement);
}

Expand Down
6 changes: 4 additions & 2 deletions barclayscrape.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ program

program
.command('csv')
.option('-p, --path', 'Export path')
.option('-p, --path <path>', 'Export path. defaults to ./export')
.description('Fetch .csv files for accounts')
.option('-f, --from <dd/mm/yyyy>', 'From date')
.option('-t, --to <dd/mm/yyyy>', 'To date')
.action(async (options) => {
var sess;
try {
Expand All @@ -85,7 +87,7 @@ program
try {
const accounts = await sess.accounts();
for (let account of accounts) {
const csvLines = await account.statementCSV();
const csvLines = await account.statementCSV(options.path, options.to);
if (csvLines) {
var label = exportLabel(account);
var extraLog = '';
Expand Down