Skip to content

Commit 7f82899

Browse files
committed
Update script to pull URLs straight from Mailchimp
1 parent 83cbfab commit 7f82899

File tree

2 files changed

+72
-22
lines changed

2 files changed

+72
-22
lines changed

docker-compose.yml

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ services:
1818
PGUSER: wagtailorg
1919
PGPASSWORD: wagtailorg
2020
PGDATABASE: wagtailorg
21+
MAILCHIMP_API_KEY: ${MAILCHIMP_API_KEY}
2122
ports:
2223
- 8000:8000
2324
volumes:

wagtailio/newsletter/management/commands/fetch_newsletters.py

+71-22
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,97 @@
11
from datetime import datetime
2-
import json
2+
import os
33
from pathlib import Path
44

55
from django.core.management.base import BaseCommand
66

7+
from mailchimp_marketing import Client
78
import requests
89

910

10-
class Command(BaseCommand):
11-
help = "Fetches newsletter URLs from the provided JSON file and saves them to the archive directory"
11+
def get_mailchimp_client():
12+
"""Initialize and return a Mailchimp client."""
13+
api_key = os.getenv("MAILCHIMP_API_KEY")
14+
if not api_key:
15+
raise ValueError("MAILCHIMP_API_KEY environment variable is not set")
16+
17+
# Extract datacenter from API key (it's the part after the '-')
18+
datacenter = api_key.split("-")[-1]
19+
20+
client = Client()
21+
client.set_config({"api_key": api_key, "server": datacenter})
22+
client.ping.get()
23+
return client
24+
25+
26+
def fetch_campaign_urls():
27+
"""Fetch all campaign URLs from Mailchimp."""
28+
client = get_mailchimp_client()
1229

13-
def add_arguments(self, parser):
14-
parser.add_argument(
15-
"input_file",
16-
type=str,
17-
help="Path to the JSON file containing newsletter data",
30+
campaigns = []
31+
offset = 0
32+
count = 100 # Number of campaigns to fetch per request
33+
34+
while True:
35+
# Get a batch of campaigns
36+
response = client.campaigns.list(
37+
count=count,
38+
offset=offset,
39+
fields=[
40+
"campaigns.archive_url",
41+
"campaigns.settings.title",
42+
"campaigns.send_time",
43+
"campaigns.id",
44+
"total_items",
45+
],
1846
)
1947

20-
def handle(self, *args, **options):
21-
input_file = options["input_file"]
22-
23-
# Read the JSON file
24-
try:
25-
with open(input_file) as f:
26-
newsletters = json.load(f)
27-
except FileNotFoundError:
28-
self.stderr.write(self.style.ERROR(f"Input file not found: {input_file}"))
29-
return
30-
except json.JSONDecodeError:
31-
self.stderr.write(self.style.ERROR(f"Invalid JSON in file: {input_file}"))
32-
return
48+
# Extract campaign data
49+
for campaign in response["campaigns"]:
50+
# Only include campaigns that have been sent
51+
if campaign.get("send_time"):
52+
campaigns.append(
53+
{
54+
"id": campaign["id"],
55+
"send_date": campaign["send_time"],
56+
"title": campaign["settings"]["title"],
57+
"url": campaign["archive_url"],
58+
}
59+
)
60+
61+
# Check if we've fetched all campaigns
62+
total_items = response["total_items"]
63+
offset += count
64+
if offset >= total_items:
65+
break
66+
67+
# Sort campaigns by send date, newest first
68+
campaigns.sort(key=lambda x: x["send_date"], reverse=True)
69+
return campaigns
3370

71+
72+
class Command(BaseCommand):
73+
help = "Fetches newsletters from Mailchimp and saves them to the archive directory"
74+
75+
def handle(self, *args, **options):
3476
# Create output directory if it doesn't exist
3577
output_path = Path(__file__).resolve().parent.parent.parent / "archive"
3678
output_path.mkdir(parents=True, exist_ok=True)
3779

80+
cutoff_date = datetime.fromisoformat("2023-07-20")
81+
3882
# Process each newsletter
39-
for newsletter in newsletters:
83+
for newsletter in fetch_campaign_urls():
4084
url = newsletter.get("url")
4185
send_date = newsletter.get("send_date")
4286
title = newsletter.get("title")
4387

4488
# Convert send_date to a datetime object
4589
date = datetime.fromisoformat(send_date.replace("Z", "+00:00"))
90+
91+
# Skip if newsletter is older than cutoff date
92+
if date.date() < cutoff_date.date():
93+
continue
94+
4695
# Create filename using date and title
4796
filename = f"{date.strftime('%Y-%m-%d')}_{title.replace(' ', '_')}.html"
4897
filepath = output_path / filename

0 commit comments

Comments
 (0)