summaryrefslogtreecommitdiff
path: root/rss.py
diff options
context:
space:
mode:
authorAndré Glüpker <git@wgmd.de>2024-05-01 10:02:10 +0200
committerAndré Glüpker <git@wgmd.de>2024-05-01 10:04:51 +0200
commitfdb01d72cbd4c5aafea32eca517dbf9ef7bbc62d (patch)
tree6f2213e91be3ea8ef09402d9f1d18cc4816e966c /rss.py
parent9e75b5cb4b64e1fe430ce3720dbe3f80988d8435 (diff)
downloadrss-feeds-fdb01d72cbd4c5aafea32eca517dbf9ef7bbc62d.tar.gz
rss-feeds-fdb01d72cbd4c5aafea32eca517dbf9ef7bbc62d.tar.bz2
rss-feeds-fdb01d72cbd4c5aafea32eca517dbf9ef7bbc62d.zip
Configure and run ruff (check & format)
Diffstat (limited to 'rss.py')
-rwxr-xr-xrss.py38
1 files changed, 19 insertions, 19 deletions
diff --git a/rss.py b/rss.py
index 8625c73..ccf0cb5 100755
--- a/rss.py
+++ b/rss.py
@@ -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>"