diff options
author | André Glüpker <git@wgmd.de> | 2024-05-01 10:20:30 +0200 |
---|---|---|
committer | André Glüpker <git@wgmd.de> | 2024-05-01 10:20:30 +0200 |
commit | b335495aadc9f5116a88d0359087ae33c9524aed (patch) | |
tree | 674f79b4dea91d17d56bf4a23ef91f5e689df347 | |
parent | 121f1890b1618c92d7a8fc6d174881e66ab6b7c1 (diff) | |
download | rss-feeds-b335495aadc9f5116a88d0359087ae33c9524aed.tar.gz rss-feeds-b335495aadc9f5116a88d0359087ae33c9524aed.tar.bz2 rss-feeds-b335495aadc9f5116a88d0359087ae33c9524aed.zip |
Fix dataclass usage
-rwxr-xr-x | rss.py | 30 | ||||
-rwxr-xr-x | rss_types.py | 21 | ||||
-rwxr-xr-x | test.py | 12 | ||||
-rwxr-xr-x | zdf.py | 35 |
4 files changed, 55 insertions, 43 deletions
@@ -1,8 +1,9 @@ #!/usr/bin/env python3 -from dataclasses import dataclass from datetime import datetime +from rss_types import RSSFeed + def _format_date(dt): """convert a datetime into an RFC 822 formatted date @@ -50,39 +51,12 @@ def _format_date(dt): ) -@dataclass -class RSSItem: - title: str - url: str - content: str - date: str - enclosures: list[str] - guid: str - - -@dataclass -class RSSFeed: - title: str - url: str - description: str - content: list[RSSItem] - - def escape(str): str = str.replace("&", "&") return str def buildRSS(feed_data: RSSFeed): - """ - feed_data = { - title, url, description, - content = [{ - title, url, content, date, [enclosures], guid - }] - } - """ - feed = f"""<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/"> <channel> diff --git a/rss_types.py b/rss_types.py new file mode 100755 index 0000000..4217b29 --- /dev/null +++ b/rss_types.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 + +from dataclasses import dataclass + + +@dataclass +class RSSItem: + title: str + url: str + content: str + date: str + enclosures: list[str] + guid: str + + +@dataclass +class RSSFeed: + title: str + url: str + description: str + content: list[RSSItem] @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 + +import logging + +from rss import buildRSS +from zdf import zdf + +logging.basicConfig(level=logging.INFO) + +zdf_data = zdf("comedy/heute-show") +rss = buildRSS(zdf_data) +print(rss) @@ -1,8 +1,12 @@ +#!/usr/bin/env python3 + import logging from datetime import datetime from urllib.request import Request, urlopen from xml.dom.minidom import parseString +from rss_types import RSSFeed, RSSItem + def getText(dom, element): textNode = dom.getElementsByTagName(element)[0].firstChild @@ -31,7 +35,7 @@ def zdf(feed): title = getText(xml, "title") description = getText(xml, "description") - content = [] + rss_items = [] for show in xml.getElementsByTagName("item"): s_url = getText(show, "link") if not s_url: @@ -61,22 +65,23 @@ def zdf(feed): s_desc = getText(show, "description") s_guid = getText(show, "guid") - content.append( - { - "title": s_title, - "url": s_url, - "content": s_desc, - "date": s_date, - "guid": s_guid, - } + rss_items.append( + RSSItem( + title=s_title, + url=s_url, + content=s_desc, + date=s_date, + guid=s_guid, + enclosures=[], + ) ) - return { - "title": title, - "url": url, - "description": description, - "content": content, - } + return RSSFeed( + title=title, + url=url, + description=description, + content=rss_items, + ) except Exception as exc: logging.error("Working with zdf failed.", exc_info=exc) return None |