|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Weekly Substack report: new, paid and churned subscribers, revenue, and how each post of the week did. |
| 3 | +
|
| 4 | + python weekly_report.py |
| 5 | + python weekly_report.py --save last-week.json |
| 6 | +
|
| 7 | +With --save the totals are stored, and the next run with the same file adds a |
| 8 | +week-on-week line. Revenue is Substack's gross annualized figure, not cash received. |
| 9 | +""" |
| 10 | +import argparse |
| 11 | +import json |
| 12 | +from datetime import datetime, timedelta, timezone |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | +from cotlet_client import call, paginate, run |
| 16 | + |
| 17 | + |
| 18 | +def main() -> None: |
| 19 | + parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) |
| 20 | + parser.add_argument("--save", help="JSON file to compare with and update, e.g. last-week.json") |
| 21 | + args = parser.parse_args() |
| 22 | + |
| 23 | + week = call("get_publication_stats", {"period": "7d"}) |
| 24 | + month = call("get_publication_stats", {"period": "30d"}) |
| 25 | + weekly_churn = month["subscribers_churned"] / 30 * 7 |
| 26 | + |
| 27 | + print(f"Subscribers: {week['subscribers_total']:,} total (+{week['subscribers_new']} new)") |
| 28 | + flag = " (flagged: above the 30-day weekly average)" if week["subscribers_churned"] > weekly_churn else "" |
| 29 | + print(f"Paid: {week['subscribers_paid']:,}, churned {week['subscribers_churned']} " |
| 30 | + f"(30-day weekly average: {weekly_churn:.0f}){flag}") |
| 31 | + print(f"Gross annualized revenue: ${week.get('revenue_usd', 0):,}") |
| 32 | + |
| 33 | + now = datetime.now(timezone.utc) |
| 34 | + print("Posts this week:") |
| 35 | + for post in paginate("list_posts", {"status": "published", "limit": 50}, "posts"): |
| 36 | + published = datetime.fromisoformat(post["published_at"].replace("Z", "+00:00")) |
| 37 | + if published < now - timedelta(days=7): |
| 38 | + break |
| 39 | + s = call("get_post_stats", {"post_id": post["post_id"]}) |
| 40 | + early = " (early, under 2 days of data)" if published > now - timedelta(days=2) else "" |
| 41 | + print(f' "{post["title"]}": open rate {s["open_rate"]:.1%}, {s["subscribers_gained"]} new subscribers{early}') |
| 42 | + |
| 43 | + if args.save: |
| 44 | + path = Path(args.save) |
| 45 | + if path.exists(): |
| 46 | + prev = json.loads(path.read_text()) |
| 47 | + print(f"Week on week: total {week['subscribers_total'] - prev['subscribers_total']:+,}, " |
| 48 | + f"paid {week['subscribers_paid'] - prev['subscribers_paid']:+,}") |
| 49 | + else: |
| 50 | + print("Week on week: no stored copy yet, saved this week for next time") |
| 51 | + path.write_text(json.dumps(week, indent=2)) |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + run(main) |
0 commit comments