From fec71826a97e25967cc14a98e29136843049a307 Mon Sep 17 00:00:00 2001 From: Floke Date: Fri, 30 May 2025 14:40:34 +0000 Subject: [PATCH] bugfix --- brancheneinstufung.py | 125 ++++++++++++++++++++---------------------- 1 file changed, 60 insertions(+), 65 deletions(-) diff --git a/brancheneinstufung.py b/brancheneinstufung.py index 78140777..9cd56120 100644 --- a/brancheneinstufung.py +++ b/brancheneinstufung.py @@ -4506,60 +4506,51 @@ class DataProcessor: # parent_account_name_d ist bereits oben in _process_single_row gelesen worden url_for_extraction = None - source_of_wiki_data_origin_log_msg = "Tochter (Initial)" - additional_info_for_af_col = "" + source_of_wiki_data_origin_log_msg = "Tochter (Initial)" + additional_info_for_af_col = "" - # --- Logik zur Bestimmung der zu verwendenden Wiki-URL --- - # Priorität 1: Manuell gepflegter Parent in D, wenn Tochter-Wiki R leer ist - if (not current_wiki_url_r or current_wiki_url_r.lower() == 'k.a.') and \ - (parent_account_name_d and parent_account_name_d.lower() != 'k.a.'): - self.logger.info(f" Zeile {row_num_in_sheet}: R leer, D ('{parent_account_name_d}') gesetzt. Suche Wiki für Parent D via SerpAPI.") - try: - potential_url = serp_wikipedia_lookup(parent_account_name_d, website=None) # SERP API KEY BENÖTIGT! - if potential_url and potential_url.lower() not in ["k.a.", "kein artikel gefunden"] and not potential_url.startswith("FEHLER"): - url_for_extraction = potential_url - source_of_wiki_data_origin_log_msg = f"Parent D ('{parent_account_name_d}')" - updates.append({'range': f'{self.sheet_handler._get_col_letter(COLUMN_MAP["Chat Wiki Konsistenzpruefung"] + 1)}{row_num_in_sheet}', 'values': [["INFO_PARENT_AUS_D"]]}) - additional_info_for_af_col = f"INFO: Wiki-URL von Parent (aus D): {parent_account_name_d} via SerpAPI. " - else: - self.logger.warning(f" -> Kein Wiki-Artikel für Parent D '{parent_account_name_d}' gefunden.") - additional_info_for_af_col = f"WARN: Kein Wiki für Parent D '{parent_account_name_d}' gefunden. " - except Exception as e_d_lookup_re: - self.logger.error(f"Fehler bei Wiki-Suche für Parent D '{parent_account_name_d}': {e_d_lookup_re}") - additional_info_for_af_col = f"ERR: Suche Parent D '{parent_account_name_d}' fehlgeschlagen. " + # WICHTIG: Wenn R leer ist, IMMER ZUERST versuchen, Parent-URLs (D dann O) zu finden + if not current_wiki_url_r or current_wiki_url_r.lower() == 'k.a.': + # Priorität 1: Manuell gepflegter Parent in D + if parent_account_name_d and parent_account_name_d.lower() != 'k.a.': + self.logger.info(f" Zeile {row_num_in_sheet}: R leer, D ('{parent_account_name_d}') gesetzt. Suche Wiki für Parent D via SerpAPI.") + try: + potential_url = serp_wikipedia_lookup(parent_account_name_d, website=None) # SERP API KEY BENÖTIGT! + if potential_url and potential_url.lower() not in ["k.a.", "kein artikel gefunden"] and not potential_url.startswith("FEHLER"): + url_for_extraction = potential_url + source_of_wiki_data_origin_log_msg = f"Parent D ('{parent_account_name_d}')" + updates.append({'range': f'{self.sheet_handler._get_col_letter(COLUMN_MAP["Chat Wiki Konsistenzpruefung"] + 1)}{row_num_in_sheet}', 'values': [["INFO_PARENT_AUS_D"]]}) + additional_info_for_af_col = f"INFO: Wiki-URL von Parent (aus D): {parent_account_name_d} via SerpAPI. " + else: + self.logger.warning(f" -> Kein Wiki-Artikel für Parent D '{parent_account_name_d}' gefunden.") + additional_info_for_af_col = f"WARN: Kein Wiki für Parent D '{parent_account_name_d}' gefunden. " + except Exception as e_d_lookup_re: + self.logger.error(f"Fehler bei Wiki-Suche für Parent D '{parent_account_name_d}': {e_d_lookup_re}") + additional_info_for_af_col = f"ERR: Suche Parent D '{parent_account_name_d}' fehlgeschlagen. " - # Priorität 2: Systemvorschlag Parent O, wenn R leer UND D nicht erfolgreich war/leer ist - if (url_for_extraction is None) and \ - (not current_wiki_url_r or current_wiki_url_r.lower() == 'k.a.') and \ - (system_suggested_parent_o and system_suggested_parent_o.lower() != 'k.a.'): # HIER WAR system_suggested_parent_name_o - self.logger.info(f" Zeile {row_num_in_sheet}: R leer, D nicht genutzt/erfolglos. O ('{system_suggested_parent_o}') gesetzt. Suche Wiki für Parent O via SerpAPI.") - try: - potential_url = serp_wikipedia_lookup(system_suggested_parent_o, website=None) # SERP API KEY BENÖTIGT! - if potential_url and potential_url.lower() not in ["k.a.", "kein artikel gefunden"] and not potential_url.startswith("FEHLER"): - # Hier keine explizite _validate_article, da serp_wikipedia_lookup schon filtert - # und wir davon ausgehen, dass der Parent-Name in O gut genug ist. - url_for_extraction = potential_url - source_of_wiki_data_origin_log_msg = f"Parent O ('{system_suggested_parent_o}')" - updates.append({'range': f'{self.sheet_handler._get_col_letter(COLUMN_MAP["Chat Wiki Konsistenzpruefung"] + 1)}{row_num_in_sheet}', 'values': [["INFO_PARENT_AUS_O"]]}) - additional_info_for_af_col += f"INFO: Wiki-URL von Parent (aus O): {system_suggested_parent_o} via SerpAPI. " - else: - self.logger.warning(f" -> Kein Wiki-Artikel für Parent O '{system_suggested_parent_o}' gefunden.") - additional_info_for_af_col += f"WARN: Kein Wiki für Parent O '{system_suggested_parent_o}' gefunden. " - except Exception as e_o_lookup_re: - self.logger.error(f"Fehler bei Wiki-Suche für Parent O '{system_suggested_parent_o}': {e_o_lookup_re}") - additional_info_for_af_col += f"ERR: Suche Parent O '{system_suggested_parent_o}' fehlgeschlagen. " - - # Priorität 3: Standard Tochter-Wiki-Logik (wenn keine Parent-URL ermittelt wurde oder R schon gefüllt war und reevaluiert wird) - if url_for_extraction is None or \ - (current_wiki_url_r and current_wiki_url_r.lower() != 'k.a.' and not current_wiki_url_r.startswith("FEHLER") and \ - (force_reeval or self._get_cell_value_safe(row_data, "Chat Wiki Konsistenzpruefung").strip().upper() == "X (URL COPIED)")): - - # Bestimme, ob eine neue Suche für die Tochter nötig ist oder die vorhandene URL in R verwendet wird - search_for_daughter_wiki_needed_local = False # Lokale Variable für diesen Block - + # Priorität 2: Systemvorschlag Parent O (nur wenn D nicht erfolgreich war) + if url_for_extraction is None and \ + (system_suggested_parent_o and system_suggested_parent_o.lower() != 'k.a.'): + self.logger.info(f" Zeile {row_num_in_sheet}: R leer, D nicht genutzt/erfolglos. O ('{system_suggested_parent_o}') gesetzt. Suche Wiki für Parent O via SerpAPI.") + try: + potential_url = serp_wikipedia_lookup(system_suggested_parent_o, website=None) # SERP API KEY BENÖTIGT! + if potential_url and potential_url.lower() not in ["k.a.", "kein artikel gefunden"] and not potential_url.startswith("FEHLER"): + url_for_extraction = potential_url + source_of_wiki_data_origin_log_msg = f"Parent O ('{system_suggested_parent_o}')" + updates.append({'range': f'{self.sheet_handler._get_col_letter(COLUMN_MAP["Chat Wiki Konsistenzpruefung"] + 1)}{row_num_in_sheet}', 'values': [["INFO_PARENT_AUS_O"]]}) + additional_info_for_af_col += f"INFO: Wiki-URL von Parent (aus O): {system_suggested_parent_o} via SerpAPI. " + else: + self.logger.warning(f" -> Kein Wiki-Artikel für Parent O '{system_suggested_parent_o}' gefunden.") + additional_info_for_af_col += f"WARN: Kein Wiki für Parent O '{system_suggested_parent_o}' gefunden. " + except Exception as e_o_lookup_re: + self.logger.error(f"Fehler bei Wiki-Suche für Parent O '{system_suggested_parent_o}': {e_o_lookup_re}") + additional_info_for_af_col += f"ERR: Suche Parent O '{system_suggested_parent_o}' fehlgeschlagen. " + + # Priorität 3: Tochter-Wiki-Verarbeitung (wenn keine Parent-URL gefunden wurde oder R bereits gefüllt war und Re-Eval/Reparse ansteht) + if url_for_extraction is None: # Wenn nach Parent-Suchen immer noch keine URL da ist + search_for_daughter_wiki_needed_local = False status_ac_indicates_reparse_local = self._get_cell_value_safe(row_data, "Chat Wiki Konsistenzpruefung").strip().upper() == "X (URL COPIED)" timestamp_z_is_empty_local = not self._get_cell_value_safe(row_data, "Wikipedia Timestamp").strip() - r_url_is_valid_looking_local = current_wiki_url_r and \ "wikipedia.org/wiki/" in current_wiki_url_r.lower() and \ current_wiki_url_r.lower() not in ["k.a.", "kein artikel gefunden", "fehler bei suche"] @@ -4568,14 +4559,14 @@ class DataProcessor: self.logger.info(f" Zeile {row_num_in_sheet}: Status AC ist 'X (URL COPIED)'. Starte neue Suche für Tochter '{company_name}'.") search_for_daughter_wiki_needed_local = True elif force_reeval: - if r_url_is_valid_looking_local and url_for_extraction is None: # Nur wenn keine Parent-URL gefunden wurde + if r_url_is_valid_looking_local: self.logger.info(f" Zeile {row_num_in_sheet}: Re-Eval Modus. Nutze vorhandene Tochter-URL (R): {current_wiki_url_r}") - url_for_extraction = current_wiki_url_r # Setze die URL hier + url_for_extraction = current_wiki_url_r source_of_wiki_data_origin_log_msg = "Tochter (aus R, Re-Eval)" - elif url_for_extraction is None: # Keine Parent-URL und R ist schlecht + else: self.logger.info(f" Zeile {row_num_in_sheet}: Re-Eval Modus. Tochter-URL (R) leer/ungültig. Starte neue Suche für Tochter '{company_name}'.") search_for_daughter_wiki_needed_local = True - elif timestamp_z_is_empty_local and url_for_extraction is None: + elif timestamp_z_is_empty_local: if r_url_is_valid_looking_local: self.logger.info(f" Zeile {row_num_in_sheet}: Wikipedia Timestamp (Z) fehlt. Nutze vorhandene Tochter-URL (R): {current_wiki_url_r}") url_for_extraction = current_wiki_url_r @@ -4583,12 +4574,11 @@ class DataProcessor: else: self.logger.info(f" Zeile {row_num_in_sheet}: Wikipedia Timestamp (Z) fehlt und Tochter-URL (R) leer/ungültig. Starte neue Suche für Tochter '{company_name}'.") search_for_daughter_wiki_needed_local = True - elif not r_url_is_valid_looking_local and url_for_extraction is None: + elif not r_url_is_valid_looking_local: # Fallback, wenn Z nicht leer, aber R schlecht self.logger.info(f" Zeile {row_num_in_sheet}: Tochter-URL (R) ist ungültig ('{current_wiki_url_r}'). Starte neue Suche für Tochter '{company_name}'.") search_for_daughter_wiki_needed_local = True - elif url_for_extraction is None : # Wenn bisher keine URL gefunden/gesetzt wurde - self.logger.debug(f" Zeile {row_num_in_sheet}: Wiki-Verarbeitung für Tochter nicht nötig (Z gesetzt, R gültig, kein Re-Eval/Reparse, kein Parent-Fall).") - + else: # R ist vorhanden und Timestamp Z ist gesetzt, kein Re-Eval/Reparse -> keine Wiki-Aktion für Tochter nötig + self.logger.debug(f" Zeile {row_num_in_sheet}: Wiki-Verarbeitung für Tochter nicht nötig (Timestamp Z gesetzt, R gültig, kein Re-Eval/Reparse, kein Parent-Fall).") if search_for_daughter_wiki_needed_local: self.logger.info(f" -> Suche nach Wikipedia-Artikel für Tochter '{company_name}'...") @@ -4605,13 +4595,18 @@ class DataProcessor: self.logger.error(f"Fehler bei Wiki-Suche für Tochter '{company_name}': {e_tochter_suche_psr}") url_for_extraction = f"Fehler Suche Tochter: {str(e_tochter_suche_psr)[:50]}" - # Fallback, falls immer noch keine URL da ist, aber R eine hatte (und nicht reevaluiert wurde) - if url_for_extraction is None and current_wiki_url_r and \ - current_wiki_url_r.lower() not in ['k.a.'] and not current_wiki_url_r.startswith("FEHLER") and \ - not (force_reeval or self._get_cell_value_safe(row_data, "Chat Wiki Konsistenzpruefung").strip().upper() == "X (URL COPIED)"): - url_for_extraction = current_wiki_url_r - source_of_wiki_data_origin_log_msg = "Tochter (aus R, Fallback)" - self.logger.debug(f" Zeile {row_num_in_sheet}: Fallback auf bestehende Tochter-URL aus R: {url_for_extraction}") + elif current_wiki_url_r and current_wiki_url_r.lower() != 'k.a.' and not current_wiki_url_r.startswith("FEHLER") and \ + (force_reeval or self._get_cell_value_safe(row_data, "Chat Wiki Konsistenzpruefung").strip().upper() == "X (URL COPIED)"): + # Dieser Fall tritt ein, wenn R eine URL enthält, aber force_reeval oder "X (URL COPIED)" aktiv ist + # und url_for_extraction bereits durch eine Parent-Suche gesetzt wurde. + # In diesem Szenario hat die Parent-URL Vorrang, aber wir loggen, dass die Tochter-URL auch da war. + if url_for_extraction: # Wenn schon eine Parent-URL gefunden wurde + self.logger.info(f" Zeile {row_num_in_sheet}: Parent-URL '{url_for_extraction}' hat Vorrang. Tochter-URL in R ('{current_wiki_url_r}') wird nicht für Extraktion verwendet, obwohl Re-Eval/Reparse aktiv.") + else: # Sollte nicht passieren, wenn die obere Logik korrekt ist, aber als Sicherheitsnetz + self.logger.info(f" Zeile {row_num_in_sheet}: Re-Eval/Reparse aktiv. Nutze bestehende Tochter-URL aus R: {current_wiki_url_r}") + url_for_extraction = current_wiki_url_r + source_of_wiki_data_origin_log_msg = "Tochter (aus R, Re-Eval/Reparse)" + # --- DATENEXTRAKTION ---