Removed erroneous sys import
[minutes-rss.git] / minutes-rss.py
1 #!/usr/bin/python
2
3 # UCC Minutes Arse (RSS) generator
4
5 import subprocess
6 import shlex
7 import os
8 import time
9 from cgi import escape
10
11 MINUTESURL = 'http://www.ucc.asn.au/infobase/minutes/'
12 MINUTESPATH = '/services/http/infobase/minutes/'
13
14 def RFCTime(pubtime):
15         return time.strftime("%a, %d %b %Y %H:%M:%S +0800", pubtime)
16
17 class UCCMinutes:
18         '''Loads relevant information out of UCC minutes'''
19         def __init__(self, path):
20                 if os.path.exists(path):
21                         self.stattime = os.stat(path)[-2]
22                         self.modtime = RFCTime(time.localtime(self.stattime))
23                         f = open(path,'r')
24                         self.body = escape(f.read())
25                         f.close()
26
27                         self.title = self.body.split('\n')[0].strip()
28
29                         webpath = path.split('/')[-2:]
30                         self.url = MINUTESURL + '/'.join(webpath)
31
32 def RSS(items,pubtime=''):
33         pubtime = pubtime or RFCTime(time.localtime())
34         output = '''<?xml version='1.0' encoding='utf-8' ?>
35 <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
36 <channel>
37  <atom:link href="http://www.ucc.asn.au/infobase/minutes/minutes.xml" rel="self" type="application/rss+xml" />
38  <title>UCC Meeting Minutes</title>
39  <link>http://www.ucc.asn.au/infobase/minutes/</link>
40  <description>Meeting Minutes of the University Computer Club</description>
41 '''
42         output += ' <lastBuildDate>%s</lastBuildDate>\n'%pubtime
43         output += ''' <generator>The giant hack that is Python by JCF</generator>
44  <copyright>The University Computer Club &#x3C;[email protected]&#x3E;</copyright>
45
46 '''
47         output += items
48         output += '</channel>\n</rss>'
49
50         return output
51
52 def itemWriter(minutes):
53         output = '<item>\n'
54         output += '<guid>%s</guid>\n'%minutes.url
55         output += '<title>%s</title>\n'%minutes.title
56         output += '<pubDate>%s</pubDate>\n'%minutes.modtime
57         output += '<link>%s</link>\n'%minutes.url
58         output += '<description><![CDATA[<pre>%s</pre>]]></description>\n'%minutes.body
59         output += '</item>\n'
60         return output
61
62
63 if __name__ == '__main__':
64         xmlpath = os.path.join(MINUTESPATH, 'minutes.xml')
65         try:
66                 lastupdate = os.stat(xmlpath)[-2]
67         except OSError:
68                 lastupdate = 0
69
70         # BEWARE THE GIANT HACK - This should be replaced before it burns someone
71         args = shlex.split('/usr/bin/find %s'%MINUTESPATH + ' -maxdepth 2 -iname "*txt" -type f -printf "%TY-%Tm-%Td %TT %p\n"')
72         sub = subprocess.Popen(args, stdout=subprocess.PIPE)
73         items = sub.communicate()[0].split('\n')
74         items.sort()
75
76         items = [os.path.abspath(x.split(' ')[-1]) for x in items[-15:]]
77         items.reverse()
78
79         latestItem = UCCMinutes(items[0])
80
81         if latestItem.stattime > lastupdate:
82                 minutes = [latestItem] + [UCCMinutes(x) for x in items[1:]]
83                 output = RSS(''.join([itemWriter(x) for x in minutes]), minutes[0].modtime)
84                 open(xmlpath, 'w').write(output)

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