#!/usr/bin/env python3 """ Script to generate an RSS feed for the comic """ import sys import os from datetime import datetime from xml.etree.ElementTree import Element, SubElement, tostring from xml.dom import minidom # Add parent directory to path so we can import comics_data sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from comics_data import COMICS, COMIC_NAME # Configuration - update these for your site SITE_URL = "http://localhost:3000" # Change to your actual domain SITE_TITLE = COMIC_NAME SITE_DESCRIPTION = "A webcomic about life, the universe, and everything" SITE_LANGUAGE = "en-us" def generate_rss(): """Generate RSS feed from COMICS data""" # Create RSS root rss = Element('rss', version='2.0') channel = SubElement(rss, 'channel') # Channel metadata SubElement(channel, 'title').text = SITE_TITLE SubElement(channel, 'link').text = SITE_URL SubElement(channel, 'description').text = SITE_DESCRIPTION SubElement(channel, 'language').text = SITE_LANGUAGE SubElement(channel, 'lastBuildDate').text = datetime.now().strftime('%a, %d %b %Y %H:%M:%S +0000') # Add items for each comic (reversed to show newest first) for comic in reversed(COMICS): item = SubElement(channel, 'item') # Title title = comic.get('title', f"#{comic['number']}") SubElement(item, 'title').text = title # Link to comic page link = f"{SITE_URL}/comic/{comic['number']}" SubElement(item, 'link').text = link SubElement(item, 'guid', isPermaLink='true').text = link # Description (include author note if present) description = f"Comic #{comic['number']}" if comic.get('author_note'): description += f"\n\n{comic['author_note']}" SubElement(item, 'description').text = description # Publication date try: pub_date = datetime.strptime(comic['date'], '%Y-%m-%d') SubElement(item, 'pubDate').text = pub_date.strftime('%a, %d %b %Y 00:00:00 +0000') except: pass # Image enclosure image_url = f"{SITE_URL}/static/images/comics/{comic['filename']}" SubElement(item, 'enclosure', url=image_url, type='image/png', length='0') # Convert to pretty XML xml_str = minidom.parseString(tostring(rss)).toprettyxml(indent=' ') # Remove extra blank lines xml_str = '\n'.join([line for line in xml_str.split('\n') if line.strip()]) return xml_str def main(): """Generate and save RSS feed""" # Get path to static folder script_dir = os.path.dirname(os.path.abspath(__file__)) parent_dir = os.path.dirname(script_dir) static_dir = os.path.join(parent_dir, 'static') # Create static directory if it doesn't exist os.makedirs(static_dir, exist_ok=True) # Generate RSS rss_content = generate_rss() # Save to file rss_file = os.path.join(static_dir, 'feed.rss') with open(rss_file, 'w', encoding='utf-8') as f: f.write(rss_content) print(f"RSS feed generated: {rss_file}") print(f"Total comics: {len(COMICS)}") if __name__ == '__main__': main()