From 717d54fc63dac677f3c19728e0943fdccc510d07 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Wed, 1 Jan 2020 21:03:47 +0100 Subject: [PATCH] Abstracted some common data-types. --- fulbank/data.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ fulbank/fsb.py | 42 ++++++------------------------------------ 2 files changed, 54 insertions(+), 36 deletions(-) create mode 100644 fulbank/data.py diff --git a/fulbank/data.py b/fulbank/data.py new file mode 100644 index 0000000..36efe20 --- /dev/null +++ b/fulbank/data.py @@ -0,0 +1,48 @@ +import hashlib + +def _localname(type): + mod = type.__module__ + if mod.startswith("fulbank."): + mod = mod[8:] + return "%s.%s" % (mod, type.__name__) + +class account(object): + @property + def number(self): raise NotImplementedError("account.number") + @property + def name(self): raise NotImplementedError("account.name") + def transactions(self): raise NotImplementedError("account.transactions") + + def __repr__(self): + return "#<%s %s: %r>" % (_localname(type(self)), self.number, self.name) + +class txnaccount(account): + @property + def balance(self): raise NotImplementedError("txnaccount.balance") + @property + def clearing(self): raise NotImplementedError("txnaccount.clearing") + @property + def fullnumber(self): raise NotImplementedError("txnaccount.fullnumber") + +class cardaccount(account): + pass + +class transaction(object): + @property + def value(self): raise NotImplementedError("transaction.value") + @property + def message(self): raise NotImplementedError("transaction.message") + @property + def date(self): raise NotImplementedError("transaction.date") + + @property + def hash(self): + dig = hashlib.sha256() + dig.update(str(self.date.toordinal()).encode("ascii") + b"\0") + dig.update(self.message.encode("utf-8") + b"\0") + dig.update(str(self.value.amount).encode("ascii") + b"\0") + dig.update(self.value.currency.symbol.encode("ascii") + b"\0") + return dig.hexdigest() + + def __repr__(self): + return "#<%s %s: %r>" % (_localname(type(self)), self.value, self.message) diff --git a/fulbank/fsb.py b/fulbank/fsb.py index 85acbb9..58a54dc 100644 --- a/fulbank/fsb.py +++ b/fulbank/fsb.py @@ -1,7 +1,7 @@ -import json, http.cookiejar, binascii, time, datetime, pickle, hashlib +import json, http.cookiejar, binascii, time, datetime, pickle from urllib import request, parse from bs4 import BeautifulSoup as soup -from . import currency, auth +from . import currency, auth, data soupify = lambda cont: soup(cont, "html.parser") apibase = "https://online.swedbank.se/TDE_DAP_Portal_REST_WEB/api/" @@ -52,7 +52,7 @@ def getdsid(): def base64(data): return binascii.b2a_base64(data).decode("ascii").strip().rstrip("=") -class transaction(object): +class transaction(data.transaction): def __init__(self, account, data): self.account = account self._data = data @@ -68,19 +68,7 @@ class transaction(object): p = time.strptime(resolve(self._data, ("accountingDate",)), self._datefmt) return datetime.date(p.tm_year, p.tm_mon, p.tm_mday) - @property - def hash(self): - dig = hashlib.sha256() - dig.update(str(self.date.toordinal()).encode("ascii") + b"\0") - dig.update(self.message.encode("utf-8") + b"\0") - dig.update(str(self.value.amount).encode("ascii") + b"\0") - dig.update(self.value.currency.symbol.encode("ascii") + b"\0") - return dig.hexdigest() - - def __repr__(self): - return "#" % (self.value, self.message) - -class txnaccount(object): +class txnaccount(data.txnaccount): def __init__(self, sess, id, idata): self.sess = sess self.id = id @@ -116,10 +104,7 @@ class txnaccount(object): yield transaction(self, tx) page += 1 - def __repr__(self): - return "#" % (self.fullnumber, self.name) - -class cardtransaction(object): +class cardtransaction(data.transaction): def __init__(self, account, data): self.account = account self._data = data @@ -137,19 +122,7 @@ class cardtransaction(object): p = time.strptime(resolve(self._data, ("date",)), self._datefmt) return datetime.date(p.tm_year, p.tm_mon, p.tm_mday) - @property - def hash(self): - dig = hashlib.sha256() - dig.update(str(self.date.toordinal()).encode("ascii") + b"\0") - dig.update(self.message.encode("utf-8") + b"\0") - dig.update(str(self.value.amount).encode("ascii") + b"\0") - dig.update(self.value.currency.symbol.encode("ascii") + b"\0") - return dig.hexdigest() - - def __repr__(self): - return "#" % (self.value, self.message) - -class cardaccount(object): +class cardaccount(data.cardaccount): def __init__(self, sess, id, idata): self.sess = sess self.id = id @@ -183,9 +156,6 @@ class cardaccount(object): yield cardtransaction(self, tx) page += 1 - def __repr__(self): - return "#" % (self.number, self.name) - class session(object): def __init__(self, dsid): self.dsid = dsid -- 2.11.0