#!/usr/bin/env python3 """Attio Companies CSV export -> Apify input -> Companies CSV import keyed by Record ID. prepare: read an Attio Companies view exported with Import / Export -> "Export view as CSV", keep companies that have a domain and are missing a Phone or Email (or all with --all), dedupe, skip social/directory domains, write websites.json for shephouse/website-contact-enricher. merge: read the export + the Actor's dataset JSON, write attio_import.csv with Record ID + only the NEW values, for Companies -> Import / Export -> Import CSV. Why Record ID: Attio's help says Record IDs are included in every records export, and that on import "If an ID matches an existing record during import, it will be updated; if not, it will be skipped." Matching on Domains instead would create a new company for any row that matches nothing, and Domains is multi-value (imported domains are added to existing ones), so Domains is NOT written. Why blanks: Attio's import guide says "Empty values are skipped and won't overwrite existing data." So a company that already has a Phone (or Email) gets an EMPTY cell for it here: the existing value is left alone, never rewritten. Phones: Attio phone-number attributes need E.164 (+ country code); only the Actor's e164 values are used. No-reply emails are dropped; email is left blank when the domain has no MX. Columns: "Record ID", "Domains" (several domains in one cell are split on , ; or whitespace and the first usable one is used), plus your own company attributes, default "Phone" and "Email" (--phone-col / --email-col). Standard library only. No token or login is read or stored. """ import csv, json, re, sys SKIP = {"facebook.com", "instagram.com", "linkedin.com", "linktr.ee", "yelp.com", "google.com", "x.com", "twitter.com", "wixsite.com", "business.site"} NOREPLY = re.compile(r"^(no-?reply|do-?not-?reply|mailer-daemon)@", re.I) UUID = re.compile(r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", re.I) def opt(flag, default): return sys.argv[sys.argv.index(flag) + 1] if flag in sys.argv else default PHONE, EMAIL = opt("--phone-col", "Phone"), opt("--email-col", "Email") def norm(d): d = (d or "").strip().lower() d = re.sub(r"^https?://", "", d).split("/")[0].split("?")[0].split(":")[0] return d[4:] if d.startswith("www.") else d def col(row, *names): for n in names: if row.get(n): return row[n].strip() return "" def domain(row): """First usable domain from the Domains cell (may hold several).""" for part in re.split(r"[,;\s]+", col(row, "Domains", "Domain", "Website")): d = norm(part) if d and "." in d: return d return "" def rid(row): """Return (record_id, problem).""" i = col(row, "Record ID", "Record Id", "record_id") if not i: return "", "no Record ID (export the view from Attio; don't delete the column)" if not UUID.match(i): return "", f"Record ID {i!r} is not in Attio's UUID form; re-export" return i.lower(), "" def read_export(path): with open(path, newline="", encoding="utf-8-sig") as f: return list(csv.DictReader(f)) def label(row): return col(row, "Name", "Company name") or col(row, "Record ID") or "(blank row)" def prepare(export, out="websites.json", all_rows=False): rows = read_export(export) seen, sites, skipped = set(), [], [] for r in rows: who = label(r) _, problem = rid(r) d = domain(r) if problem: skipped.append((who, problem)); continue if not d: skipped.append((who, "no domain")); continue if any(d == s or d.endswith("." + s) for s in SKIP): skipped.append((who, "platform domain " + d)); continue if not all_rows and col(r, PHONE) and col(r, EMAIL): skipped.append((who, "already has phone and email")); continue if d in seen: skipped.append((who, "duplicate " + d)); continue seen.add(d); sites.append(d) with open(out, "w") as f: json.dump({"websites": sites, "maxPagesPerDomain": 6, "defaultCountry": "US", "delaySecs": 1}, f, indent=2) print(f"{len(rows)} companies read, {len(sites)} websites -> {out}") for who, why in skipped: print(f" skipped company {who!r}: {why}") def merge(export, dataset, out="attio_import.csv"): by = {} with open(dataset, encoding="utf-8") as f: for it in json.load(f): if it.get("domain"): by[norm(it["domain"])] = it fields = ["Record ID", PHONE, EMAIL, "Email Deliverability", "Contactability Score", "Contact Source"] n = new_p = new_e = left = 0 with open(out, "w", newline="", encoding="utf-8") as f: w = csv.DictWriter(f, fieldnames=fields); w.writeheader() for r in read_export(export): key, problem = rid(r) it = by.get(domain(r)) if problem or not it: continue phones = [p.get("e164") for p in it.get("phones") or [] if (p.get("e164") or "").startswith("+")] status = (it.get("deliverability") or {}).get("status", "UNKNOWN") emails = [e for e in it.get("emails") or [] if not NOREPLY.match(e)] old_p, old_e = col(r, PHONE), col(r, EMAIL) left += bool(old_p) + bool(old_e) p = "" if old_p else (phones[0] if phones else "") e = "" if old_e else (emails[0] if emails and status != "NO_MX" else "") new_p += bool(p); new_e += bool(e) w.writerow({"Record ID": key, PHONE: p, EMAIL: e, "Email Deliverability": status, "Contactability Score": it.get("contactabilityScore", 0), "Contact Source": "https://" + norm(it["domain"])}) n += 1 print(f"{n} rows -> {out} (match: Record ID; new phones: {new_p}; new emails: {new_e}; " f"existing values left blank so Attio skips them: {left})") if __name__ == "__main__": if len(sys.argv) >= 3 and sys.argv[1] == "prepare": prepare(sys.argv[2], all_rows="--all" in sys.argv) elif len(sys.argv) >= 4 and sys.argv[1] == "merge": merge(sys.argv[2], sys.argv[3]) else: sys.exit("usage: attio_enrich_prep.py prepare export.csv [--all] | " "merge export.csv dataset.json [--phone-col NAME] [--email-col NAME]")