/usr/share/skrooge/skrooge-sabb.py is in skrooge-common 2.11.0-1build2.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | #!/usr/bin/env python3
# -*- coding: utf-8 -*-
#**************************************************************************
#* Copyright (C) 2017 by Bernhard Scheirle bernhard@scheirle.de
#* This program is free software: you can redistribute it and/or modify
#* it under the terms of the GNU General Public License as published by
#* the Free Software Foundation, either version 3 of the License, or
#* (at your option) any later version.
#*
#* This program is distributed in the hope that it will be useful,
#* but WITHOUT ANY WARRANTY; without even the implied warranty of
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#* GNU General Public License for more details.
#*
#* You should have received a copy of the GNU General Public License
#* along with this program. If not, see <http://www.gnu.org/licenses/>
#**************************************************************************
"""
Skrooge AqBanking Bridge (SABB)
-------------------------------
Authors:
* Bernhard Scheirle <bernhard@scheirle.de>
"""
import argparse
import contextlib
import csv
import datetime
import io
import os
import re
import shutil
import subprocess
import sys
import tempfile
from distutils.version import LooseVersion
__VERSION__ = "1.0.0"
class Account(object):
def __init__(self):
self.bank_number = ""
self.account_number = ""
self.iban = ""
def toString(self):
return 'Account(' + self.bank_number + ', ' + self.account_number + ', ' + self.iban + ')'
def isValid(self):
return self.bank_number != "" and self.account_number != "" and self.iban != ""
class Accounts(object):
def __init__(self):
self._account_map = {}
def account_map_key(self, bank_number, account_number):
return str(bank_number) + '.' + str(account_number)
def _build_iban_map(self):
self._account_map = {}
regex_account_number = re.compile('accountNumber=\"(.*)\"')
regex_bank_number = re.compile('bankCode=\"(.*)\"')
regex_iban = re.compile('iban=\"(.*)\"')
#regex_bic = re.compile('bic=\"(.*)\"')
accounts_folder = os.path.expanduser('~/.aqbanking/settings/accounts/')
for file_name in os.listdir(accounts_folder):
if not file_name.endswith('.conf'):
continue
file_path = os.path.join(accounts_folder, file_name)
with open(file_path, 'r') as account_file:
content = account_file.read()
account = Account()
match = regex_account_number.search(content)
if match:
account.account_number = match.group(1)
match = regex_bank_number.search(content)
if match:
account.bank_number = match.group(1)
match = regex_iban.search(content)
if match:
account.iban = match.group(1)
# match = regex_bic.search(content)
# if match:
# account.bic = match.group(1)
# print(account.toString())
if account.isValid():
key = self.account_map_key(account.bank_number, account.account_number)
self._account_map[key] = account
def get_account(self, bank_number, account_number):
if not self._account_map:
self._build_iban_map()
key = self.account_map_key(bank_number, account_number)
if key in self._account_map:
return self._account_map[key]
else:
return Account()
def get_accounts(self):
if not self._account_map:
self._build_iban_map()
return self._account_map.values()
class AqDialect(csv.Dialect):
delimiter = ';'
quotechar = '"'
quoting = csv.QUOTE_NONE
lineterminator = '\n'
class AqDialectTransfairs(csv.Dialect):
delimiter = ';'
quotechar = '"'
quoting = csv.QUOTE_ALL
lineterminator = '\n'
class SkroogeDialect(csv.Dialect):
delimiter = ';'
quotechar = '"'
escapechar = '\\'
quoting = csv.QUOTE_ALL
lineterminator = '\n'
@contextlib.contextmanager
def TemporaryContextFile():
with tempfile.TemporaryDirectory(prefix="SSAB.") as dir_path:
context_file_path = os.path.join(dir_path, "context")
open(context_file_path, 'a').close()
yield context_file_path
class SABB(object):
# Tools
AqBanking = 'aqbanking-cli'
AqHBCI = 'aqhbci-tool4'
ReturnValue_NormalExit = 0
ReturnValue_InvalidVersion = 1
def __init__(self):
self.accounts = Accounts()
def build_command(self, executable, args):
com = [executable]
if executable is self.AqBanking:
com.append('--charset=utf-8')
com.extend(args)
return com
def get_csv_reader(self, process_result, fieldnames, dialect=AqDialect):
input = process_result.stdout.decode("utf-8").replace('\t', ';')
reader = csv.DictReader(input.splitlines(), fieldnames=fieldnames, dialect=dialect)
return reader
def get_csv_writer(self, fieldnames, generateHeader=True):
output = io.StringIO("")
writer = csv.DictWriter(output, fieldnames=fieldnames, dialect=SkroogeDialect)
if generateHeader:
writer.writeheader()
return output, writer
def format_iban(self, iban):
result = ""
for i, c in enumerate(iban):
if i % 4 == 0:
result = result + " "
result = result + c
return result.strip()
def get_iban(self, bank_number, account_number):
account = self.accounts.get_account(bank_number, account_number)
if account.isValid():
return self.format_iban(account.iban.upper())
else:
return ""
def check_version(self):
process_result = subprocess.run(
self.build_command(self.AqBanking, ['versions']),
stdout=subprocess.PIPE
)
process_result.check_returncode()
lines = process_result.stdout.decode("utf-8").splitlines()
valid_version = False
for line in lines:
line = line.strip()
if line.startswith("AqBanking-CLI:"):
line = line[14:].strip()
if LooseVersion(line) >= LooseVersion("5.6.10"):
valid_version = True
break
if not valid_version:
print("Please install a newer version of aqbanking. At least version 5.6.10 is requiered.")
return valid_version
def get_accounts(self):
if not self.check_version():
return self.ReturnValue_InvalidVersion
process_result = subprocess.run(
self.build_command(self.AqBanking, ['listaccs']),
stdout=subprocess.PIPE
)
process_result.check_returncode()
fieldnames_input = ['ignore','bank_number','account_number','bank_name','account_name']
fieldnames_output = ['id']
reader = self.get_csv_reader(process_result, fieldnames_input)
output, writer = self.get_csv_writer(fieldnames_output)
accounts = []
for record in reader:
row = {}
account_id = self.get_iban(record['bank_number'], record['account_number'])
if account_id == "" or account_id in accounts:
# Filter out duplicates and empty ids
continue
row['id'] = account_id
accounts.append(account_id)
writer.writerow(row)
print(output.getvalue().strip())
return self.ReturnValue_NormalExit
def write_balance_file(self, output_folder_path, context_file_path):
process_result = subprocess.run(
self.build_command(self.AqBanking,
['listbal',
'-c', context_file_path]),
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL
)
fieldnames_input = ['ignore', 'bank_number', 'account_number', 'bank_name', 'account_name',
'booked_date', 'booked_time', 'booked_value', 'booked_currency',
'noted_date', 'noted_time', 'noted_value', 'noted_currency']
fieldnames_output = ['id', 'date', 'balance', 'unit']
reader = self.get_csv_reader(process_result, fieldnames_input)
output, writer = self.get_csv_writer(fieldnames_output)
for record in reader:
row = {}
row['id'] = self.get_iban(record['bank_number'], record['account_number'])
try:
row['balance'] = record['booked_value']
row['unit'] = record['booked_currency']
row['date'] = datetime.datetime.strptime(record['booked_date'], "%d.%m.%Y").strftime("%Y-%m-%d")
except:
try:
row['balance'] = record['noted_value']
row['unit'] = record['noted_currency']
row['date'] = datetime.datetime.strptime(record['noted_date'], "%d.%m.%Y").strftime("%Y-%m-%d")
except:
continue
writer.writerow(row)
output_file_path = os.path.join(output_folder_path, "balance.csv")
with open(output_file_path, 'w') as f:
f.write(output.getvalue())
def convert_transactions(self, aqbanking_output, generateHeader=True):
reader = csv.DictReader(aqbanking_output.splitlines(), dialect=AqDialectTransfairs)
fieldnames_output = ['date', 'mode', 'comment', 'payee', 'amount', 'unit']
output, writer = self.get_csv_writer(fieldnames_output, generateHeader)
for record in reader:
row = {}
row['date'] = record['date']
row['mode'] = record['purpose']
comment = ''
for i in range(1, 12):
comment = comment + record['purpose' + str(i)] + " "
row['comment'] = comment.strip()
row['payee'] = (record['remoteName'] + " " + record['remoteName1']).strip()
row['amount'] = record['value_value']
row['unit'] = record['value_currency']
writer.writerow(row)
return output.getvalue()
def download(self, output_folder_path, balance, openTerminal):
if not self.check_version():
return self.ReturnValue_InvalidVersion
with TemporaryContextFile() as context_file_path:
args = ['request',
'--ignoreUnsupported',
'--transactions',
'-c',
context_file_path
]
if balance:
args.append('--balance')
command = self.build_command(self.AqBanking, args)
if openTerminal:
shell_command = ['x-terminal-emulator', '-e']
shell_command.extend(command)
command = shell_command
process = subprocess.Popen(command)
process.wait()
output_folder_path = os.path.abspath(output_folder_path)
if not os.path.exists(output_folder_path):
os.makedirs(output_folder_path)
files = {}
for account in self.accounts.get_accounts():
process_result = subprocess.run(
self.build_command(self.AqBanking,
['listtrans',
'--bank=' + account.bank_number,
'--account=' + account.account_number,
'-c',
context_file_path
]),
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL
)
transactions = process_result.stdout.decode("utf-8")
output_file_path = os.path.join(output_folder_path, self.format_iban(account.iban.upper()) + ".csv")
if output_file_path in files:
files[output_file_path] = files[output_file_path] + '\n' + self.convert_transactions(transactions, False)
else:
files[output_file_path] = self.convert_transactions(transactions, True)
for path, content in files.items():
with open(path, 'w') as f:
f.write(content)
if balance:
self.write_balance_file(output_folder_path, context_file_path)
return self.ReturnValue_NormalExit
def main():
parser = argparse.ArgumentParser(prog='sabb', description='Skrooge AqBanking Bridge (SABB)')
# Global arguments
parser.add_argument('--version', '-v', action='version', version='%(prog)s ' + __VERSION__, help='Shows version information.')
subparsers = parser.add_subparsers(title='Commands', dest='command')
# Command: listaccounts
parser_listaccounts = subparsers.add_parser('listaccounts', help='Returns a list of accounts that can be queried with AqBanking.')
# Command: bulkdownload
parser_download = subparsers.add_parser('bulkdownload', help='Downloads all transactions into the given output folder')
parser_download.add_argument('--output_folder', required=True, help='The folder to store the csv files.')
parser_download.add_argument('--balance', required=False, action='store_true',
help='Additionally also download the current balance of all accounts and stores it in a "balance.csv" file in the output folder.')
args = parser.parse_args()
if (args.command == "listaccounts"):
return SABB().get_accounts()
elif (args.command == "bulkdownload"):
return SABB().download(args.output_folder, args.balance, True)
else:
parser.print_help()
return 1
return 0
if __name__ == "__main__":
sys.exit(main())
|