Skip to content

Instantly share code, notes, and snippets.

@iest
Last active August 31, 2015 11:25
Show Gist options
  • Save iest/a0cd4a0713fe980b5211 to your computer and use it in GitHub Desktop.
Save iest/a0cd4a0713fe980b5211 to your computer and use it in GitHub Desktop.
HSBC don't let customers download CSVs of transaction prior to 2 months ago. With a little node, we can do it
'use strict';
const moment = require('moment')
const jsdom = require('jsdom');
const fs = require('fs');
const FILE_NAME = process.argv[2] || './May-Jun.html';
jsdom.env(
fs.readFileSync(FILE_NAME).toString(),
["http://code.jquery.com/jquery.js"],
function (err, window) {
const $ = window.$;
let arr = ['Date,Payee,Category,Memo,Outflow,Inflow'];
// Input: [DD MMM] [Type] [Desc] [Out] [In]
const rows = $('tbody tr');
rows.each(function(i, el) {
const cells = $(el).children();
const date = moment($(cells[0]).text().trim(), 'DD MMM').year(moment().year());
const desc = $(cells[2]).text().trim();
const out = $(cells[3]).text().trim();
const inn = $(cells[4]).text().trim();
// output [YYYY-MM-DD],[Desc],[empty category],[empty memo],[out],[in]
arr.push(`${date.format('DD/MM/YY')},${desc},,,${out},${inn}`);
});
fs.writeFileSync(`${FILE_NAME}-processed.csv`, arr.join('\n'));
}
);
@iest
Copy link
Author

iest commented Aug 31, 2015

What?

Turns a HTML file containing a table with the following rows: [DD MMM] [Type] [Desc] [Out] [In] into a CSV file with [YYYY-MM-DD],[Desc],[empty category],[empty memo],[out],[in]. I wrote it for use with YNAB but I'm sure you could use it with other budget software.

Usage

  1. Copy the raw HTML of the transactions table from the HSBC website and save it to a file
  2. Save HSBChtml2YNABCSV.js to your desktop or whatever
  3. npm install moment jsdom
  4. In your terminal: node HSBChtml2YNABCSV.js path/to/htmlFile
  5. You'll have a new file: [name-of-original-file]-processed.html
  6. Import to YNAB, and you're done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment