Created
October 4, 2010 17:58
-
-
Save fleger/610149 to your computer and use it in GitHub Desktop.
Command-line utility to create RSS feeds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
import sys | |
import os | |
import datetime | |
import feedparser | |
import PyRSS2Gen | |
from optparse import OptionParser | |
def main(): | |
criteria = lambda x: True | |
parser = OptionParser(usage="Usage: %prog [options] fileName [itemTitle] [itemDescription] [itemLink]") | |
parser.add_option("-t", "--feed-title", dest="feedTitle", help="title of the feed", metavar="TITLE", default=None) | |
parser.add_option("-d", "--feed-description", dest="feedDescription", help="description of the feed", metavar="DESCRIPTION", default=None) | |
parser.add_option("-l", "--feed-link", dest="feedLink", help="link to the feed", metavar="URL", default=None) | |
(options, args) = parser.parse_args() | |
if len(args) <= 0: | |
print "Need at least a file name !" | |
sys.exit(1) | |
if os.path.isfile(args[0]): | |
with open(args[0]) as f: | |
feed = feedparser.parse(f) | |
items = [PyRSS2Gen.RSSItem( | |
title = unicode(x.title) if "title" in x else None, | |
link = unicode(x.link) if "link" in x else None, | |
description = unicode(x.summary) if "summary" in x else None, | |
#guid = unicode(x.link) if "link" in x else None, | |
pubDate = datetime.datetime( | |
x.modified_parsed[0], | |
x.modified_parsed[1], | |
x.modified_parsed[2], | |
x.modified_parsed[3], | |
x.modified_parsed[4], | |
x.modified_parsed[5], | |
) | |
) | |
for x in feed.entries if criteria(x) | |
] | |
feedTitle = unicode(options.feedTitle, "utf-8") if options.feedTitle is not None else unicode(feed.feed.title) | |
feedLink = unicode(options.feedLink, "utf-8") if options.feedLink is not None else unicode(feed.feed.link) | |
feedDescription = unicode(options.feedDescription, "utf-8") if options.feedDescription is not None else unicode(feed.feed.subtitle) | |
else: | |
feedTitle = unicode(options.feedTitle, "utf-8") if options.feedTitle is not None else u"" | |
feedLink = unicode(options.feedLink, "utf-8") if options.feedLink is not None else u"" | |
feedDescription = unicode(options.feedDescription, "utf-8") if options.feedDescription is not None else u"" | |
items = [] | |
if len(args) > 1: | |
item = PyRSS2Gen.RSSItem( | |
title = unicode(args[1], "utf-8") if len(args) >= 2 else None, | |
description = unicode(args[2], "utf-8") if len(args) >= 3 else None, | |
link = unicode(args[3], "utf-8") if len(args) >= 4 else None, | |
#guid = unicode(args[3], "utf-8") if len(args) >= 4 else None, | |
pubDate = datetime.datetime.utcnow() | |
) | |
items.insert(0, item) | |
rss = PyRSS2Gen.RSS2( | |
title = feedTitle, | |
link = feedLink, | |
description = feedDescription, | |
lastBuildDate = datetime.datetime.utcnow(), | |
items = items | |
) | |
with open(args[0], 'w') as f: | |
rss.write_xml(f, "utf-8") | |
if __name__=="__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment