Files
Spendr/app/importers.py
T
2026-11-17 21:48:36 +01:00

160 lines
5.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Parsers for bank CSV exports (ING NL, Revolut) into a common shape."""
import csv
import io
from dataclasses import dataclass, field
from datetime import date, datetime
from decimal import Decimal, InvalidOperation
@dataclass
class ParsedTx:
date: date
amount: Decimal # negative = out
counterparty: str
description: str
counter_iban: str | None = None
currency: str = "EUR"
raw_type: str = ""
@dataclass
class ParseResult:
fmt: str
txs: list[ParsedTx] = field(default_factory=list)
own_iban: str | None = None
balance: Decimal | None = None
balance_date: date | None = None
skipped: int = 0
def to_decimal(s: str) -> Decimal:
s = (s or "").strip().replace(" ", "").replace("€", "").replace("−", "-")
if not s:
return Decimal("0")
if "," in s and "." in s: # 1.234,56 or 1,234.56
if s.rfind(",") > s.rfind("."):
s = s.replace(".", "").replace(",", ".")
else:
s = s.replace(",", "")
elif "," in s:
s = s.replace(",", ".")
try:
return Decimal(s)
except InvalidOperation as e:
raise ValueError(f"Cannot parse amount {s!r}") from e
def _date(s: str) -> date:
s = s.strip()
for fmt in ("%Y%m%d", "%Y-%m-%d", "%d-%m-%Y", "%d/%m/%Y"):
try:
return datetime.strptime(s[:10] if "-" in s[:10] else s, fmt).date()
except ValueError:
continue
raise ValueError(f"Cannot parse date {s!r}")
def _rows(text: str):
first = text.splitlines()[0] if text else ""
delim = ";" if first.count(";") > first.count(",") else ","
reader = csv.DictReader(io.StringIO(text), delimiter=delim)
headers = [h.strip().lower() for h in (reader.fieldnames or [])]
rows = [{(k or "").strip().lower(): (v or "").strip() for k, v in r.items()} for r in reader]
return headers, rows
def _g(row: dict, *names: str) -> str:
for n in names:
if n in row and row[n] is not None:
return row[n]
return ""
def parse(content: bytes) -> ParseResult:
text = content.decode("utf-8-sig", errors="replace")
headers, rows = _rows(text)
hs = set(headers)
if {"started date", "amount", "state"} <= hs:
return parse_revolut(rows)
if "naam / omschrijving" in hs or "name / description" in hs:
return parse_ing(rows)
raise ValueError("Unrecognised CSV. Expected an ING (NL) or Revolut export. Headers found: "
+ ", ".join(headers))
def parse_ing(rows: list[dict]) -> ParseResult:
res = ParseResult(fmt="ing")
latest: tuple[date, int] | None = None
for i, row in enumerate(rows):
d = _date(_g(row, "datum", "date"))
direction = _g(row, "af bij", "debit/credit").lower()
amount = to_decimal(_g(row, "bedrag (eur)", "amount (eur)"))
if direction in ("af", "debit"):
amount = -abs(amount)
else:
amount = abs(amount)
own = _g(row, "rekening", "account") or None
res.own_iban = res.own_iban or own
res.txs.append(ParsedTx(
date=d,
amount=amount,
counterparty=_g(row, "naam / omschrijving", "name / description"),
description=_g(row, "mededelingen", "notifications"),
counter_iban=_g(row, "tegenrekening", "counterparty") or None,
raw_type=_g(row, "mutatiesoort", "transaction type") or _g(row, "code"),
))
bal = _g(row, "saldo na mutatie", "resulting balance")
# ING lists newest first: the first row of the newest date carries the current balance
if bal and (latest is None or d > latest[0]):
latest = (d, i)
res.balance, res.balance_date = to_decimal(bal), d
return res
def parse_revolut(rows: list[dict]) -> ParseResult:
res = ParseResult(fmt="revolut")
latest: str | None = None
for row in rows:
if _g(row, "state").upper() != "COMPLETED":
res.skipped += 1
continue
product = _g(row, "product").lower()
if product and product != "current": # savings vaults, deposits, etc.
res.skipped += 1
continue
ts = _g(row, "completed date") or _g(row, "started date")
amount = to_decimal(_g(row, "amount")) - to_decimal(_g(row, "fee"))
desc = _g(row, "description")
res.txs.append(ParsedTx(
date=_date(ts),
amount=amount,
counterparty=desc,
description=desc,
currency=(_g(row, "currency") or "EUR").upper(),
raw_type=_g(row, "type"),
))
bal = _g(row, "balance")
if bal and (_g(row, "currency") or "EUR").upper() == "EUR" and (latest is None or ts >= latest):
latest = ts
res.balance, res.balance_date = to_decimal(bal), _date(ts)
return res
def parse_enablebanking(t: dict) -> ParsedTx:
"""Map one Enable Banking transaction object to ParsedTx."""
amt = Decimal(str(t["transaction_amount"]["amount"]))
out = t.get("credit_debit_indicator") == "DBIT"
party = (t.get("creditor") if out else t.get("debtor")) or {}
party_acc = (t.get("creditor_account") if out else t.get("debtor_account")) or {}
d = t.get("booking_date") or t.get("value_date") or t.get("transaction_date")
info = t.get("remittance_information") or []
return ParsedTx(
date=date.fromisoformat(d),
amount=-abs(amt) if out else abs(amt),
counterparty=party.get("name") or (info[0] if info else ""),
description=" ".join(info),
counter_iban=party_acc.get("iban"),
currency=t["transaction_amount"].get("currency", "EUR"),
raw_type=(t.get("bank_transaction_code") or {}).get("description") or "",
)