Skip to content

Commit d97f2ce

Browse files
author
dalj8690
committed
Updated the script for generating the RSS feeds
1 parent 7fd38c4 commit d97f2ce

File tree

1 file changed

+32
-35
lines changed

1 file changed

+32
-35
lines changed

generate_rss.py

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import os
22
import xml.etree.ElementTree as ET
3-
from datetime import datetime
4-
from email.utils import format_datetime, parsedate_to_datetime
3+
from datetime import datetime, timezone
4+
from email.utils import parsedate_to_datetime
55

66
# Path to your docs folder
77
docs_folder = 'docs'
8-
rss_file = os.path.join(docs_folder, 'rss.xml')
8+
rss_file = 'docs/rss.xml'
99

10-
# Get a list of all PDF files in docs and subdirectories
10+
# Get a list of all PDF files in docs and its subdirectories
1111
pdf_files = []
12-
for root_dir, dirs, files in os.walk(docs_folder):
12+
for root, dirs, files in os.walk(docs_folder):
1313
for file in files:
1414
if file.endswith(".pdf"):
15-
pdf_files.append(os.path.join(root_dir, file))
15+
pdf_files.append(os.path.join(root, file))
1616

1717
print(f"Found {len(pdf_files)} PDFs in the 'docs' folder.")
1818

@@ -28,48 +28,45 @@
2828
ET.SubElement(channel, "link").text = "https://github.com/damirlj/modern_cpp_tutorials"
2929
ET.SubElement(channel, "description").text = "New articles and updates in the docs/ folder"
3030

31-
# Create a map of current items to remove duplicates
32-
existing_items = {item.find("guid").text: item for item in channel.findall("item") if item.find("guid") is not None}
31+
# Create a dictionary of existing items by GUID (commit URL)
32+
existing_items = {item.find("guid").text: item for item in channel.findall("item")}
3333

34-
# Get current date in RFC 2822 format (for RSS)
35-
current_date = format_datetime(datetime.utcnow())
34+
# Get current UTC date with tzinfo
35+
current_date = datetime.now(timezone.utc).strftime("%a, %d %b %Y %H:%M:%S GMT")
3636

37-
# Add or update RSS items
37+
# Add or update items
3838
for pdf in pdf_files:
3939
relative_path = os.path.relpath(pdf, docs_folder)
4040
commit_url = f"https://github.com/damirlj/modern_cpp_tutorials/blob/main/{relative_path}"
4141

42-
# Remove old item if it exists
42+
# If item exists, update pubDate; otherwise, create new
4343
if commit_url in existing_items:
44-
channel.remove(existing_items[commit_url])
44+
existing_items[commit_url].find("pubDate").text = current_date
45+
else:
46+
item = ET.Element("item")
47+
ET.SubElement(item, "title").text = relative_path
48+
ET.SubElement(item, "link").text = commit_url
49+
ET.SubElement(item, "guid").text = commit_url
50+
ET.SubElement(item, "pubDate").text = current_date
51+
channel.append(item)
4552

46-
# Create and add the new item
47-
item = ET.Element("item")
48-
ET.SubElement(item, "title").text = relative_path
49-
ET.SubElement(item, "link").text = commit_url
50-
ET.SubElement(item, "guid").text = commit_url
51-
ET.SubElement(item, "pubDate").text = current_date
52-
channel.append(item)
53-
54-
# Sort items by pubDate descending
55-
items = channel.findall("item")
56-
57-
# Parse pubDate strings to datetime objects for sorting
53+
# Sort all items by pubDate descending
5854
def get_pub_date(item):
59-
pub_date = item.find("pubDate").text
60-
return parsedate_to_datetime(pub_date)
55+
pub_date_text = item.find("pubDate").text
56+
dt = parsedate_to_datetime(pub_date_text)
57+
return dt if dt.tzinfo else dt.replace(tzinfo=timezone.utc)
6158

59+
items = channel.findall("item")
6260
items.sort(key=get_pub_date, reverse=True)
6361

64-
# Optional: Keep only the latest N entries (e.g., 20)
65-
#MAX_ENTRIES = 20
66-
#for item in channel.findall("item"):
67-
# channel.remove(item)
68-
#for item in items[:MAX_ENTRIES]:
69-
# channel.append(item)
62+
# Clear old items and re-append in sorted order
63+
for item in channel.findall("item"):
64+
channel.remove(item)
65+
for item in items:
66+
channel.append(item)
7067

71-
# Save updated RSS feed
68+
# Save the updated RSS feed
7269
tree = ET.ElementTree(root)
7370
tree.write(rss_file, encoding="UTF-8", xml_declaration=True)
7471

75-
print(f"Generated RSS feed with {min(len(items), MAX_ENTRIES)} articles.")
72+
print(f"Generated RSS feed with {len(items)} article(s).")

0 commit comments

Comments
 (0)