Skip to content

Commit

Permalink
Minor fixes and improvements for csv2yaml and transactions scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-si committed Sep 9, 2020
1 parent 6a352b6 commit 202c3d4
Show file tree
Hide file tree
Showing 13 changed files with 227 additions and 57 deletions.
Binary file added examples/receipts/2020-01-07t1205_lunch.pdf
Binary file not shown.
7 changes: 2 additions & 5 deletions scripts/csv2yaml/dkb-visa.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
rmEmptyString,
keysToEnglish,
noteToAccount,
sanitizeYaml,
} = require('../helpers.js')


Expand Down Expand Up @@ -74,11 +75,7 @@ function normalizeAndPrint (filePathTemp) {
.localeCompare(String(transB.utc), 'en'),
)

const yamlString = yaml
.dump({transactions})
.replace(/^ {2}- /gm, '\n -\n ')
.replace(/^([\w- ]+): '(.+)'$/gm, '$1: $2')
.replace(/utc: 20(.+)$/gm, 'utc: \'20$1\'')
const yamlString = sanitizeYaml(yaml.dump({transactions}))

console.info(yamlString)
})
Expand Down
91 changes: 91 additions & 0 deletions scripts/csv2yaml/dkb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const fse = require('fs-extra')
const yaml = require('js-yaml')
const csvnorm = require('csvnorm')
const converter = require('converter')

const {
rmEmptyString,
keysToEnglish,
noteToAccount,
sanitizeYaml,
} = require('../helpers.js')


function normalizeAndPrint (filePathTemp) {
const csv2json = converter({
from: 'csv',
to: 'json',
})

let jsonTemp = ''
csv2json.on('data', chunk => {
jsonTemp += chunk
})
csv2json.on('end', () => {
const transactions = JSON
.parse(jsonTemp)
.map(keysToEnglish)
.map(transaction => {
const note = transaction.note
.replace(/<br\s+\/>/g, '\n')
const amount = transaction.amount + ' €'
const sortedTransaction = {
utc: transaction['value-utc'] < transaction['entry-utc']
? transaction['value-utc']
: transaction['entry-utc'],
}

if (transaction['value-utc'] !== sortedTransaction.utc) {
sortedTransaction['value-utc'] = transaction['value-utc']
}
if (transaction['entry-utc'] !== sortedTransaction.utc) {
sortedTransaction['entry-utc'] = transaction['entry-utc']
}

sortedTransaction.type = transaction.type
sortedTransaction.note = note

const transfersObj = transaction.amount.startsWith('-')
? {
transfers: [{
from: 'dkb:visa',
to: noteToAccount(note),
amount: amount.slice(1),
'original-amount': transaction['original-amount'],
}],
}
: {
transfers: [{
from: noteToAccount(note),
to: 'dkb:visa',
// TODO: Remove when github.com/adius/csvnorm/issues/1 is solved
amount: transaction.amount === '0,00' ? '0 €' : amount,
'original-amount': transaction['original-amount'],
}],
}
const newTransaction = Object.assign(sortedTransaction, transfersObj)

delete newTransaction.amount

return JSON.parse(JSON.stringify(newTransaction, rmEmptyString))
})
.sort((transA, transB) =>
// Oldest first
String(transA.utc)
.localeCompare(String(transB.utc), 'en'),
)

const yamlString = sanitizeYaml(yaml.dump({transactions}))

console.info(yamlString)
})

csvnorm.default({
encoding: 'latin1',
readableStream: fse.createReadStream(filePathTemp),
skipLinesStart: 6,
writableStream: csv2json,
})
}

normalizeAndPrint(process.argv[2])
74 changes: 74 additions & 0 deletions scripts/csv2yaml/fidor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const fse = require('fs-extra')
const yaml = require('js-yaml')
const csvnorm = require('csvnorm')
const converter = require('converter')

const {
rmEmptyString,
keysToEnglish,
noteToAccount,
sanitizeYaml,
} = require('../helpers.js')


