-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrss_feed.py
44 lines (37 loc) · 1.44 KB
/
rss_feed.py
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
"""generate RSS feed for all blog posts"""
from post_parser import get_all_posts, Post
SUMMARY_TITLE = "alifeee's blog"
SUMMARY_LINK = "https://blog.alifeee.co.uk"
SUMMARY_AUTHOR = "alifeee"
SUMMARY_ID = "https://blog.alifeee.co.uk/"
SUMMARY_ICON = "https://blog.alifeee.co.uk/og-image.png"
posts = get_all_posts()
# sort posts by date
posts.sort(key=lambda post: post.date, reverse=True)
# generate feed
feed = ""
feed += "<?xml version='1.0' encoding='UTF-8'?>\n"
feed += '<?xml-stylesheet href="/feed.xsl" type="text/xsl"?>\n'
feed += '<feed xmlns="http://www.w3.org/2005/Atom">\n\n'
feed += f"<title>{SUMMARY_TITLE}</title>\n"
feed += f"<link href='{SUMMARY_LINK}' rel='self' />\n"
feed += f"<updated>{posts[0].date.isoformat()}</updated>\n"
feed += "<author>\n"
feed += f" <name>{SUMMARY_AUTHOR}</name>\n"
feed += "</author>\n"
feed += f"<id>{SUMMARY_ID}</id>\n"
feed += f"<icon>{SUMMARY_ICON}</icon>\n\n"
for post in posts:
link = SUMMARY_LINK + "/" + post.relative_url.lstrip("./")
feed += "<entry>\n"
feed += f" <title>{post.title}</title>\n"
feed += f" <link href='{link}' />\n"
feed += f" <id>{SUMMARY_ID}/{post.relative_url.lstrip('./')}</id>\n"
feed += f" <updated>{post.date.isoformat()}</updated>\n"
feed += f" <summary>{post.description}</summary>\n"
feed += "</entry>\n\n"
feed += "</feed>\n"
# write feed to file
with open("feed.xml", "w", encoding="utf-8") as f:
f.write(feed)
print("feed written to feed.xml")