Scripts zusammengefügt
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
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"
|
||||
cookie_file = "chrome_cookies.json"
|
||||
|
||||
# Geckodriver-Pfad (anpassen, falls nötig)
|
||||
chromedriver_path = "/Pfad/zu/deinem/chromedriver"
|
||||
|
||||
# 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:
|
||||
driver.add_cookie(cookie)
|
||||
print("✅ Cookies importiert.")
|
||||
|
||||
driver.get("https://chat.openai.com/")
|
||||
|
||||
# Cookies speichern (nur beim ersten Mal notwendig)
|
||||
if not os.path.exists(cookie_file):
|
||||
time.sleep(30) # Wartezeit für manuelles Einloggen
|
||||
with open(cookie_file, "w") as file:
|
||||
json.dump(driver.get_cookies(), file)
|
||||
print("✅ Cookies gespeichert.")
|
||||
|
||||
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}")
|
||||
Reference in New Issue
Block a user