From cb9f2096cd6ae8bf8a894cc06600526448b584ca Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Fri, 5 Apr 2019 08:01:00 +0200 Subject: change to use matrix in place of xmpp --- transfers.py | 55 +++++++++++++++++++++++-------------------------------- 1 file changed, 23 insertions(+), 32 deletions(-) (limited to 'transfers.py') diff --git a/transfers.py b/transfers.py index 3271832..63e051f 100755 --- a/transfers.py +++ b/transfers.py @@ -6,8 +6,8 @@ import logging from fints.client import FinTS3PinTanClient import peewee as p -from slixmpp import ClientXMPP -import transaction_details +from playhouse.fields import PickledField +from matrix_client.client import MatrixClient logging.basicConfig(level=logging.INFO) @@ -25,21 +25,20 @@ db = p.SqliteDatabase(args.db) class Transaction(p.Model): created_date = p.DateField(default=datetime.date.today) accountnumber = p.CharField() + identifier = p.CharField() sepa_entry_date = p.DateField() - sepa_amount = p.DecimalField() - sepa_transaction_details = p.CharField() - sepa_status = p.CharField() - sepa_currency = p.CharField() + details = PickledField() - def format_message(self): - details = transaction_details.parse_transaction_details(self.sepa_transaction_details) - return '{transaction[accountnumber]} {details[Buchungstext]}: {transaction[sepa_amount]} ' \ - '{transaction[sepa_currency]} {details[Auftraggeber Name]} {details[Verwendungszweck]}'.format( - transaction=self._data, details=details) + def format_message_html(self): + return """\ +{accountnumber} {details[posting_text]}: +{details[amount].amount} {details[amount].currency} +{details[applicant_name]} {details[purpose]} {details[end_to_end_reference]} +""".format(**self._data) class Meta: database = db - indexes = ((('accountnumber', 'sepa_entry_date', 'sepa_transaction_details'), True),) + indexes = ((('accountnumber', 'identifier'), True),) db.create_tables([Transaction], True) @@ -59,17 +58,14 @@ accounts = f.get_sepa_accounts() account = list(filter(lambda a: a.accountnumber == config['bank']['accountnumber'], accounts))[0] logging.info("select transactions on account %s starting from %s", account.accountnumber, last_transaction_date) -transactions = f.get_statement(account, last_transaction_date, datetime.date.today()) +transactions = f.get_transactions(account, last_transaction_date, datetime.date.today()) new_transactions = [] for t in transactions: - filter_pred = { - 'accountnumber': config['bank']['accountnumber'], - 'sepa_entry_date': datetime.date.fromordinal(t.data['entry_date'].toordinal()), - 'sepa_transaction_details': t.data['transaction_details'], - } - data = {'sepa_amount': t.data['amount'].amount, 'sepa_status': t.data['status'], - 'sepa_currency': t.data['currency']} + date = datetime.date.fromordinal(t.data['entry_date'].toordinal()), + identifier = "{applicant_bin}-{applicant_iban}-{mydate}".format(mydate=date, **t.data) + filter_pred = {'accountnumber': config['bank']['accountnumber'], 'identifier': identifier} + data = {'details': t.data, 'sepa_entry_date': date} try: record = Transaction.get(**filter_pred) record.update(data) @@ -82,18 +78,13 @@ for t in transactions: record.save() if len(new_transactions) > 0: - cl = ClientXMPP(config['jabber']['jid'], config['jabber']['password']) + client = MatrixClient(config['matrix']['host']) + token = client.login(config['matrix']['user'], config['matrix']['pass']) + room = client.join_room(config['matrix']['room']) - def session_start(event): - for t in new_transactions: - message = t.format_message() - cl.send_message(config['jabber']['recipient'], message, mtype='chat') - logging.info("Sent info %s", message) - cl.disconnect() + for t in new_transactions: + message = t.format_message_html() + room.send_html(message) - - cl.add_event_handler('session_start', session_start) - cl.add_event_handler('killed', lambda *_: cl.loop.stop()) - cl.connect() - cl.process() + client.logout() -- cgit v1.2.1