Ablage als CSV und Json

This commit is contained in:
hubobel 2026-06-06 17:28:03 +02:00
parent 0eb6640317
commit e02145ea58

View file

@ -1,16 +1,22 @@
#!/usr/bin/env python3
from pathlib import Path
from datetime import date, timedelta
from datetime import date, timedelta, datetime
from fints.client import FinTS3PinTanClient
import csv
import json
from urllib.request import urlopen
# --------------------------------------------------
# Konfiguration laden
# --------------------------------------------------
config = {}
# Zuerst Home-Verzeichnis prüfen
config_file = Path.home() / ".ing.conf"
# Falls nicht vorhanden, Datei neben dem Skript verwenden
if not config_file.exists():
config_file = Path(__file__).parent / ".ing.conf"
@ -27,23 +33,44 @@ with open(config_file, encoding="utf-8") as f:
key, value = line.split("=", 1)
config[key] = value
# --------------------------------------------------
# ING-Konfiguration
# --------------------------------------------------
USER = config["ING_USER"]
PIN = config["ING_PIN"]
TARGET_IBAN = config["ING_IBAN"]
IOBROKER_HOST = config["IOBROKER_HOST"]
IOBROKER_PORT = config["IOBROKER_PORT"]
IOBROKER_DP_TRANSACTIONS = config["IOBROKER_DP_TRANSACTIONS"]
BLZ = "50010517"
# --------------------------------------------------
# Exportverzeichnis bestimmen
export_dir = Path.home() / "logs"
# --------------------------------------------------
if not export_dir.exists():
export_dir = Path(__file__).parent / "logs"
base_dir = Path.home() / "Transaktionen"
# Verzeichnis bei Bedarf anlegen
export_dir.mkdir(parents=True, exist_ok=True)
if not base_dir.exists():
base_dir = Path(__file__).parent / "Transaktionen"
CSV_FILE = export_dir / "transactions.csv"
JSON_FILE = export_dir / "transactions.json"
# Kalenderwoche bestimmen
year, week, _ = datetime.now().isocalendar()
year_dir = base_dir / str(year)
csv_dir = year_dir / "csv"
json_dir = year_dir / "json"
# Verzeichnisse anlegen
csv_dir.mkdir(parents=True, exist_ok=True)
json_dir.mkdir(parents=True, exist_ok=True)
CSV_FILE = csv_dir / f"transactions_{year}_KW{week:02d}.csv"
JSON_FILE = json_dir / f"transactions_{year}_KW{week:02d}.json"
# --------------------------------------------------
# ING-Verbindung
# --------------------------------------------------
client = FinTS3PinTanClient(
BLZ,
@ -55,12 +82,20 @@ client = FinTS3PinTanClient(
export_data = []
# --------------------------------------------------
# Transaktionen abrufen
# --------------------------------------------------
with client:
account_found = False
for account in client.get_sepa_accounts():
if account.iban != TARGET_IBAN:
continue
account_found = True
transactions = client.get_transactions(
account,
start_date=date.today() - timedelta(days=7),
@ -83,8 +118,17 @@ with client:
break
if not account_found:
raise RuntimeError(
f"IBAN nicht gefunden: {TARGET_IBAN}"
)
# --------------------------------------------------
# CSV schreiben
# --------------------------------------------------
with open(CSV_FILE, "w", newline="", encoding="utf-8") as csvfile:
writer = csv.DictWriter(
csvfile,
fieldnames=[
@ -100,11 +144,68 @@ with open(CSV_FILE, "w", newline="", encoding="utf-8") as csvfile:
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
)
)
# --------------------------------------------------
# Statistiken berechnen
# --------------------------------------------------
transaction_count = len(export_data)
week_sum = 0.0
week_expenses = 0.0
for transaction in transactions:
amount = float(transaction.data["amount"].amount)
week_sum += amount
if amount < 0:
week_expenses += abs(amount)
# --------------------------------------------------
# An ioBroker senden
# --------------------------------------------------
base_url = f"http://{IOBROKER_HOST}:{IOBROKER_PORT}/set"
urlopen(
f"{base_url}/javascript.0.Variablen.Konto_Transaktionen"
f"?value={transaction_count}"
).read()
urlopen(
f"{base_url}/javascript.0.Variablen.Konto_Wochensumme"
f"?value={round(week_sum, 2)}"
).read()
urlopen(
f"{base_url}/javascript.0.Variablen.Konto_Wochenausgaben"
f"?value={round(week_expenses, 2)}"
).read()
# --------------------------------------------------
# Erfolgsmeldung
# --------------------------------------------------
transaction_count = len(export_data)
url = (
f"http://{IOBROKER_HOST}:{IOBROKER_PORT}"
f"/set/{IOBROKER_DP_TRANSACTIONS}"
f"?value={transaction_count}"
)
urlopen(url).read()