import os import json import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options # Verzeichnisse input_file = "wfrp4e-eis-actors-extracted.json" split_directory = "actors_split" translated_directory = "actors_split_translated" merged_file_path = "wfrp4e-eis-actors-merged.json" os.makedirs(split_directory, exist_ok=True) os.makedirs(translated_directory, exist_ok=True) # Pfad zu deinem Chrome Profil (anpassen) chrome_profile_path = "C:/Users/Alzer/AppData/Local/Google/Chrome/User Data/Default/" # Deine Chat-URL (ersetzen) chat_url = "https://chatgpt.com/c/68271195-7a84-800d-b66a-7a73edfb121a" # Deine echte Chat-URL hier einfügen # Geckodriver-Pfad (anpassen, falls nötig) chromedriver_path = "C:/Windows/System32/chromedriver.exe" cookie_file = "chrome_cookies.json" # Schritt 1: Datei in Einträge aufteilen try: with open(input_file, 'r', encoding='utf-8') as file: data = json.load(file) print(f"{len(data)} Einträge gefunden. Aufteilen der Datei...") for actor_id, actor_data in data.items(): # Eintrag speichern split_file_path = os.path.join(split_directory, f"{actor_id}.json") with open(split_file_path, 'w', encoding='utf-8') as split_file: json.dump({actor_id: actor_data}, split_file, ensure_ascii=False, indent=4) print(f"Datei in {len(data)} Einträge aufgeteilt.") except FileNotFoundError as e: print(f"Fehler beim Laden der Datei: {e}") exit(1) except json.JSONDecodeError as e: print(f"Fehler beim Dekodieren der JSON-Datei: {e}") exit(1) # Schritt 2: Automatisierter Upload und Download try: # Chrome-Optionen options = Options() options.add_argument(f"--user-data-dir={chrome_profile_path}") options.add_argument("--profile-directory=Default") options.add_argument("--disable-blink-features=AutomationControlled") options.add_argument("--no-sandbox") options.add_argument("--disable-dev-shm-usage") options.headless = False # Setze auf True, wenn du den Browser nicht sehen willst # Webdriver starten service = Service(chromedriver_path) driver = webdriver.Chrome(service=service, options=options) driver.get("https://chat.openai.com/") # Cookies importieren, falls vorhanden if os.path.exists(cookie_file): with open(cookie_file, "r") as file: cookies = json.load(file) for cookie in cookies: # Cookie-Domain korrigieren if "domain" in cookie: cookie["domain"] = ".chat.openai.com" try: driver.add_cookie(cookie) except Exception as e: print(f"⚠️ Fehler beim Setzen des Cookies: {e}") print("✅ Cookies importiert.") # Seite neu laden, um Cookies zu aktivieren driver.get("https://chat.openai.com/") # Cookies speichern (nur beim ersten Mal notwendig) if not os.path.exists(cookie_file): # Warten, bis du dich eingeloggt hast input("🔑 Bitte logge dich ein und drücke dann Enter, wenn du bereit bist...") with open(cookie_file, "w") as file: cookies = driver.get_cookies() for cookie in cookies: if "domain" in cookie: cookie["domain"] = ".chat.openai.com" json.dump(cookies, file) print("✅ Cookies gespeichert.") # Warten, bis du dich eingeloggt hast input("🔑 Bitte logge dich ein und drücke dann Enter, wenn du bereit bist...") translated_entries = {} for file_name in os.listdir(split_directory): if file_name.endswith(".json"): file_path = os.path.abspath(os.path.join(split_directory, file_name)) try: # Datei-Upload starten upload_button = driver.find_element(By.XPATH, "//input[@type='file']") upload_button.send_keys(file_path) print(f"✅ Datei hochgeladen: {file_name}") # Warte auf die Verarbeitung (anpassen je nach Dateigröße) time.sleep(30) # Übersetzten Inhalt extrahieren translated_text = driver.page_source # Datei speichern translated_file_path = os.path.join(translated_directory, file_name) with open(translated_file_path, 'w', encoding='utf-8') as out_file: out_file.write(translated_text) print(f"✅ Übersetzte Datei gespeichert: {file_name}") # Eintrag zum zusammenführen vorbereiten with open(translated_file_path, 'r', encoding='utf-8') as translated_file: try: translated_entry = json.load(translated_file) translated_entries.update(translated_entry) except json.JSONDecodeError as e: print(f"❌ Fehler beim Laden der Datei {file_name}: {e}") except Exception as e: print(f"❌ Fehler beim Datei-Upload: {e}") except Exception as e: print(f"❌ Fehler beim Starten des Chrome-Browsers: {e}") exit(1) finally: # Browser schließen, auch bei Fehlern try: driver.quit() print("✅ Browser geschlossen.") except NameError: print("⚠️ Kein aktiver Browser gefunden.") # Schritt 3: Validierung und Zusammenführung print("🔍 Überprüfe und zusammenführe die übersetzten Einträge...") valid_entries = {} for entry_id, entry_data in translated_entries.items(): try: # Überprüfung der JSON-Integrität json.dumps(entry_data) valid_entries[entry_id] = entry_data except (TypeError, ValueError) as e: print(f"❌ Ungültiger Eintrag ({entry_id}): {e}") # Gesamte Datei zusammenführen und speichern with open(merged_file_path, 'w', encoding='utf-8') as merged_file: json.dump(valid_entries, merged_file, ensure_ascii=False, indent=4) print(f"✅ Zusammengeführte Datei gespeichert: {merged_file_path}")