|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Add emails from a CSV file (or the command line) to your Substack as free subscribers, or give them a complimentary paid plan. |
| 3 | +
|
| 4 | + python add_subscribers.py --csv workshop.csv --dry-run |
| 5 | + python add_subscribers.py --csv workshop.csv |
| 6 | + python add_subscribers.py --email maya@example.com --plan comp --comp-until 2026-12-31 |
| 7 | +
|
| 8 | +The CSV needs an "email" column. Substack sends each new subscriber its usual |
| 9 | +welcome email. Add only people who agreed to hear from you. |
| 10 | +""" |
| 11 | +import argparse |
| 12 | +import csv |
| 13 | + |
| 14 | +from cotlet_client import call, paginate, run |
| 15 | + |
| 16 | + |
| 17 | +def main() -> None: |
| 18 | + parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) |
| 19 | + parser.add_argument("--csv", help="CSV file with an email column") |
| 20 | + parser.add_argument("--email", action="append", default=[], help="Email to add (repeatable)") |
| 21 | + parser.add_argument("--plan", choices=["free", "comp"], default="free") |
| 22 | + parser.add_argument("--comp-until", help="End of the complimentary plan (YYYY-MM-DD), with --plan comp") |
| 23 | + parser.add_argument("--dry-run", action="store_true", help="Only show who would be added") |
| 24 | + args = parser.parse_args() |
| 25 | + |
| 26 | + emails = list(args.email) |
| 27 | + if args.csv: |
| 28 | + with open(args.csv, newline="", encoding="utf-8-sig") as fh: |
| 29 | + emails += [row["email"].strip() for row in csv.DictReader(fh) if row.get("email", "").strip()] |
| 30 | + if not emails: |
| 31 | + parser.error("pass --csv FILE or --email ADDRESS") |
| 32 | + |
| 33 | + existing = {s["email"].lower() for s in paginate("list_subscribers", {"limit": 500}, "subscribers")} |
| 34 | + new = sorted({e.lower() for e in emails} - existing) |
| 35 | + print(f"{len(emails)} emails, {len(emails) - len(new)} already subscribed, {len(new)} to add") |
| 36 | + for email in new: |
| 37 | + if args.dry_run: |
| 38 | + print(f" would add {email} ({args.plan})") |
| 39 | + continue |
| 40 | + result = call("add_subscriber", {"email": email, "plan": args.plan, "comp_until": args.comp_until}) |
| 41 | + print(f" {email}: {result.get('status')}") |
| 42 | + |
| 43 | + |
| 44 | +if __name__ == "__main__": |
| 45 | + run(main) |
0 commit comments