function normalizeAndPrint (filePathTemp) {
const csv2json = converter({
from: 'csv',
to: 'json',
})

let jsonTemp = ''
csv2json.on('data', chunk => {
jsonTemp += chunk
})
csv2json.on('end', () => {
const transactions = JSON
.parse(jsonTemp)
.map(keysToEnglish)
.reverse() // Now sorted ascending by value date
.map(transaction => {
const currency = ' €'
const sortedTransaction = {
utc: transaction.date,
note: transaction.note + '\n' + transaction.note2,
}
const account = noteToAccount(transaction.note) || '_todo_'
const transfersObj = transaction.amount.startsWith('-')
? {
transfers: [{
from: 'fidor:giro',
to: account,
amount: transaction.amount.slice(1) + currency,
}],
}
: {
transfers: [{
from: account,
to: 'fidor:giro',
// TODO: Remove when https://github.com/adius/csvnorm/issues/1
// is solved
amount: transaction.amount === '0,00'
? 0
: transaction.amount + currency,
}],
}
const newTransaction = Object.assign(sortedTransaction, transfersObj)

delete newTransaction.amount

return JSON.parse(JSON.stringify(newTransaction, rmEmptyString))
})

const yamlString = sanitizeYaml(yaml.dump({transactions}))

console.info(yamlString)
})

csvnorm.default({
encoding: 'utf-8',
readableStream: fse.createReadStream(filePathTemp),
writableStream: csv2json,
})
}

normalizeAndPrint(process.argv[2])
7 changes: 2 additions & 5 deletions scripts/csv2yaml/finvesto.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
rmEmptyString,
keysToEnglish,
noteToAccount,
sanitizeYaml,
} = require('../helpers.js')


Expand Down Expand Up @@ -69,11 +70,7 @@ function normalizeAndPrint (filePathTemp) {
return JSON.parse(JSON.stringify(newTransaction, rmEmptyString))
})

const yamlString = yaml
.dump({transactions})
.replace(/^ {2}- /gm, '\n {2}-\n {4}')
.replace(/^([\w- ]+): '(.+)'$/gm, '$1: $2')
.replace(/utc: 20(.+)$/gm, 'utc: \'20$1\'')
const yamlString = sanitizeYaml(yaml.dump({transactions}))

console.info(yamlString)
})
Expand Down
7 changes: 2 additions & 5 deletions scripts/csv2yaml/hypovereinsbank.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
rmEmptyString,
keysToEnglish,
noteToAccount,
sanitizeYaml,
} = require('../helpers.js')


Expand Down Expand Up @@ -66,11 +67,7 @@ function normalizeAndPrint (filePathTemp) {
return JSON.parse(JSON.stringify(newTransaction, rmEmptyString))
})

const yamlString = yaml
.dump({transactions})
.replace(/^ {2}- /gm, '\n -\n ')
.replace(/^([\w- ]+): '(.+)'$/gm, '$1: $2')
.replace(/utc: 20(.+)$/gm, 'utc: \'20$1\'')
const yamlString = sanitizeYaml(yaml.dump({transactions}))

console.info(yamlString)
})
Expand Down
9 changes: 3 additions & 6 deletions scripts/csv2yaml/mbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
rmEmptyString,
keysToEnglish,
noteToAccount,
sanitizeYaml,
} = require('../helpers.js')


Expand Down Expand Up @@ -41,7 +42,7 @@ function normalizeAndPrint (filePathTemp) {
delete sortedTransaction['entry-utc']
}

const account = noteToAccount(transaction.to)
const account = noteToAccount(transaction.to) || '_todo_'
const transfersObj = transaction.amount.startsWith('-')
? {
transfers: [{
Expand Down Expand Up @@ -69,11 +70,7 @@ function normalizeAndPrint (filePathTemp) {
return JSON.parse(JSON.stringify(newTransaction, rmEmptyString))
})

const yamlString = yaml
.dump({transactions})
.replace(/^ {2}- /gm, '\n -\n ')
.replace(/^([\w- ]+): '(.+)'$/gm, '$1: $2')
.replace(/utc: 20(.+)$/gm, 'utc: \'20$1\'')
const yamlString = sanitizeYaml(yaml.dump({transactions}))

console.info(yamlString)
})
Expand Down
17 changes: 6 additions & 11 deletions scripts/csv2yaml/paypal.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const chrono = require('chrono-node')
const {
rmEmptyString,
keysToEnglish,
noteToAccount,
sanitizeYaml,
} = require('../helpers.js')


