]> oss.titaniummirror.com Git - webber.git/blobdiff - plugins/rss_feed.py
rss_feed.py: sort items by modification time
[webber.git] / plugins / rss_feed.py
index a4197b1e16fd37845f51d363ad08ede01f6a7fee..1dbc11e25b874ffad8157b9cf3d561d7a2e22605 100644 (file)
@@ -1,6 +1,6 @@
 # -*- coding: iso-8859-1 -*-
 from webber import *
-import os, datetime
+import os, datetime, re
 try:
        import PyRSS2Gen
 except ImportError:
@@ -17,8 +17,8 @@ def checkconfig(params):
                cfg.rss_file = "feed.rss"
 
 
+# Helper class needed for datetime.datetime to generate GMT timestamps
 ZERO = datetime.timedelta(0)
-
 class UTC(datetime.tzinfo):
     """UTC"""
 
@@ -52,27 +52,41 @@ def sitemap_scan(params):
                link = full_url,
                guid = PyRSS2Gen.Guid("%s %s" % (full_url, file["mtime"]), isPermaLink=0),
                description = change,
-               pubDate = datetime.datetime.fromtimestamp(file["mtime"], utc),
+               pubDate = file["mtime"]
        )
        items.append(item)
 
 
-
 @set_hook("finish")
 def finish(params):
+       # Sort items by pubDate, which still holds the mtime
+       items.sort(key=lambda i: i.pubDate, reverse=True)
+
+       # convert mtime to real pupDate
+       for i in items:
+               i.pubDate = datetime.datetime.fromtimestamp(i.pubDate, utc)
+
        rss = PyRSS2Gen.RSS2(
                title = cfg.subtitle,
                link = "http://%s" % cfg.main_url,
                description = cfg.subtitle,
-               lastBuildDate = datetime.datetime.now(),
+               lastBuildDate = datetime.datetime.now(utc),
                items = items,
        )
+       # Step one of self-reference
+       # (see http://feedvalidator.org/docs/warning/MissingAtomSelfLink.html)
+       rss.rss_attrs["xmlns:atom"] = "http://www.w3.org/2005/Atom"
+
        try:
                os.makedirs(cfg.out_dir)
        except:
                pass
        f = open(os.path.join(cfg.out_dir, cfg.rss_file), "w")
        # Ugly XML beautification
-       s = rss.to_xml().replace("<", "\n<").replace("\n\n", "\n")[1:]
-       f.write(s)
+       s = rss.to_xml()
+       s = re.sub("<(?!/)", "\n<", s)
+       s = s.replace("\n\n", "\n")
+       # Step two of self-reference
+       s = s.replace('<channel>', '<channel>\n<atom:link href="http://%s/%s" rel="self" type="application/rss+xml" />' % (cfg.main_url, cfg.rss_file))
+       f.write(s[1:])
        f.write("\n")