ING/transactions.py

110 lines
No EOL
2.4 KiB
Python

#!/usr/bin/env python3
from pathlib import Path
from datetime import date, timedelta
from fints.client import FinTS3PinTanClient
import csv
import json
# Konfiguration laden
config = {}
config_file = Path.home() / ".ing.conf"
if not config_file.exists():
config_file = Path(__file__).parent / ".ing.conf"
with open(config_file, encoding="utf-8") as f:
for line in f:
line = line.strip()
if not line or line.startswith("#"):
continue
if "=" not in line:
continue
key, value = line.split("=", 1)
config[key] = value
USER = config["ING_USER"]
PIN = config["ING_PIN"]
TARGET_IBAN = config["ING_IBAN"]
BLZ = "50010517"
# Exportverzeichnis bestimmen
export_dir = Path.home() / "logs"
if not export_dir.exists():
export_dir = Path(__file__).parent / "logs"
# Verzeichnis bei Bedarf anlegen
export_dir.mkdir(parents=True, exist_ok=True)
CSV_FILE = export_dir / "transactions.csv"
JSON_FILE = export_dir / "transactions.json"
client = FinTS3PinTanClient(
BLZ,
USER,
PIN,
"https://fints.ing.de/fints/",
product_id="PythonFinTS"
)
export_data = []
with client:
for account in client.get_sepa_accounts():
if account.iban != TARGET_IBAN:
continue
transactions = client.get_transactions(
account,
start_date=date.today() - timedelta(days=7),
end_date=date.today()
)
for transaction in transactions:
data = transaction.data
row = {
"date": str(data.get("date", "")),
"amount": str(data.get("amount", "")),
"posting_text": data.get("posting_text", ""),
"applicant_name": data.get("applicant_name", ""),
"purpose": data.get("purpose", "")
}
export_data.append(row)
break
# CSV schreiben
with open(CSV_FILE, "w", newline="", encoding="utf-8") as csvfile:
writer = csv.DictWriter(
csvfile,
fieldnames=[
"date",
"amount",
"posting_text",
"applicant_name",
"purpose"
],
delimiter=";"
)
writer.writeheader()
writer.writerows(export_data)
# JSON schreiben
with open(JSON_FILE, "w", encoding="utf-8") as jsonfile:
json.dump(
export_data,
jsonfile,
ensure_ascii=False,
indent=2
)