61 lines
2.4 KiB
Python
61 lines
2.4 KiB
Python
import json
|
|
import sys
|
|
import os
|
|
import sqlite3
|
|
|
|
# Füge das Hauptverzeichnis zum Python-Pfad hinzu, damit der Connector gefunden wird
|
|
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
|
|
from company_explorer_connector import handle_company_workflow
|
|
from db import get_leads, DB_PATH
|
|
|
|
def update_lead_enrichment(lead_id, data, status):
|
|
"""Aktualisiert einen Lead in der Datenbank mit neuen Enrichment-Daten und einem neuen Status."""
|
|
conn = sqlite3.connect(DB_PATH)
|
|
c = conn.cursor()
|
|
c.execute('UPDATE leads SET enrichment_data = ?, status = ? WHERE id = ?',
|
|
(json.dumps(data), status, lead_id))
|
|
conn.commit()
|
|
conn.close()
|
|
print(f"Lead {lead_id} aktualisiert. Neuer Status: {status}")
|
|
|
|
def run_sync():
|
|
"""
|
|
Haupt-Synchronisationsprozess.
|
|
Holt alle neuen Leads und stößt den Company Explorer Workflow für jeden an.
|
|
"""
|
|
# Hole nur die Leads, die wirklich neu sind und noch nicht verarbeitet wurden
|
|
leads_to_process = [lead for lead in get_leads() if lead['status'] == 'new']
|
|
|
|
print(f"Found {len(leads_to_process)} new leads to sync with Company Explorer.")
|
|
|
|
if not leads_to_process:
|
|
print("No new leads to process. Sync finished.")
|
|
return
|
|
|
|
for lead in leads_to_process:
|
|
company_name = lead['company_name']
|
|
print(f"\n--- Processing Lead ID: {lead['id']}, Company: '{company_name}' ---")
|
|
|
|
# Rufe den zentralen Workflow auf, den wir im Connector definiert haben
|
|
# Diese Funktion kümmert sich um alles: prüfen, erstellen, discovern, pollen, analysieren
|
|
result = handle_company_workflow(company_name)
|
|
|
|
# Bereite die Daten für die Speicherung in der DB vor
|
|
enrichment_data = {
|
|
"sync_status": result.get("status"),
|
|
"ce_id": result.get("data", {}).get("id") if result.get("data") else None,
|
|
"message": result.get("message"),
|
|
"ce_data": result.get("data")
|
|
}
|
|
|
|
# Setze den finalen Status und speichere die Daten in der DB
|
|
update_lead_enrichment(lead['id'], enrichment_data, status='synced')
|
|
|
|
print("\n--- Sync cycle finished. ---")
|
|
|
|
if __name__ == "__main__":
|
|
# Dieser Block ermöglicht es, das Skript direkt für Debugging-Zwecke auszuführen
|
|
print("Running manual sync...")
|
|
run_sync()
|
|
print("Manual sync finished.")
|