- Force UTF-8 stdout with line buffering to fix Windows encoding error - Use batch_create/batch_update in Airtable sync (10x faster) - Fix CampaignID field type (string, not int) - Fix FechaEntrada → Fecha in leads query Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
96 lines
3.7 KiB
Python
96 lines
3.7 KiB
Python
from pyairtable import Api
|
|
from datetime import datetime
|
|
import config
|
|
|
|
|
|
class AirtableClient:
|
|
def __init__(self):
|
|
self.api = Api(config.AIRTABLE_TOKEN)
|
|
self.leads = self.api.table(config.AIRTABLE_BASE_ID, config.LEADS_TABLE)
|
|
self.campaigns = self.api.table(config.AIRTABLE_BASE_ID, config.CAMPAIGNS_TABLE)
|
|
|
|
def get_active_campaigns(self) -> list[dict]:
|
|
"""Lee todas las campañas activas desde 'Google Ads Campaigns'."""
|
|
records = self.campaigns.all(formula="{Status}='Activa'")
|
|
result = []
|
|
for r in records:
|
|
f = r["fields"]
|
|
result.append({
|
|
"airtable_id": r["id"],
|
|
"curso": f.get("Campaign Name", "Sin nombre"),
|
|
"cursoid_text": str(f.get("CursoID Text", "")).strip(),
|
|
"google_campaign_id": str(f.get("CampaignID", "")).strip(),
|
|
"ppl": float(f.get("PPL", 0)),
|
|
"capping_mensual": int(f.get("CapTotalMes", 0)),
|
|
"cpa_maximo": float(f.get("CPAMax", 0)),
|
|
"margen_objetivo": float(f.get("MargenObjetivo", 0)),
|
|
})
|
|
return [c for c in result if c["google_campaign_id"]] # descartar sin ID
|
|
|
|
def sync_campaigns_from_google_ads(self, google_campaigns: list[dict]) -> dict:
|
|
"""
|
|
Sincroniza campañas de Google Ads → Airtable.
|
|
- Crea las campañas nuevas con el status real de Google Ads.
|
|
- Actualiza nombre y/o status si han cambiado.
|
|
Devuelve un resumen con listas de campañas creadas y actualizadas.
|
|
"""
|
|
STATUS_MAP = {"ENABLED": "Activa", "PAUSED": "Pausada"}
|
|
|
|
all_records = self.campaigns.all()
|
|
at_by_gid = {
|
|
str(r["fields"].get("CampaignID", "")).strip(): r
|
|
for r in all_records
|
|
if r["fields"].get("CampaignID")
|
|
}
|
|
|
|
created = []
|
|
updated = []
|
|
to_create = []
|
|
to_update = [] # lista de (record_id, changes, name)
|
|
|
|
for gc in google_campaigns:
|
|
gid = gc["id"]
|
|
at_status = STATUS_MAP.get(gc["status"], "Pausada")
|
|
at_record = at_by_gid.get(gid)
|
|
|
|
if at_record is None:
|
|
to_create.append({"Campaign Name": gc["name"], "CampaignID": gid, "Status": at_status})
|
|
created.append(gc["name"])
|
|
else:
|
|
changes = {}
|
|
if at_record["fields"].get("Campaign Name") != gc["name"]:
|
|
changes["Campaign Name"] = gc["name"]
|
|
if at_record["fields"].get("Status") != at_status:
|
|
changes["Status"] = at_status
|
|
if changes:
|
|
to_update.append((at_record["id"], changes, gc["name"]))
|
|
updated.append(gc["name"])
|
|
|
|
# Crear en lotes de 10
|
|
for i in range(0, len(to_create), 10):
|
|
self.campaigns.batch_create(to_create[i:i+10])
|
|
|
|
# Actualizar en lotes de 10
|
|
for i in range(0, len(to_update), 10):
|
|
batch = [{"id": rid, "fields": changes} for rid, changes, _ in to_update[i:i+10]]
|
|
self.campaigns.batch_update(batch)
|
|
|
|
return {"created": created, "updated": updated}
|
|
|
|
def get_leads_this_month(self, cursoid_text: str) -> int:
|
|
"""
|
|
Cuenta todos los leads validados del mes actual para un curso.
|
|
Filtra por attr_cursoid (que coincide con CursoID Text de la campaña).
|
|
"""
|
|
now = datetime.now()
|
|
mes_inicio = f"{now.year}-{now.month:02d}-01"
|
|
|
|
formula = (
|
|
f"AND("
|
|
f"{{attr_cursoid}}='{cursoid_text}',"
|
|
f"{{creado}}>='{mes_inicio}'"
|
|
f")"
|
|
)
|
|
records = self.leads.all(formula=formula)
|
|
return len(records)
|