diff options
author | André Glüpker <git@wgmd.de> | 2021-05-05 20:09:30 +0200 |
---|---|---|
committer | André Glüpker <git@wgmd.de> | 2021-05-05 20:09:30 +0200 |
commit | 5774dbfb2caa42cb55bafab98a40e47f395e44d9 (patch) | |
tree | 8294b7b6fefebc1befeed4104f3b5604683999a8 /rss.py | |
download | rss-feeds-5774dbfb2caa42cb55bafab98a40e47f395e44d9.tar.gz rss-feeds-5774dbfb2caa42cb55bafab98a40e47f395e44d9.tar.bz2 rss-feeds-5774dbfb2caa42cb55bafab98a40e47f395e44d9.zip |
Initial commit of RSS converter application
Diffstat (limited to 'rss.py')
-rwxr-xr-x | rss.py | 56 |
1 files changed, 56 insertions, 0 deletions
@@ -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 = """<?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>""" + + 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>' + 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 += ' </channel>' + feed += '</rss>' + return feed |