Skip to content

Commit

Permalink
Merge pull request #13 from tarioch/feature/truelayer_importer
Browse files Browse the repository at this point in the history
New importer using truelayer api
  • Loading branch information
tarioch authored Apr 25, 2020
2 parents bd13874 + 69997cc commit b986c6d
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
23 changes: 21 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Import from `Transferwise <https://www.transferwise.com/>`_ using their api

::

from tariochbctools.importers.transferwiseimport importer as twimp
from tariochbctools.importers.transferwise import importer as twimp
CONFIG = [twimp.Importer()]

Create a file called transferwise.yaml in your import location (e.g. download folder).
Expand All @@ -113,6 +113,25 @@ Create a file called transferwise.yaml in your import location (e.g. download fo
token: <your api token>
baseAccount: <Assets:Transferwise:>

**TrueLayer**

Import from `TrueLayer <https://www.truelayer.com/>`_ using their api services. e.g. supports Revolut.
You need to create a dev account and see their documentation about how to get a refresh token.

::

from tariochbctools.importers.truelayer import importer as tlimp
CONFIG = [tlimp.Importer()]

Create a file called truelayer.yaml in your import location (e.g. download folder).

::

baseAccount: <Assets:MyBank:>
client_id: <CLIENT ID>
client_secret: <CLIENT SECRET>
refresh_token: <REFRESH TOKEN>

**zkb**

Import mt940 from `Zürcher Kantonalbank <https://www.zkb.ch/>`_
Expand All @@ -124,7 +143,7 @@ Import mt940 from `Zürcher Kantonalbank <https://www.zkb.ch/>`_

**ibkr**

Import transactions from `Interactive Brokers <https://www.interactivebrokers.com/>`_
Import dividends from `Interactive Brokers <https://www.interactivebrokers.com/>`_

Create a file called ibkr.yaml in your import location (e.g. downloads folder).

Expand Down
66 changes: 66 additions & 0 deletions src/tariochbctools/importers/truelayer/importer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import yaml
import dateutil.parser
from os import path
import requests

from beancount.ingest import importer
from beancount.core import data
from beancount.core import amount
from beancount.core.number import D


class Importer(importer.ImporterProtocol):
"""An importer for Truelayer API (e.g. for Revolut)."""

def identify(self, file):
return 'truelayer.yaml' == path.basename(file.name)

def file_account(self, file):
return ''

def extract(self, file, existing_entries):
with open(file.name, 'r') as f:
config = yaml.safe_load(f)
baseAccount = config['baseAccount']
clientId = config['client_id']
clientSecret = config['client_secret']
refreshToken = config['refresh_token']

r = requests.post('https://auth.truelayer.com/connect/token', data={
"grant_type": "refresh_token",
"client_id": clientId,
"client_secret": clientSecret,
"refresh_token": refreshToken,
})
tokens = r.json()
accessToken = tokens['access_token']
headers = {'Authorization': 'Bearer ' + accessToken}

entries = []
r = requests.get('https://api.truelayer.com/data/v1/accounts', headers=headers)
for account in r.json()['results']:
accountId = account['account_id']
accountCcy = account['currency']
r = requests.get(f'https://api.truelayer.com/data/v1/accounts/{accountId}/transactions', headers=headers)
for trx in r.json()['results']:
metakv = {
'ref': trx['meta']['provider_id'],
}
if trx['transaction_classification']:
metakv['category'] = trx['transaction_classification'][0]
meta = data.new_metadata('', 0, metakv)
entry = data.Transaction(
meta,
dateutil.parser.parse(trx['timestamp']).date(),
'*',
'',
trx['description'],
data.EMPTY_SET,
data.EMPTY_SET,
[
data.Posting(baseAccount + accountCcy, amount.Amount(D(str(trx['amount'])), trx['currency']), None, None, None, None),
]
)
entries.append(entry)

return entries

0 comments on commit b986c6d

Please sign in to comment.