| |
| """Select a deterministic sample of email addresses for publishing. |
| |
| Process: |
| 1. Require temporary/all_email_blasts_consolidated.csv to exist. |
| 2. Read SALT from .env. |
| 3. Derive ordering salt from SALT and magic words "email ordering". |
| 4. Deduplicate email addresses from the consolidated file. |
| 5. Hash each email with the ordering salt and sort by hash. |
| 6. Select the first 100,000 emails and write them in hash order. |
| |
| Output: |
| temporary/sampled_email_addresses.csv |
| """ |
|
|
| from __future__ import annotations |
|
|
| import argparse |
| import csv |
| import hashlib |
| from pathlib import Path |
|
|
|
|
| def parse_args() -> argparse.Namespace: |
| project_root = Path(__file__).resolve().parent.parent |
| parser = argparse.ArgumentParser( |
| description="Deterministically sample email addresses for publishable output." |
| ) |
| parser.add_argument( |
| "--input", |
| default=str(project_root / "temporary" / "all_email_blasts_consolidated.csv"), |
| help="Path to consolidated email blasts file.", |
| ) |
| parser.add_argument( |
| "--env-file", |
| default=str(project_root / ".env"), |
| help="Path to .env file containing SALT.", |
| ) |
| parser.add_argument( |
| "--output", |
| default=str(project_root / "temporary" / "sampled_email_addresses.csv"), |
| help="Path for sampled output CSV.", |
| ) |
| parser.add_argument( |
| "--sample-size", |
| type=int, |
| default=100000, |
| help="Maximum number of emails to sample.", |
| ) |
| return parser.parse_args() |
|
|
|
|
| def read_salt(env_path: Path) -> str: |
| if not env_path.exists(): |
| raise FileNotFoundError(f"Required env file not found: {env_path}") |
|
|
| salt = "" |
| with env_path.open("r", encoding="utf-8") as f: |
| for line in f: |
| line = line.strip() |
| if not line or line.startswith("#"): |
| continue |
| if line.startswith("SALT="): |
| salt = line.split("=", 1)[1].strip().strip('"').strip("'") |
| break |
|
|
| if not salt: |
| raise RuntimeError(f"SALT was not found or empty in: {env_path}") |
|
|
| return salt |
|
|
|
|
| def derive_email_ordering_hash(salt: str) -> str: |
| return hashlib.sha256(f"{salt}email ordering".encode("utf-8")).hexdigest() |
|
|
|
|
| def read_deduplicated_emails(input_path: Path) -> list[str]: |
| if not input_path.exists(): |
| raise FileNotFoundError( |
| "Required input not found: " |
| f"{input_path}. Run consolidate_email_blasts.py first." |
| ) |
|
|
| emails = set() |
| with input_path.open("r", encoding="utf-8", newline="") as f: |
| reader = csv.DictReader(f) |
| if not reader.fieldnames: |
| raise RuntimeError(f"Input file has no header: {input_path}") |
|
|
| if "email" not in reader.fieldnames: |
| raise RuntimeError( |
| f"Input file missing required 'email' column: {input_path}" |
| ) |
|
|
| for row in reader: |
| email = (row.get("email") or "").strip().lower() |
| if email: |
| emails.add(email) |
|
|
| if not emails: |
| raise RuntimeError(f"No email values were found in: {input_path}") |
|
|
| return list(emails) |
|
|
|
|
| def email_order_hash(email_ordering_hash: str, email: str) -> str: |
| return hashlib.sha256(f"{email_ordering_hash}{email}".encode("utf-8")).hexdigest() |
|
|
|
|
| def write_sampled_output(output_path: Path, sampled_emails: list[str]) -> None: |
| output_path.parent.mkdir(parents=True, exist_ok=True) |
| with output_path.open("w", encoding="utf-8", newline="") as f: |
| writer = csv.writer(f) |
| writer.writerow(["email"]) |
| for email in sampled_emails: |
| writer.writerow([email]) |
|
|
|
|
| def main() -> None: |
| args = parse_args() |
| input_path = Path(args.input) |
| env_path = Path(args.env_file) |
| output_path = Path(args.output) |
|
|
| salt = read_salt(env_path) |
| email_ordering_hash = derive_email_ordering_hash(salt) |
| emails = read_deduplicated_emails(input_path) |
|
|
| if len(emails) < args.sample_size: |
| raise RuntimeError( |
| "Not enough unique emails to satisfy requested sample size: " |
| f"{len(emails):,} < {args.sample_size:,}" |
| ) |
|
|
| sorted_pairs = sorted( |
| ((email_order_hash(email_ordering_hash, email), email) for email in emails), |
| key=lambda pair: (pair[0], pair[1]), |
| ) |
| sampled = [email for _, email in sorted_pairs[: args.sample_size]] |
|
|
| write_sampled_output(output_path, sampled) |
|
|
| print(f"Unique emails found: {len(emails):,}") |
| print(f"Selected emails: {len(sampled):,}") |
| print(f"Output: {output_path}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |