bug fixes
[planet-ucc.git] / update-planet
1 #!/usr/bin/python
2 #
3 # update-planet
4 #
5 # Downloads feeds from the URLs specified and generates the XHTML files.
6 #
7 # (c) 2004, Davyd Madeley <[email protected]>
8 #
9
10 # standard python modules
11 import sys, codecs
12 # planetUCC modules
13 import XMLParse2 as XMLParse, XMLWriter, CacheHandler
14 # planetUCC output plugins
15 import XHTMLWriter, RSS2Writer, RSS1Writer, FOAFWriter, OPMLWriter
16
17 # step 1: read in the config and check each object from cache
18 cache   = CacheHandler.CacheHandler()
19 feeds   = []
20
21 for feed in open('feedlist').readlines():
22         if feed.strip()[0] != '#':
23                 storage         = feed.strip().split('\t')
24                 name, feed      = storage[0], storage[-1]
25                 try:
26                         feeds.append((name, feed, cache.getBlog(name, feed)))
27                 except:
28                         sys.stderr.write('DEBUG: update-planet: something went wrong retrieving feed\n')
29
30 # step 2: process each feed
31 tainted = False
32 blogs   = []
33 for feed in feeds:
34         # XMLParse2 takes two paramaters, a URL and a CacheObject
35         blog    = XMLParse.XMLParse(feed[1], feed[2]).parse()
36         if blog:
37                 blog.blogName   = feed[0]
38                 blog.feedURL    = feed[1]
39                 blogs.append(blog)
40                 # check the old copy of the cache, vs the new copy
41                 if not feed[2] or not feed[2].cache or not blog or not blog.cache or feed[2].cache != blog.cache:
42                         tainted = True
43                 # write the cache back down to disk
44                 cache.storeBlog(blog)
45         else:
46                 pass
47
48 if not tainted:
49         sys.stdout.write('PlanetUCC: no objects have changed in the cache, not updating\n')
50         sys.exit(1)
51
52 # step 3: sift the feeds
53 xmlwriter       = XMLWriter.XMLWriter(blogs)
54
55 # step 4: write feed to disk
56 try:
57         codecs.open('planet.html', 'wb', 'utf-8').write(xmlwriter.write(XHTMLWriter.XHTMLWriter))
58 except:
59         sys.stderr.write('DEBUG: update-planet: could not write planet.html, aborting\n')
60
61 try:
62         codecs.open('rss2.xml', 'wb', 'utf-8').write(xmlwriter.write(RSS2Writer.RSS2Writer))
63 except:
64         sys.stderr.write('DEBUG: update-planet: could not write rss2.xml, aborting\n')
65
66 try:
67         codecs.open('rss1.xml', 'wb', 'utf-8').write(xmlwriter.write(RSS1Writer.RSS1Writer))
68 except:
69         sys.stderr.write('DEBUG: update-planet: could not write rss1.xml, aborting\n')
70
71 try:
72         codecs.open('foaf.xml', 'wb', 'utf-8').write(xmlwriter.write(FOAFWriter.FOAFWriter))
73 except:
74         sys.stderr.write('DEBUG: update-planet: could not write foaf.xml, aborting\n')
75
76 try:
77         codecs.open('opml.xml', 'wb', 'utf-8').write(xmlwriter.write(OPMLWriter.OPMLWriter))
78 except:
79         sys.stderr.write('DEBUG: update-planet: could not write opml.xml, aborting\n')
80         raise

UCC git Repository :: git.ucc.asn.au