root/hacks/trunk/statusbot/statusbot.py

Revision 55, 1.2 kB (checked in by verbosus, 22 months ago)

Removed htmlescape in favor of cgi.escape (thanks, C8E)

Line 
1# -*- coding: utf-8 -*-
2
3"""Statusbot, a GTalk-based (Jabber) status publisher
4"""
5
6import re
7from cgi import escape
8import time, sys, codecs
9import jabberbot
10
11__author__ = "Antonio Cavedoni <antonio@cavedoni.org>"
12__svnid__ = "$Id$"
13
14def extract_link(input):
15    link_r = re.compile(r'(?P<desc>.*?): (?P<uri>(http://.*)+)')
16    results = link_r.search(input)
17    if results:
18        return u'<a href="%s">%s</a>' % \
19            (results.group('uri'), escape(results.group('desc')))
20    else:
21        return escape(input)
22
23def save_status(status):
24    """Write status (a unicode string) to filename (as UTF-8)"""
25    template = codecs.open('/path/to/template.html', 'r', 'utf-8')
26    t = template.read()
27    f = codecs.open('/path/to/output.html', 'w', 'utf-8')
28    status = extract_link(status)
29    f.write(t.replace(u'{{ status }}', status))
30    template.close()
31    f.close()
32   
33if __name__ == "__main__":
34    statusbot = jabberbot.JabberBot('username', 'password')
35    statusbot.connect()
36    for x in xrange(10):
37        statusbot.check_events()
38        status = statusbot.roster.getStatus('jabberid')
39        if status:
40            save_status(status)
41            sys.exit(0)
42        time.sleep(0.5)
Note: See TracBrowser for help on using the browser.