Repository navigation
Expand file tree
/
Copy pathlistmonk_rss.py
More file actions
266 lines (212 loc) · 8.7 KB
/
Copy pathlistmonk_rss.py
File metadata and controls
266 lines (212 loc) · 8.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import os
import json
from datetime import datetime, timedelta, timezone
from pathlib import Path
import httpx
import feedparser
from jinja2 import Template
from dotenv import load_dotenv
from bs4 import BeautifulSoup
import click
import logging
logging.basicConfig(level=logging.INFO) # Set to DEBUG, INFO, WARNING, ERROR, or CRITICAL
# Load environment variables
load_dotenv()
# Constants
TEMPLATE_FILE = Path("template.md.j2")
def env(name: str, default=None):
"""Read an environment variable, ignoring surrounding whitespace.
Empty values count as unset, since GitHub Actions passes unset
variables as empty strings.
"""
value = os.getenv(name, "").strip()
return value or default
def get_opengraph_data(url):
response = httpx.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
og_data = {}
for meta in soup.find_all('meta'):
prop = meta.get('property', '')
if prop.startswith('og:'):
key = prop[3:]
og_data[key] = meta.get('content', '')
return og_data
def get_last_update() -> datetime:
"""Get the last update timestamp from GitHub repo variable."""
github_token = env("GH_TOKEN")
repo = env("GH_REPOSITORY")
url = f"https://api.github.com/repos/{repo}/actions/variables/LAST_UPDATE"
headers = {
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {github_token}",
"X-GitHub-Api-Version": "2022-11-28"
}
try:
response = httpx.get(url, headers=headers)
response.raise_for_status()
data = response.json()
return datetime.fromisoformat(data["value"])
except httpx.HTTPStatusError as e:
if e.response.status_code == 404:
return datetime.min
raise
def save_last_update(timestamp: datetime):
"""Save the last update timestamp to GitHub repo variable."""
github_token = env("GH_TOKEN")
repo = env("GH_REPOSITORY")
url = f"https://api.github.com/repos/{repo}/actions/variables/LAST_UPDATE"
headers = {
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {github_token}",
"X-GitHub-Api-Version": "2022-11-28"
}
data = {
"name": "LAST_UPDATE",
"value": timestamp.isoformat()
}
response = httpx.patch(url, headers=headers, json=data)
response.raise_for_status()
logging.info(f"Saved last update timestamp to GitHub repo variable")
def fetch_rss_feed(feed_url: str, last_update: datetime) -> list:
"""Fetch and parse RSS feed, returning new items since last update."""
feed = feedparser.parse(feed_url)
new_items = []
logging.info(f"There are in total {len(feed.entries)} entries for {feed_url}")
for entry in feed.entries:
# Prefer the publication date; Atom feeds (e.g. Publii) may only have `updated`
date_parsed = entry.get("published_parsed") or entry.get("updated_parsed")
if not date_parsed:
logging.warning(f"Skipping entry without a date: {entry.get('link', entry.get('title'))}")
continue
if datetime(*date_parsed[:6]) > last_update:
og = get_opengraph_data(entry.link)
if og.get("image"):
entry.media_content=og.get("image")
new_items.append(entry)
return new_items
def get_list_id(host: str, api_user: str, api_token: str, list_name: str) -> int:
"""Get list ID from list name using Listmonk API."""
url = f"{host}/api/lists"
auth=(api_user, api_token)
headers = {
"Content-Type": "application/json"
}
with httpx.Client() as client:
response = client.get(url, headers=headers, auth=auth )
response.raise_for_status()
# Find the list with matching name
lists = response.json()["data"]["results"]
for lst in lists:
if lst["name"] == list_name:
return lst["id"]
raise ValueError(f"List '{list_name}' not found")
def create_campaign_content(items: list, template: Template) -> str:
"""Generate campaign content using Jinja2 template."""
return template.render(items=items)
def schedule_campaign(host: str, api_user: str, api_token: str, list_id: int, content: str, subject: str, dry_run: bool = False):
"""Send campaign draft using Listmonk API."""
url = f"{host}/api/campaigns"
auth=(api_user, api_token)
headers = {
"Content-Type": "application/json"
}
current_datetime = datetime.now().strftime("%Y-%m-%d %H:%M")
# for the send time, we assume that the linkmonk server runs in UTC (this
# is what the default in PikaPods are)
if dry_run:
# Set delay to 10 years for dry run
print("*** This is a dry run, the campaign is scheduled 10 years in the future")
delay_mins = 365 * 24 * 60 * 10
else:
delay_mins = int(env("DELAY_SEND_MINS", 30))
send_datetime = datetime.now(timezone.utc) + timedelta(minutes=delay_mins)
send_datetime = send_datetime.strftime("%Y-%m-%dT%H:%M:%SZ")
data = {
"name" : f"RSS Update Newsletter, {current_datetime}",
"subject": subject,
"lists": [list_id],
"body": content,
"content_type": "markdown",
"type": "regular",
"send_at" : send_datetime
}
# Use a specific Listmonk campaign template, otherwise Listmonk uses its default
template_id = env("LISTMONK_TEMPLATE_ID")
if template_id:
data["template_id"] = int(template_id)
with httpx.Client() as client:
response = client.post(url, json=data, headers=headers,auth=auth)
response.raise_for_status()
parsed = response.json()
assert parsed.get("data",{}).get("id",None), "Cannot get the id of the created campaign"
campaign_id = parsed.get("data",{}).get("id")
print(f"Campaign draft {campaign_id} successfully created!")
url = f"{url}/{campaign_id}/status"
data = {"status": "scheduled"}
headers = {
"Content-Type": "application/json"
}
with httpx.Client() as client:
response = client.put(url, json=data, auth=auth)
response.raise_for_status()
assert parsed.get("data",{}).get("id",None) == campaign_id, f"Cannot schedule campaign {campaign_id}"
print(f"Campaign {campaign_id} successfully scheduled with {delay_mins} mins delay!")
# Send Pushover notification
pushover_user_key = env("PUSHOVER_USER_KEY")
pushover_api_token = env("PUSHOVER_API_TOKEN")
if pushover_user_key and pushover_api_token:
response = httpx.post(
"https://api.pushover.net/1/messages.json",
data={
"token": pushover_api_token,
"user": pushover_user_key,
"message": f"A new campaign has been successfully scheduled with {delay_mins} mins delay! Check if you want to review this before sending.",
"title": "Newsletter for your blog"
},
headers={"Content-type": "application/x-www-form-urlencoded"}
)
response.raise_for_status()
return True
@click.command()
@click.option("--dry-run", is_flag=True, help="Create draft campaign with a 10-year delay and don't update last update time.")
def main(dry_run: bool):
if dry_run:
print("*** This is a dry run")
assert env("RSS_FEED"), "No RSS feed given"
# Load template
template = Template(TEMPLATE_FILE.read_text())
# Get last update time
last_update = get_last_update()
# Fetch new RSS items
items = list(reversed(fetch_rss_feed(env("RSS_FEED"), last_update)))
if not items:
print(f"No new items found, I keep the update as of my last state '{last_update}' (UTC) in GitHub.")
return
# Create campaign content
content = create_campaign_content(items, template)
# Get list ID
list_id = get_list_id(
host=env("LISTMONK_HOST"),
api_user=env("LISTMONK_API_USER"),
api_token=env("LISTMONK_API_TOKEN"),
list_name=env("LIST_NAME")
)
# Schedule campaign
success = schedule_campaign(
host=env("LISTMONK_HOST"),
api_user=env("LISTMONK_API_USER"),
api_token=env("LISTMONK_API_TOKEN"),
list_id=list_id,
content=content,
subject=env("LIST_NAME"),
dry_run=dry_run
)
# Update last update time only if not dry run and successful
if success and not dry_run:
save_last_update(datetime.now())
elif dry_run:
print("*** This is a dry run, I don't update the last_save state")
elif not success:
print("*** Something went wrong with scheduling the campaign")
if __name__ == "__main__":
main()