#!/usr/bin/env python3 # Dieses Skript durchsucht alle JSON-Dateien im aktuellen Verzeichnis nach Bestellungen und extrahiert relevante Informationen zu bestimmten Produkten. Die Ergebnisse werden in einer CSV-Datei gespeichert. # Maintainer: peignerpausz # Version: 0.1 import json import csv import glob OUTPUT_FILE = "activation_overview.csv" TARGET_PRODUCTS = { "Spam Control", "Mailversand Basic", "Mailversand Pro", "VIP Support", "Domain Protection" } def get_customer_name(order): customer = order.get("customer", {}) firstname = customer.get("name", "").strip() lastname = customer.get("surname", "").strip() return f"{firstname} {lastname}".strip() def get_customer_email(order): customer = order.get("customer", {}) return customer.get("email", "").strip() def get_domains(order): domains = set() for item in order.get("data", []): for field in item.get("additional_fields", []): if field.get("key") == "domain_name": domains.add(field.get("value")) if item.get("name") and "." in str(item.get("name")): domains.add(item.get("name")) return domains def get_domain_for_product(product_item, all_domains): # Domain direkt am Produkt hinterlegt for field in product_item.get("additional_fields", []): if field.get("key") == "domain_name": return field.get("value") # Sonst erste gefundene Domain der Bestellung verwenden if all_domains: return sorted(all_domains)[0] return "" rows = [] for filename in glob.glob("*.json"): try: with open(filename, "r", encoding="utf-8") as f: order = json.load(f) order_no = str(order.get("id", "")) customer_no = order.get("customer_no", "") order_date = order.get("created_at", "") if order_date: order_date = order_date.split("T")[0] customer_name = get_customer_name(order) customer_email = get_customer_email(order) domains = get_domains(order) customer_name_prompted = False customer_email_prompted = False for item in order.get("data", []): product = item.get("product_name") if product not in TARGET_PRODUCTS: continue if not customer_name and not customer_name_prompted: customer_name = input( f"No customer name found for customer {customer_no} " f"(order {order_no}). " "Enter a name to add to the CSV: " ).strip() customer_name_prompted = True if not customer_email and not customer_email_prompted: customer_email = input( f"No customer email found for customer {customer_no} " f"(order {order_no}). " "Enter an email to add to the CSV: " ).strip() customer_email_prompted = True domain = get_domain_for_product(item, domains) rows.append({ "order_no": order_no, "order_date": order_date, "customer_no": customer_no, "customer_name": customer_name, "customer_email": customer_email, "product": product, "domain": domain }) except Exception as e: print(f"Fehler bei {filename}: {e}") with open(OUTPUT_FILE, "w", newline="", encoding="utf-8") as csvfile: writer = csv.DictWriter( csvfile, fieldnames=[ "order_no", "order_date", "customer_no", "customer_name", "customer_email", "product", "domain" ] ) writer.writeheader() writer.writerows(rows) print(f"{len(rows)} Einträge nach {OUTPUT_FILE} exportiert.")