rss_feed: moan about a missing python-pyrss2gen
[webber:webber.git] / plugins / rss_feed.py
1 # -*- coding: iso-8859-1 -*-
2 from webber import *
3 import os, datetime
4 try:
5         import PyRSS2Gen
6 except ImportError:
7         print "rss_feed needs the python module PyRSS2Gen"
8         raise
9
10 items = []
11
12
13 @set_hook("checkconfig")
14 def checkconfig(params):
15         if not cfg.has_key("rss_file"):
16                 log('no "rss_file:" configured, using "feed.rss":', 4)
17                 cfg.rss_file = "feed.rss"
18
19
20 ZERO = datetime.timedelta(0)
21
22 class UTC(datetime.tzinfo):
23     """UTC"""
24
25     def utcoffset(self, dt):
26         return ZERO
27
28     def tzname(self, dt):
29         return "UTC"
30
31     def dst(self, dt):
32         return ZERO
33 utc = UTC()
34
35
36 @set_hook("scan")
37 def sitemap_scan(params):
38         global items
39
40         file = params.file
41         if not file.has_key("linktitle"):
42                 return
43         if not file.has_key("change"):
44                 return
45
46         fname_out = os.path.join(cfg.out_dir, file.out_path)
47         full_url = "http://%s/%s" % (cfg.main_url, fname_out)
48         item = PyRSS2Gen.RSSItem(
49                 title = file["title"],
50                 link = full_url,
51                 guid = PyRSS2Gen.Guid("%s %s" % (full_url, file["mtime"])),
52                 description = file["change"],
53                 pubDate = datetime.datetime.fromtimestamp(file["mtime"], utc),
54         )
55         items.append(item)
56
57
58
59 @set_hook("finish")
60 def finish(params):
61         rss = PyRSS2Gen.RSS2(
62                 title = cfg.subtitle,
63                 link = "http://%s" % cfg.main_url,
64                 description = cfg.subtitle,
65                 lastBuildDate = datetime.datetime.now(),
66                 items = items,
67         )
68         try:
69                 os.makedirs(cfg.out_dir)
70         except:
71                 pass
72         rss.write_xml( open(os.path.join(cfg.out_dir, cfg.rss_file), "w"))