Compare commits

..

4 Commits

Author SHA1 Message Date
93778beb31 bump version 0.1.2 2025-11-23 22:19:35 -08:00
6bb281098d add version print 2025-11-23 22:19:23 -08:00
35381a4c1d add CIBC purchase parser 2025-11-23 22:16:35 -08:00
e690a2c4d2 Fix Scotia parser wrong direction 2025-11-23 22:16:10 -08:00
3 changed files with 41 additions and 3 deletions

View File

@@ -178,11 +178,16 @@ class CIBCParser(TransactionParser):
flags=re.MULTILINE, flags=re.MULTILINE,
) )
PURCHASE_EXTRACT_RE = re.compile(
r"made a purchase with your CIBC.*ending in (?P<account>\d{4}) for \$(?P<amount>\d+\.\d{2}) at (?P<payee>.*?).<br", # noqa: E501
re.MULTILINE,
)
def __init__(self, account_map: dict[int, UUID]): def __init__(self, account_map: dict[int, UUID]):
self._account_map = account_map self._account_map = account_map
def match(self, msg: Message) -> bool: def match(self, msg: Message) -> bool:
return msg["From"] == "CIBC Banking <mailbox.noreply@cibc.com>" return msg["From"] == "CIBC Banking <Mailbox.noreply@cibc.com>"
def extract_payment(self, msg: EmailMessage): def extract_payment(self, msg: EmailMessage):
content = self.get_content(msg) content = self.get_content(msg)
@@ -209,10 +214,37 @@ class CIBCParser(TransactionParser):
imported_id=msg["Message-ID"], imported_id=msg["Message-ID"],
) )
def extract_purchase(self, msg: EmailMessage) -> Optional[Transaction]:
content = self.get_content(msg)
matches = self.PURCHASE_EXTRACT_RE.search(content)
if matches is None:
raise TransactionParsingFailed("no matches for extraction RE")
amount = Decimal(matches["amount"].replace(",", "")) * -1
date = parse_email_time(msg["Date"]).date()
account_ref = int(matches["account"])
if account_ref not in self._account_map:
warning("Account %s not in account map, skipping transaction", account_ref)
return None
account_id = self._account_map[account_ref]
return Transaction(
account=account_id,
date=date,
amount=amount,
payee=matches["payee"],
notes="via email",
imported_id=msg["Message-ID"],
)
def extract(self, msg: EmailMessage) -> Optional[Transaction]: def extract(self, msg: EmailMessage) -> Optional[Transaction]:
match msg["Subject"]: match msg["Subject"]:
case "New payment to your credit card": case "New payment to your credit card":
return self.extract_payment(msg) return self.extract_payment(msg)
case "New purchase on your credit card":
return self.extract_purchase(msg)
return None return None
@@ -235,7 +267,7 @@ class ScotiaBankParser(TransactionParser):
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["amount"].replace(",", "")) amount = Decimal(matches["amount"].replace(",", "")) * -1
date = parse_email_time(msg["Date"]).date() date = parse_email_time(msg["Date"]).date()
if matches["account"] not in self._account_map: if matches["account"] not in self._account_map:

View File

@@ -1,6 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
import asyncio import asyncio
import email.policy import email.policy
import importlib.metadata
import importlib.util import importlib.util
import logging import logging
import os import os
@@ -129,6 +130,11 @@ class App:
def main(): def main():
logging.basicConfig(level=getenv("LOG_LEVEL", "INFO")) logging.basicConfig(level=getenv("LOG_LEVEL", "INFO"))
logging.info(
"actual-imap-parser %s starting up",
importlib.metadata.version("actual-imap-poll"),
)
app_config = AppConfig() app_config = AppConfig()
logging.debug("Config: %s", app_config)
app = App(app_config, CONFIG_PATH) app = App(app_config, CONFIG_PATH)
asyncio.run(app.run()) asyncio.run(app.run())

View File

@@ -1,6 +1,6 @@
[project] [project]
name = "actual-imap-poll" name = "actual-imap-poll"
version = "0.1.1" version = "0.1.2"
description = "Poll an IMAP mailbox looking for transactions to submit to Actual Budget" description = "Poll an IMAP mailbox looking for transactions to submit to Actual Budget"
authors = [ authors = [
{name = "Keenan Tims",email = "ktims@gotroot.ca"} {name = "Keenan Tims",email = "ktims@gotroot.ca"}