diff options
author | André Glüpker <git@wgmd.de> | 2021-05-09 20:13:08 +0200 |
---|---|---|
committer | André Glüpker <git@wgmd.de> | 2021-05-09 20:23:18 +0200 |
commit | 93f3613dab38125d9ae7d5e74498c5395ac80ab0 (patch) | |
tree | 4f1a80138a83c98348464f1b9ddd949766441488 /rss.py | |
parent | 5774dbfb2caa42cb55bafab98a40e47f395e44d9 (diff) | |
download | rss-feeds-93f3613dab38125d9ae7d5e74498c5395ac80ab0.tar.gz rss-feeds-93f3613dab38125d9ae7d5e74498c5395ac80ab0.tar.bz2 rss-feeds-93f3613dab38125d9ae7d5e74498c5395ac80ab0.zip |
Unify method returns / return RSS object
Diffstat (limited to 'rss.py')
-rwxr-xr-x | rss.py | 52 |
1 files changed, 28 insertions, 24 deletions
@@ -21,36 +21,40 @@ def _format_date(dt): "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][dt.month-1], dt.year, dt.hour, dt.minute, dt.second) -def buildRSS(title, url, description, content): +def buildRSS(feed_data): """ - Feed basic info: title, url, descriptions - Content: List[Dict{title, url, content, date, enclosures, guid}] + feed_data = { + title, url, description, + content = [{ + title, url, content, date, [enclosures], guid + }] + } """ - feed = """<?xml version="1.0" encoding="UTF-8"?> + feed = f"""<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/"> <channel> - <title>""" + title + """</title> - <link>""" + url + """</link> - <description>""" + description + """</description> - <lastBuildDate>""" + _format_date(datetime.now()) + """</lastBuildDate>""" + <title>{feed_data['title']}</title> + <link>{feed_data['url']}</link> + <description>{feed_data['description']}</description> + <lastBuildDate>{_format_date(datetime.now())}</lastBuildDate>""" - for item in content: - feed += ' <item>' - feed += ' <title><![CDATA[' + item.get('title', 'N/A') + ']]></title>' - feed += ' <link>' + item.get('url', 'N/A') + '</link>' - feed += ' <description><![CDATA[' + item.get('content', 'N/A') + ']]></description>' - if 'date' in item: - if type(item['date']) is str: - feed += ' <pubDate>' + item['date'] + '</pubDate>' + 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>" else: - feed += ' <pubDate>' + _format_date(item['date']) + '</pubDate>' - for enclosure in item.get('enclosures', []): - feed += ' <media:content url="' + enclosure + '" />' - if 'guid' in item: - feed += ' <guid>' + item['guid'] + '</guid>' - feed += ' </item>' + feed += f" <pubDate>{_format_date(item['date'])}</pubDate>" + for enclosure in item.get("enclosures", []): + feed += f" <media:content url=\"{enclosure}\" />" + if "guid" in item: + feed += f" <guid>{item['guid']}</guid>" + feed += " </item>" - feed += ' </channel>' - feed += '</rss>' + feed += " </channel>" + feed += "</rss>" return feed |