askrobots / dbbasic-rss
Public
RSS feeds from TSV, CSV, JSON or Python lists, for any Python web framework or a static site.
templates.py · 3.9 KB · 114 lines
| 1 | """RSS XML template rendering (pure Python, no external dependencies)""" |
| 2 | |
| 3 | from typing import TYPE_CHECKING |
| 4 | from html import escape |
| 5 | |
| 6 | if TYPE_CHECKING: |
| 7 | from .feed import Feed, FeedItem |
| 8 | |
| 9 | |
| 10 | def escape_xml(text: str) -> str: |
| 11 | """Escape XML special characters""" |
| 12 | if text is None: |
| 13 | return "" |
| 14 | return escape(str(text), quote=True) |
| 15 | |
| 16 | |
| 17 | def render_item(item: 'FeedItem') -> str: |
| 18 | """Render a single feed item to XML""" |
| 19 | xml_parts = [' <item>'] |
| 20 | |
| 21 | # Required fields |
| 22 | xml_parts.append(f' <title>{escape_xml(item.title)}</title>') |
| 23 | xml_parts.append(f' <link>{escape_xml(item.link)}</link>') |
| 24 | xml_parts.append(f' <guid>{escape_xml(item.guid)}</guid>') |
| 25 | |
| 26 | # Description |
| 27 | if item.description: |
| 28 | xml_parts.append(f' <description>{escape_xml(item.description)}</description>') |
| 29 | |
| 30 | # Content (with CDATA for HTML) |
| 31 | if item.content: |
| 32 | xml_parts.append(f' <content:encoded><![CDATA[{item.content}]]></content:encoded>') |
| 33 | |
| 34 | # Publication date |
| 35 | if item.pub_date: |
| 36 | xml_parts.append(f' <pubDate>{escape_xml(item.pub_date)}</pubDate>') |
| 37 | |
| 38 | # Author |
| 39 | if item.author: |
| 40 | xml_parts.append(f' <dc:creator>{escape_xml(item.author)}</dc:creator>') |
| 41 | |
| 42 | # Categories |
| 43 | for category in item.categories: |
| 44 | xml_parts.append(f' <category>{escape_xml(category)}</category>') |
| 45 | |
| 46 | # Enclosure (for podcasts/media) |
| 47 | if item.enclosure: |
| 48 | enc_url = escape_xml(item.enclosure.get('url', '')) |
| 49 | enc_type = escape_xml(item.enclosure.get('type', 'audio/mpeg')) |
| 50 | enc_length = escape_xml(item.enclosure.get('length', '0')) |
| 51 | xml_parts.append(f' <enclosure url="{enc_url}" type="{enc_type}" length="{enc_length}" />') |
| 52 | |
| 53 | xml_parts.append(' </item>') |
| 54 | return '\n'.join(xml_parts) |
| 55 | |
| 56 | |
| 57 | def render_rss(feed: 'Feed') -> str: |
| 58 | """Render complete RSS 2.0 feed""" |
| 59 | xml_parts = ['<?xml version="1.0" encoding="UTF-8"?>'] |
| 60 | |
| 61 | # RSS opening tag with namespaces |
| 62 | xml_parts.append( |
| 63 | '<rss version="2.0" ' |
| 64 | 'xmlns:content="http://purl.org/rss/1.0/modules/content/" ' |
| 65 | 'xmlns:dc="http://purl.org/dc/elements/1.1/" ' |
| 66 | 'xmlns:atom="http://www.w3.org/2005/Atom">' |
| 67 | ) |
| 68 | |
| 69 | xml_parts.append(' <channel>') |
| 70 | |
| 71 | # Required channel elements |
| 72 | xml_parts.append(f' <title>{escape_xml(feed.title)}</title>') |
| 73 | xml_parts.append(f' <link>{escape_xml(feed.link)}</link>') |
| 74 | xml_parts.append(f' <description>{escape_xml(feed.description)}</description>') |
| 75 | |
| 76 | # Optional channel elements |
| 77 | if feed.language: |
| 78 | xml_parts.append(f' <language>{escape_xml(feed.language)}</language>') |
| 79 | |
| 80 | if feed.build_date: |
| 81 | xml_parts.append(f' <lastBuildDate>{escape_xml(feed.build_date)}</lastBuildDate>') |
| 82 | |
| 83 | if feed.ttl: |
| 84 | xml_parts.append(f' <ttl>{feed.ttl}</ttl>') |
| 85 | |
| 86 | if feed.category: |
| 87 | xml_parts.append(f' <category>{escape_xml(feed.category)}</category>') |
| 88 | |
| 89 | # Managing editor / webmaster |
| 90 | if feed.author_email and feed.author: |
| 91 | xml_parts.append(f' <managingEditor>{escape_xml(feed.author_email)} ({escape_xml(feed.author)})</managingEditor>') |
| 92 | elif feed.author_email: |
| 93 | xml_parts.append(f' <managingEditor>{escape_xml(feed.author_email)}</managingEditor>') |
| 94 | |
| 95 | # Atom self-reference (best practice) |
| 96 | if feed.link: |
| 97 | xml_parts.append(f' <atom:link href="{escape_xml(feed.link)}" rel="self" type="application/rss+xml" />') |
| 98 | |
| 99 | # Image (logo/icon) |
| 100 | if feed.image_url: |
| 101 | xml_parts.append(' <image>') |
| 102 | xml_parts.append(f' <url>{escape_xml(feed.image_url)}</url>') |
| 103 | xml_parts.append(f' <title>{escape_xml(feed.image_title)}</title>') |
| 104 | xml_parts.append(f' <link>{escape_xml(feed.image_link)}</link>') |
| 105 | xml_parts.append(' </image>') |
| 106 | |
| 107 | # Items |
| 108 | for item in feed.items: |
| 109 | xml_parts.append(render_item(item)) |
| 110 | |
| 111 | xml_parts.append(' </channel>') |
| 112 | xml_parts.append('</rss>') |
| 113 | |
| 114 | return '\n'.join(xml_parts) |