diff options
Diffstat (limited to 'rss.py')
-rwxr-xr-x | rss.py | 38 |
1 files changed, 19 insertions, 19 deletions
@@ -1,7 +1,7 @@ #!/usr/bin/env python3 +from dataclasses import dataclass from datetime import datetime -from typing import List def _format_date(dt): @@ -50,20 +50,22 @@ def _format_date(dt): ) +@dataclass class RSSItem: title: str url: str content: str date: str - enclosures: List[str] + enclosures: list[str] guid: str +@dataclass class RSSFeed: title: str url: str description: str - content: List[RSSItem] + content: list[RSSItem] def escape(str): @@ -84,27 +86,25 @@ def buildRSS(feed_data: RSSFeed): feed = f"""<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/"> <channel> - <title>{feed_data['title']}</title> - <link>{feed_data['url']}</link> - <description>{escape(feed_data['description'])}</description> + <title>{feed_data.title}</title> + <link>{feed_data.url}</link> + <description>{escape(feed_data.description)}</description> <lastBuildDate>{_format_date(datetime.now())}</lastBuildDate>""" - for item in feed_data["content"]: + for item in feed_data.content: feed += " <item>" - feed += f" <title><![CDATA[{item.get('title', 'N/A')}]]></title>" - feed += f" <link>{item.get('url', 'N/A')}</link>" - feed += ( - f" <description><![CDATA[{item.get('content', 'N/A')}]]></description>" - ) - if "date" in item: - if type(item["date"]) is str: - feed += f" <pubDate>{item['date']}</pubDate>" + feed += f" <title><![CDATA[{item.title or 'N/A'}]]></title>" + feed += f" <link>{item.url or 'N/A'}</link>" + feed += f" <description><![CDATA[{item.content or 'N/A'}]]></description>" + if item.date: + if isinstance(item.date, str): + feed += f" <pubDate>{item.date}</pubDate>" else: - feed += f" <pubDate>{_format_date(item['date'])}</pubDate>" - for enclosure in item.get("enclosures", []): + feed += f" <pubDate>{_format_date(item.date)}</pubDate>" + for enclosure in item.enclosures: feed += f' <media:content url="{enclosure}" />' - if "guid" in item: - feed += f" <guid>{item['guid']}</guid>" + if item.guid: + feed += f" <guid>{item.guid}</guid>" feed += " </item>" feed += " </channel>" |