diff options
author | André Glüpker <git@wgmd.de> | 2021-07-29 10:48:55 +0200 |
---|---|---|
committer | André Glüpker <git@wgmd.de> | 2021-07-29 10:51:55 +0200 |
commit | b526cc68929250a7f71ff21ed8410ffd8db87a9d (patch) | |
tree | 54ff4b25b4eea9a142a46741eaf502116b030383 /rss.py | |
parent | af15eade0f59c17c4867baffe0e1dc05033b06c8 (diff) | |
download | rss-feeds-b526cc68929250a7f71ff21ed8410ffd8db87a9d.tar.gz rss-feeds-b526cc68929250a7f71ff21ed8410ffd8db87a9d.tar.bz2 rss-feeds-b526cc68929250a7f71ff21ed8410ffd8db87a9d.zip |
Format using black
Diffstat (limited to 'rss.py')
-rwxr-xr-x | rss.py | 56 |
1 files changed, 43 insertions, 13 deletions
@@ -3,6 +3,7 @@ from datetime import datetime from typing import List + def _format_date(dt): """convert a datetime into an RFC 822 formatted date Input date must be in GMT. @@ -15,12 +16,39 @@ def _format_date(dt): # Isn't there a standard way to do this for Python? The # rfc822 and email.Utils modules assume a timestamp. The # following is based on the rfc822 module. + weekdays = [ + "Mon", + "Tue", + "Wed", + "Thu", + "Fri", + "Sat", + "Sun", + ] + months = [ + "Jan", + "Feb", + "Mar", + "Apr", + "May", + "Jun", + "Jul", + "Aug", + "Sep", + "Oct", + "Nov", + "Dec", + ] return "%s, %02d %s %04d %02d:%02d:%02d GMT" % ( - ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"][dt.weekday()], - dt.day, - ["Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][dt.month-1], - dt.year, dt.hour, dt.minute, dt.second) + weekdays[dt.weekday()], + dt.day, + months[dt.month - 1], + dt.year, + dt.hour, + dt.minute, + dt.second, + ) + class RSSItem: title: str @@ -40,12 +68,12 @@ class RSSFeed: def buildRSS(feed_data: RSSFeed): """ - feed_data = { - title, url, description, - content = [{ - title, url, content, date, [enclosures], guid - }] - } + feed_data = { + title, url, description, + content = [{ + title, url, content, date, [enclosures], guid + }] + } """ feed = f"""<?xml version="1.0" encoding="UTF-8"?> @@ -60,14 +88,16 @@ def buildRSS(feed_data: RSSFeed): 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>" + 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 += f" <pubDate>{_format_date(item['date'])}</pubDate>" for enclosure in item.get("enclosures", []): - feed += f" <media:content url=\"{enclosure}\" />" + feed += f' <media:content url="{enclosure}" />' if "guid" in item: feed += f" <guid>{item['guid']}</guid>" feed += " </item>" |