From b76f5fe991dddde8dbad002af426e35c034a9a30 Mon Sep 17 00:00:00 2001 From: Floke Date: Fri, 18 Apr 2025 06:40:17 +0000 Subject: [PATCH] bugfix --- brancheneinstufung.py | 63 ++++++++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 22 deletions(-) diff --git a/brancheneinstufung.py b/brancheneinstufung.py index 9616ea33..277df831 100644 --- a/brancheneinstufung.py +++ b/brancheneinstufung.py @@ -658,7 +658,7 @@ def process_wiki_updates_from_chatgpt(sheet_handler, data_processor, row_limit=N - Wenn nein (U keine URL, U==M, oder U ungültig): LÖSCHT den Inhalt von U und markiert S als 'X (Invalid Suggestion)'. Verarbeitet maximal row_limit Zeilen. """ - debug_print("Starte Modus: Wiki-Updates (URL-Validierung & Löschen ungültiger Vorschläge)...") # Titel angepasst + debug_print("Starte Modus: Wiki-Updates (URL-Validierung & Löschen ungültiger Vorschläge)...") if not sheet_handler.load_data(): return all_data = sheet_handler.get_all_data_with_headers() @@ -666,17 +666,34 @@ def process_wiki_updates_from_chatgpt(sheet_handler, data_processor, row_limit=N header_rows = 5 data_rows = all_data[header_rows:] - # Indizes holen (wie zuvor) - required_keys = [...] # Alle benötigten Schlüssel wie vorher - col_indices = {}; all_keys_found = True - for key in required_keys: idx = COLUMN_MAP.get(key); col_indices[key] = idx; if idx is None: debug_print(f"FEHLER: Key '{key}' fehlt!"); all_keys_found = False - if not all_keys_found: return debug_print("Breche ab.") + # --- Indizes holen (Korrigierte Schleife) --- + required_keys = [ + "Chat Wiki Konsistenzprüfung", "Chat Vorschlag Wiki Artikel", "Wiki URL", + "Wikipedia Timestamp", "Wiki Verif. Timestamp", "Timestamp letzte Prüfung", "Version", + "ReEval Flag" # Spalte A für ReEval-Flag + ] + col_indices = {} + all_keys_found = True + # --- KORRIGIERTE SCHLEIFE --- + for key in required_keys: + idx = COLUMN_MAP.get(key) + col_indices[key] = idx # Speichere den Index (kann auch None sein) + if idx is None: + debug_print(f"FEHLER: Schlüssel '{key}' für Spaltenindex fehlt in COLUMN_MAP!") + all_keys_found = False + # --- ENDE KORRIGIERTE SCHLEIFE --- + + if not all_keys_found: + debug_print("Breche Wiki-Updates ab, da Spaltenindizes fehlen.") + return + # --- Ende Indizes holen --- all_sheet_updates = [] processed_rows_count = 0 # Zählt jetzt beide Arten von Updates updated_url_count = 0 cleared_suggestion_count = 0 - error_rows_count = 0 + error_rows_count = 0 # Behalte Fehlerzählung bei + # wiki_scraper wird nicht mehr benötigt for idx, row in enumerate(data_rows): row_num_in_sheet = idx + header_rows + 1 @@ -685,40 +702,44 @@ def process_wiki_updates_from_chatgpt(sheet_handler, data_processor, row_limit=N debug_print(f"Zeilenlimit ({row_limit}) erreicht.") break - def get_value(key): idx = col_indices.get(key); return row[idx] if idx is not None and len(row) > idx else "" - konsistenz_s = get_value("Chat Wiki Konsistenzprüfung"); vorschlag_u = get_value("Chat Vorschlag Wiki Artikel"); url_m = get_value("Wiki URL") + def get_value(key): + index = col_indices.get(key) + if index is not None and len(row) > index: return row[index] + return "" + + konsistenz_s = get_value("Chat Wiki Konsistenzprüfung") + vorschlag_u = get_value("Chat Vorschlag Wiki Artikel") + url_m = get_value("Wiki URL") # Bedingungen prüfen is_update_candidate = False; new_url = "" konsistenz_s_upper = konsistenz_s.strip().upper() vorschlag_u_cleaned = vorschlag_u.strip() url_m_cleaned = url_m.strip() - condition1_status_nok = konsistenz_s_upper not in ["OK", "X (UPDATED)", "X (URL COPIED)", "X (INVALID SUGGESTION)", ""] # Schließe auch neuen Status aus + condition1_status_nok = konsistenz_s_upper not in ["OK", "X (UPDATED)", "X (URL COPIED)", "X (INVALID SUGGESTION)", ""] condition2_u_is_url = vorschlag_u_cleaned.lower().startswith(("http://", "https://")) and "wikipedia.org/wiki/" in vorschlag_u_cleaned.lower() condition3_u_differs_m = False; condition4_u_is_valid = False - if condition1_status_nok and condition2_u_is_url: # Nur wenn Status NOK und U eine URL ist, prüfen wir weiter + if condition1_status_nok and condition2_u_is_url: new_url = vorschlag_u_cleaned condition3_u_differs_m = new_url != url_m_cleaned if condition3_u_differs_m: - debug_print(f"Zeile {row_num_in_sheet}: Potenzieller Kandidat. Prüfe Validität von URL: {new_url}...") - condition4_u_is_valid = is_valid_wikipedia_article_url(new_url) - if not condition4_u_is_valid: debug_print(f"Zeile {row_num_in_sheet}: URL '{new_url}' ist KEIN valider Artikel.") + # debug_print(f"Zeile {row_num_in_sheet}: Potenzieller Kandidat. Prüfe Validität von URL: {new_url}...") # Weniger Lärm + condition4_u_is_valid = is_valid_wikipedia_article_url(new_url) # Annahme: Funktion existiert + # if not condition4_u_is_valid: debug_print(f"Zeile {row_num_in_sheet}: URL '{new_url}' ist KEIN valider Artikel.") # Weniger Lärm is_update_candidate = condition1_status_nok and condition2_u_is_url and condition3_u_differs_m and condition4_u_is_valid - # --- NEU: Behandlung ungültiger/unpassender Vorschläge --- - # Trigger, wenn Status NOK war, aber KEIN valides Update durchgeführt werden kann - # (weil U keine URL, U==M oder U ungültige URL) + # Behandlung ungültiger/unpassender Vorschläge clear_invalid_suggestion = condition1_status_nok and not is_update_candidate # --- Verarbeitung des Kandidaten ODER Löschen des Vorschlags --- if is_update_candidate: - # --- Fall 1: Gültiges Update durchführen --- + # Fall 1: Gültiges Update durchführen debug_print(f"Zeile {row_num_in_sheet}: Update-Kandidat VALIDIERUNG ERFOLGREICH. Setze ReEval-Flag und bereite Updates vor.") processed_rows_count += 1 updated_url_count += 1 - # Updates sammeln (M, S="X (URL Copied)", U="URL übernommen", Timestamps/Version löschen, A="x") + # Updates sammeln (M, S, U, Timestamps/Version löschen, A setzen) m_l=sheet_handler._get_col_letter(col_indices["Wiki URL"]+1); s_l=sheet_handler._get_col_letter(col_indices["Chat Wiki Konsistenzprüfung"]+1); u_l=sheet_handler._get_col_letter(col_indices["Chat Vorschlag Wiki Artikel"]+1); an_l=sheet_handler._get_col_letter(col_indices["Wikipedia Timestamp"]+1); ax_l=sheet_handler._get_col_letter(col_indices["Wiki Verif. Timestamp"]+1); ao_l=sheet_handler._get_col_letter(col_indices["Timestamp letzte Prüfung"]+1); ap_l=sheet_handler._get_col_letter(col_indices["Version"]+1); a_l=sheet_handler._get_col_letter(col_indices["ReEval Flag"]+1) row_updates = [ {'range': f'{m_l}{row_num_in_sheet}', 'values': [[new_url]]}, @@ -733,7 +754,7 @@ def process_wiki_updates_from_chatgpt(sheet_handler, data_processor, row_limit=N all_sheet_updates.extend(row_updates) elif clear_invalid_suggestion: - # --- Fall 2: Ungültigen Vorschlag löschen/markieren --- + # Fall 2: Ungültigen Vorschlag löschen/markieren debug_print(f"Zeile {row_num_in_sheet}: Status S war '{konsistenz_s}', aber Vorschlag U ('{vorschlag_u_cleaned}') ist ungültig/identisch. Lösche U und setze Status S.") processed_rows_count += 1 # Zähle auch diese Aktion cleared_suggestion_count += 1 @@ -746,8 +767,6 @@ def process_wiki_updates_from_chatgpt(sheet_handler, data_processor, row_limit=N ] all_sheet_updates.extend(row_updates) - # Kein 'else' hier, Zeilen, wo S=OK oder bereits bearbeitet wurde, werden ignoriert - # --- Batch Update am Ende --- if all_sheet_updates: debug_print(f"BEREIT ZUM SENDEN: Batch-Update für {processed_rows_count} verarbeitete Zeilen ({len(all_sheet_updates)} Zellen)...")