|
1 | 1 | from datetime import datetime
|
2 |
| -import json |
| 2 | +import os |
3 | 3 | from pathlib import Path
|
4 | 4 |
|
5 | 5 | from django.core.management.base import BaseCommand
|
6 | 6 |
|
| 7 | +from mailchimp_marketing import Client |
7 | 8 | import requests
|
8 | 9 |
|
9 | 10 |
|
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() |
12 | 29 |
|
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 | + ], |
18 | 46 | )
|
19 | 47 |
|
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 |
33 | 70 |
|
| 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): |
34 | 76 | # Create output directory if it doesn't exist
|
35 | 77 | output_path = Path(__file__).resolve().parent.parent.parent / "archive"
|
36 | 78 | output_path.mkdir(parents=True, exist_ok=True)
|
37 | 79 |
|
| 80 | + cutoff_date = datetime.fromisoformat("2023-07-20") |
| 81 | + |
38 | 82 | # Process each newsletter
|
39 |
| - for newsletter in newsletters: |
| 83 | + for newsletter in fetch_campaign_urls(): |
40 | 84 | url = newsletter.get("url")
|
41 | 85 | send_date = newsletter.get("send_date")
|
42 | 86 | title = newsletter.get("title")
|
43 | 87 |
|
44 | 88 | # Convert send_date to a datetime object
|
45 | 89 | 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 | + |
46 | 95 | # Create filename using date and title
|
47 | 96 | filename = f"{date.strftime('%Y-%m-%d')}_{title.replace(' ', '_')}.html"
|
48 | 97 | filepath = output_path / filename
|
|
0 commit comments