Skip to content

Commit 165c925

Browse files
author
dalj8690
committed
Updated the script, for generating the RSS feeds
1 parent 3f8d507 commit 165c925

File tree

1 file changed

+50
-47
lines changed

1 file changed

+50
-47
lines changed

generate_rss.py

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,53 @@
11
import os
2-
import datetime
3-
4-
DOCS_DIR = "docs"
5-
RSS_FILE = os.path.join(DOCS_DIR, "rss.xml")
6-
BASE_URL = "https://damirlj.github.io/modern_cpp_tutorials/docs"
7-
8-
header = f"""<?xml version="1.0" encoding="UTF-8" ?>
9-
<rss version="2.0">
10-
<channel>
11-
<title>Modern C++ Tutorials</title>
12-
<link>{BASE_URL}/</link>
13-
<description>Latest articles in Modern C++ Tutorials</description>
14-
<lastBuildDate>{datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S +0000')}</lastBuildDate>
15-
"""
16-
17-
# Collecting all PDF files in the docs folder, ordered by date
18-
items = []
19-
for file in sorted(os.listdir(DOCS_DIR), reverse=True):
20-
if file.endswith(".pdf"):
21-
title = file.replace("_", " ").replace(".pdf", "").title()
22-
link = f"{BASE_URL}/{file}"
23-
pub_date = datetime.datetime.utcfromtimestamp(os.path.getmtime(os.path.join(DOCS_DIR, file))).strftime('%a, %d %b %Y %H:%M:%S +0000')
24-
items.append(f"""
25-
<item>
26-
<title>{title}</title>
27-
<link>{link}</link>
28-
<guid>{link}</guid>
29-
<pubDate>{pub_date}</pubDate>
30-
</item>""")
31-
32-
footer = """
33-
</channel>
34-
</rss>
35-
"""
36-
37-
# If rss.xml exists, update it; else create a new one
38-
if os.path.exists(RSS_FILE):
39-
with open(RSS_FILE, "r", encoding="utf-8") as f:
40-
content = f.read()
41-
# Remove the old <channel> content before adding new items
42-
new_content = content.split("</channel>")[0] + "</channel>" + footer
43-
with open(RSS_FILE, "w", encoding="utf-8") as f:
44-
f.write(new_content)
2+
import xml.etree.ElementTree as ET
3+
from datetime import datetime
4+
5+
# Path to your docs folder
6+
docs_folder = "docs"
7+
8+
# Path to your RSS file
9+
rss_file = os.path.join(docs_folder, "rss.xml")
10+
11+
# Get a list of all PDF files in docs and its subdirectories
12+
pdf_files = []
13+
for root, dirs, files in os.walk(docs_folder):
14+
for file in files:
15+
if file.endswith(".pdf"):
16+
pdf_files.append(os.path.join(root, file))
17+
18+
# Create or load RSS XML
19+
if os.path.exists(rss_file):
20+
tree = ET.parse(rss_file)
21+
root = tree.getroot()
4522
else:
46-
# If RSS file doesn't exist, create it from scratch
47-
with open(RSS_FILE, "w", encoding="utf-8") as f:
48-
f.write(header + "\n".join(items) + footer)
23+
root = ET.Element("rss", version="2.0")
24+
channel = ET.SubElement(root, "channel")
25+
ET.SubElement(channel, "title").text = "Modern C++ Tutorials - Docs Updates"
26+
ET.SubElement(channel, "link").text = "https://github.com/damirlj/modern_cpp_tutorials"
27+
ET.SubElement(channel, "description").text = "New articles and updates in the docs/ folder"
28+
29+
# Get current date for publishing
30+
current_date = datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S GMT")
31+
32+
# Iterate through each PDF file found
33+
for pdf in pdf_files:
34+
# Convert the full file path to a relative URL for GitHub
35+
relative_path = os.path.relpath(pdf, docs_folder)
36+
commit_url = f"https://github.com/damirlj/modern_cpp_tutorials/blob/main/{relative_path}"
37+
38+
# Create a new RSS item for each PDF
39+
item = ET.Element("item")
40+
ET.SubElement(item, "title").text = relative_path # Use relative path as title
41+
ET.SubElement(item, "link").text = commit_url
42+
ET.SubElement(item, "guid").text = commit_url
43+
ET.SubElement(item, "pubDate").text = current_date
44+
45+
# Append the item to the channel
46+
channel = root.find("channel")
47+
channel.append(item)
48+
49+
# Save the updated RSS feed
50+
tree = ET.ElementTree(root)
51+
tree.write(rss_file, encoding="UTF-8", xml_declaration=True)
4952

50-
print("✅ RSS feed updated for PDFs!")
53+
print(f"Generated RSS feed with {len(pdf_files)} articles.")

0 commit comments

Comments
 (0)