Fix setup_airtable_meta_tables.py: UTF-8 stdout and idempotent re-runs
Windows console encoding crashed the script right after creating the first table; make it re-runnable by skipping tables that already exist instead of aborting entirely.
This commit is contained in:
parent
9239e2f67f
commit
f8cbd5f29e
@ -16,6 +16,8 @@ import sys
|
||||
import requests
|
||||
from dotenv import load_dotenv
|
||||
|
||||
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
||||
|
||||
load_dotenv()
|
||||
|
||||
TOKEN = os.environ.get("AIRTABLE_TOKEN", "")
|
||||
@ -41,24 +43,30 @@ def create_table(name: str, fields: list) -> dict:
|
||||
|
||||
existing = requests.get(META_URL, headers=HEADERS, timeout=15)
|
||||
existing.raise_for_status()
|
||||
existing_names = {t["name"] for t in existing.json().get("tables", [])}
|
||||
|
||||
if "Meta Ads Campaigns" in existing_names or "MetaCampaignMes" in existing_names:
|
||||
print("Error: 'Meta Ads Campaigns' or 'MetaCampaignMes' already exist in this base. Aborting.")
|
||||
sys.exit(1)
|
||||
existing_tables = {t["name"]: t for t in existing.json().get("tables", [])}
|
||||
|
||||
print(f"Creando tablas en la base {BASE_ID}...")
|
||||
|
||||
campaigns_table = create_table("Meta Ads Campaigns", [
|
||||
{"name": "Campaign Name", "type": "singleLineText"},
|
||||
{"name": "CampaignID", "type": "singleLineText"},
|
||||
{"name": "CursoID Text", "type": "singleLineText"},
|
||||
{
|
||||
"name": "Status", "type": "singleSelect",
|
||||
"options": {"choices": [{"name": "Activa"}, {"name": "Pausada"}]},
|
||||
},
|
||||
{"name": "PPL", "type": "number", "options": {"precision": 2}},
|
||||
])
|
||||
if "Meta Ads Campaigns" in existing_tables:
|
||||
campaigns_table = existing_tables["Meta Ads Campaigns"]
|
||||
print(f" = Table 'Meta Ads Campaigns' already exists (id={campaigns_table['id']}), skipping")
|
||||
else:
|
||||
campaigns_table = create_table("Meta Ads Campaigns", [
|
||||
{"name": "Campaign Name", "type": "singleLineText"},
|
||||
{"name": "CampaignID", "type": "singleLineText"},
|
||||
{"name": "CursoID Text", "type": "singleLineText"},
|
||||
{
|
||||
"name": "Status", "type": "singleSelect",
|
||||
"options": {"choices": [{"name": "Activa"}, {"name": "Pausada"}]},
|
||||
},
|
||||
{"name": "PPL", "type": "number", "options": {"precision": 2}},
|
||||
])
|
||||
|
||||
if "MetaCampaignMes" in existing_tables:
|
||||
print(f" = Table 'MetaCampaignMes' already exists (id={existing_tables['MetaCampaignMes']['id']}), skipping")
|
||||
print("""
|
||||
""" + "="*55 + "\n Nada que hacer: ambas tablas ya existen.\n" + "="*55)
|
||||
sys.exit(0)
|
||||
|
||||
metacampaignmes_table = create_table("MetaCampaignMes", [
|
||||
{"name": "Mes", "type": "singleLineText"},
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user