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 #!/usr/bin/env python3
from pathlib import Path from pathlib import Path
from datetime import date, timedelta from datetime import date, timedelta, datetime
from fints.client import FinTS3PinTanClient from fints.client import FinTS3PinTanClient
import csv import csv
import json import json
from urllib.request import urlopen
# --------------------------------------------------
# Konfiguration laden # Konfiguration laden
# --------------------------------------------------
config = {} config = {}
# Zuerst Home-Verzeichnis prüfen
config_file = Path.home() / ".ing.conf" config_file = Path.home() / ".ing.conf"
# Falls nicht vorhanden, Datei neben dem Skript verwenden
if not config_file.exists(): if not config_file.exists():
config_file = Path(__file__).parent / ".ing.conf" 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) key, value = line.split("=", 1)
config[key] = value config[key] = value
# --------------------------------------------------
# ING-Konfiguration
# --------------------------------------------------
USER = config["ING_USER"] USER = config["ING_USER"]
PIN = config["ING_PIN"] PIN = config["ING_PIN"]
TARGET_IBAN = config["ING_IBAN"] TARGET_IBAN = config["ING_IBAN"]
IOBROKER_HOST = config["IOBROKER_HOST"]
IOBROKER_PORT = config["IOBROKER_PORT"]
IOBROKER_DP_TRANSACTIONS = config["IOBROKER_DP_TRANSACTIONS"]
BLZ = "50010517" BLZ = "50010517"
# --------------------------------------------------
# Exportverzeichnis bestimmen # Exportverzeichnis bestimmen
export_dir = Path.home() / "logs" # --------------------------------------------------
if not export_dir.exists(): base_dir = Path.home() / "Transaktionen"
export_dir = Path(__file__).parent / "logs"
# Verzeichnis bei Bedarf anlegen if not base_dir.exists():
export_dir.mkdir(parents=True, exist_ok=True) base_dir = Path(__file__).parent / "Transaktionen"
CSV_FILE = export_dir / "transactions.csv" # Kalenderwoche bestimmen
JSON_FILE = export_dir / "transactions.json" 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( client = FinTS3PinTanClient(
BLZ, BLZ,
@ -55,12 +82,20 @@ client = FinTS3PinTanClient(
export_data = [] export_data = []
# --------------------------------------------------
# Transaktionen abrufen
# --------------------------------------------------
with client: with client:
account_found = False
for account in client.get_sepa_accounts(): for account in client.get_sepa_accounts():
if account.iban != TARGET_IBAN: if account.iban != TARGET_IBAN:
continue continue
account_found = True
transactions = client.get_transactions( transactions = client.get_transactions(
account, account,
start_date=date.today() - timedelta(days=7), start_date=date.today() - timedelta(days=7),
@ -83,8 +118,17 @@ with client:
break break
if not account_found:
raise RuntimeError(
f"IBAN nicht gefunden: {TARGET_IBAN}"
)
# --------------------------------------------------
# CSV schreiben # CSV schreiben
# --------------------------------------------------
with open(CSV_FILE, "w", newline="", encoding="utf-8") as csvfile: with open(CSV_FILE, "w", newline="", encoding="utf-8") as csvfile:
writer = csv.DictWriter( writer = csv.DictWriter(
csvfile, csvfile,
fieldnames=[ fieldnames=[
@ -100,11 +144,68 @@ with open(CSV_FILE, "w", newline="", encoding="utf-8") as csvfile:
writer.writeheader() writer.writeheader()
writer.writerows(export_data) writer.writerows(export_data)
# --------------------------------------------------
# JSON schreiben # JSON schreiben
# --------------------------------------------------
with open(JSON_FILE, "w", encoding="utf-8") as jsonfile: with open(JSON_FILE, "w", encoding="utf-8") as jsonfile:
json.dump( json.dump(
export_data, export_data,
jsonfile, jsonfile,
ensure_ascii=False, ensure_ascii=False,
indent=2 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()