add MBNA parser

This commit is contained in:
Keenan Tims 2025-04-24 15:32:33 -07:00
parent f453222bf7
commit 81c7bb94a4
2 changed files with 37 additions and 3 deletions

View File

@ -42,7 +42,7 @@ class RogersBankParser(TransactionParser):
body = msg.get_body()
if body is None:
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:
raise TransactionParsingFailed("No matches for extraction RE")
amount = Decimal(matches[1])
@ -57,3 +57,37 @@ class RogersBankParser(TransactionParser):
notes="via email",
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"],
)

View File

@ -78,8 +78,8 @@ async def process_message(msg_b: bytes):
trans = parser.extract(msg)
info("Submitting transaction to Actual: %s", trans)
await submit_transaction(trans)
except TransactionParsingFailed:
warning("Unable to parse message")
except TransactionParsingFailed as e:
warning("Unable to parse message %s", e)
except Exception as e:
warning("Unexpected exception %s", e)