Expand Down Expand Up @@ -46,21 +48,18 @@ function normalizeAndPrint (filePathTemp) {
},
transaction,
)
const account = noteToAccount(transaction.Name)
const transfer = transaction.Gross.startsWith('-')
? {
from: '_todo_:paypal:' +
transaction.Currency
.toLowerCase()
.trim(),
to: transaction.Name
? `${transaction.Name} <${transaction['To Email Address']}>`
: 'paypal',
to: account ? account : 'paypal',
amount: transaction.Gross.slice(1) + ' ' + currency,
}
: {
from: transaction.Name
? `${transaction.Name} <${transaction['From Email Address']}>`
: 'paypal',
from: account ? account : 'paypal',
to: '_todo_:paypal:' +
transaction.Currency
.toLowerCase()
Expand Down Expand Up @@ -101,11 +100,7 @@ function normalizeAndPrint (filePathTemp) {
return JSON.parse(JSON.stringify(newTransaction, rmEmptyString))
})

const yamlString = yaml
.dump({transactions})
.replace(/^ {2}- /gm, '\n -\n ')
.replace(/^([\w- ]+): '(.+)'$/gm, '$1: $2')
.replace(/utc: 20(.+)$/gm, 'utc: \'20$1\'')
const yamlString = sanitizeYaml(yaml.dump({transactions}))

console.info(yamlString)
})
Expand Down
36 changes: 33 additions & 3 deletions scripts/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@ module.exports = {
prettyFormat,
prettyPrint,
rmEmptyString,
toDdotMdotYYYY,
sanitizeYaml,
toDDdotMMdotYYYY,
toDdotMdotYYYY,
}


function sanitizeYaml (yaml) {
if (typeof yaml !== 'string') {
throw new Error('YAML must be passed as a string')
}
return yaml
.replace(/^ {2}- /gm, '\n -\n ')
.replace(/^ {2}([\w- ]+): '(.+)'$/gm, ' $1: $2')
.replace(/utc: ([0-9TZ:.-]+)$/gm, 'utc: \'$1\'')
}


Expand Down Expand Up @@ -80,6 +92,7 @@ function keysToEnglish (object) {
.replace('Verwendungszweck', 'note')
.replace('Waehrung', 'currency')
.replace('Wertstellung', 'value-utc')
.replace('Wert', 'amount')
.replace('Zahlungsbetrag in ZW', 'amount')
.replace('Zahlungswährung (ZW)', 'currency')

Expand All @@ -89,6 +102,9 @@ function keysToEnglish (object) {
}

function noteToAccount (note) {
// Remove misleading terms
note = note.replace('Apple Pay', '')

// Sorted by ascending importance
// I.e. later keywords overwrite selection
/* eslint-disable quote-props */
Expand All @@ -98,11 +114,13 @@ function noteToAccount (note) {
'amazon': 'amazon',
'amazon prime': 'amazon:prime',
'patreon': 'patreon',
'facebook': 'facebook',
'google ireland limited cloud platform': 'google:cloud',
'day night sports gmbh': 'day_night_sports',
'spotify': 'spotify',
'apple': 'apple',
'itunes': 'apple:itunes',
'mozilla foundation': 'mozilla_foundation',
'mozilla': 'mozilla',
'namecheap': 'namecheap',
'name-cheap': 'namecheap',
Expand Down Expand Up @@ -130,6 +148,14 @@ function noteToAccount (note) {
'jimmy joy': 'jimmy_joy',
'wecircberlin': 'circ',
'lime ride': 'lime',
'lime electric': 'lime',
'bird rides': 'bird',
'google': 'google',
'free software foundation': 'free_software_foundation',
'landr audio': 'landr',
'mzla technologies': 'mzla_technologies',
'vodafone': 'vodafone',
'mailgun': 'mailgun',

// German
'ihk ': 'ihk',
Expand Down Expand Up @@ -159,11 +185,15 @@ function noteToAccount (note) {
'qthority': 'qthority',
'nosh good taste': 'nosh',
'musikhaus thomann': 'thomann',
'fratellis frankfurt': 'fratellis_frankfurt',
'fratellis frankfurt': 'fratellis_ristorante',
'fratellis rist': 'fratellis_ristorante',
'hansemerkur speziale kv': 'hansemerkur',
'zwanzigeins e.v.': 'zwanzigeins',
'tier de': 'tier',
'zalando': 'zalando',
'hetzner online': 'hetzner',
'golem media': 'golem',
'pro rauchfrei': 'pro_rauchfrei',
}
/* eslint-enable quote-props */
let account = note
Expand All @@ -180,5 +210,5 @@ function noteToAccount (note) {
}
})

return account || '_todo_'
return account
}
Loading

0 comments on commit 202c3d4

Please sign in to comment.