|
| 1 | +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) |
| 45 | +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) |
| 49 | + |
| 50 | +print("✅ RSS feed updated for PDFs!") |
0 commit comments