#!/usr/bin/env python3 from pathlib import Path 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" 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 # -------------------------------------------------- # 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 # -------------------------------------------------- base_dir = Path.home() / "Transaktionen" if not base_dir.exists(): base_dir = Path(__file__).parent / "Transaktionen" # 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, USER, PIN, "https://fints.ing.de/fints/", product_id="PythonFinTS" ) 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), 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 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=[ "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 ) # -------------------------------------------------- # 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()