From 5774dbfb2caa42cb55bafab98a40e47f395e44d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Gl=C3=BCpker?= Date: Wed, 5 May 2021 20:09:30 +0200 Subject: Initial commit of RSS converter application --- rss.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100755 rss.py (limited to 'rss.py') diff --git a/rss.py b/rss.py new file mode 100755 index 0000000..66ffb35 --- /dev/null +++ b/rss.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 + +from datetime import datetime + +def _format_date(dt): + """convert a datetime into an RFC 822 formatted date + Input date must be in GMT. + Stolen from PyRSS2Gen. + """ + # Looks like: + # Sat, 07 Sep 2002 00:00:01 GMT + # Can't use strftime because that's locale dependent + # + # 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. + 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) + +def buildRSS(title, url, description, content): + """ + Feed basic info: title, url, descriptions + Content: List[Dict{title, url, content, date, enclosures, guid}] + """ + + feed = """ + + + """ + title + """ + """ + url + """ + """ + description + """ + """ + _format_date(datetime.now()) + """""" + + for item in content: + feed += ' ' + feed += ' <![CDATA[' + item.get('title', 'N/A') + ']]>' + feed += ' ' + item.get('url', 'N/A') + '' + feed += ' ' + if 'date' in item: + if type(item['date']) is str: + feed += ' ' + item['date'] + '' + else: + feed += ' ' + _format_date(item['date']) + '' + for enclosure in item.get('enclosures', []): + feed += ' ' + if 'guid' in item: + feed += ' ' + item['guid'] + '' + feed += ' ' + + feed += ' ' + feed += '' + return feed -- cgit v1.2.3