add MBNA parser
This commit is contained in:
parent
f453222bf7
commit
81c7bb94a4
36
parsers.py
36
parsers.py
@ -42,7 +42,7 @@ class RogersBankParser(TransactionParser):
|
|||||||
body = msg.get_body()
|
body = msg.get_body()
|
||||||
if body is None:
|
if body is None:
|
||||||
raise TransactionParsingFailed("No body of message found")
|
raise TransactionParsingFailed("No body of message found")
|
||||||
matches = RogersBankParser.EXTRACT_RE.search(body.as_string())
|
matches = self.EXTRACT_RE.search(body.as_string())
|
||||||
if matches is None:
|
if matches is None:
|
||||||
raise TransactionParsingFailed("No matches for extraction RE")
|
raise TransactionParsingFailed("No matches for extraction RE")
|
||||||
amount = Decimal(matches[1])
|
amount = Decimal(matches[1])
|
||||||
@ -57,3 +57,37 @@ class RogersBankParser(TransactionParser):
|
|||||||
notes="via email",
|
notes="via email",
|
||||||
imported_id=msg["Message-ID"],
|
imported_id=msg["Message-ID"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class MBNAParser(TransactionParser):
|
||||||
|
EXTRACT_RE = re.compile(
|
||||||
|
r"A purchase of \$(\d+\.\d{2}) from ([^<]+) was made at (\d{1,2}:\d{2} (AM|PM)) UTC on (\d{4}-\d{2}-\d{2})" # noqa: E501
|
||||||
|
)
|
||||||
|
|
||||||
|
def __init__(self, account_id: UUID):
|
||||||
|
self.account_id = account_id
|
||||||
|
|
||||||
|
def match(self, msg: Message):
|
||||||
|
return (
|
||||||
|
msg["From"] == "MBNA Notifications <noreply@mbna.ca>"
|
||||||
|
and msg["Subject"] == "MBNA - Transaction Alert"
|
||||||
|
)
|
||||||
|
|
||||||
|
def extract(self, msg: EmailMessage) -> Transaction:
|
||||||
|
body = msg.get_body()
|
||||||
|
if body is None:
|
||||||
|
raise TransactionParsingFailed("No body of message found")
|
||||||
|
matches = self.EXTRACT_RE.search(body.as_string())
|
||||||
|
if matches is None:
|
||||||
|
raise TransactionParsingFailed("No matches for extraction RE")
|
||||||
|
amount = Decimal(matches[1])
|
||||||
|
payee = matches[2]
|
||||||
|
date = matches[5]
|
||||||
|
return Transaction(
|
||||||
|
account=self.account_id,
|
||||||
|
date=date,
|
||||||
|
amount=amount,
|
||||||
|
payee=payee,
|
||||||
|
notes="via email",
|
||||||
|
imported_id=msg["Message-ID"],
|
||||||
|
)
|
||||||
|
@ -78,8 +78,8 @@ async def process_message(msg_b: bytes):
|
|||||||
trans = parser.extract(msg)
|
trans = parser.extract(msg)
|
||||||
info("Submitting transaction to Actual: %s", trans)
|
info("Submitting transaction to Actual: %s", trans)
|
||||||
await submit_transaction(trans)
|
await submit_transaction(trans)
|
||||||
except TransactionParsingFailed:
|
except TransactionParsingFailed as e:
|
||||||
warning("Unable to parse message")
|
warning("Unable to parse message %s", e)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
warning("Unexpected exception %s", e)
|
warning("Unexpected exception %s", e)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user