Loading...

Blog: Simple Notify script for TracTalk: coderesort-talk-notifier.py

File coderesort-talk-notifier.py, 2.3 KB (added by simon@…, 10 years ago)

Python Talk notify script.

Line 
1#!/usr/bin/env python
2""" An small example plugin that marks you as online in specified
3    projects + notifies you of any activity.
4    Requires: OSX, Growl + Growl python bindings (SDK)
5"""
6from getpass import getpass
7import sys, xmlrpclib, time
8import Growl
9
10SLEEPY = 115 # <120 to support 'seen' by marking presence (without dropouts)
11STICKY = False # True to manually dismiss each Growl notification
12SERVER = 'www.coderesort.com'
13ICON = './coderesort-icon.png'
14
15def log(message):
16    print('[%s] %s' % (time.strftime('%Y-%m-%d %X'), message))
17
18def main():
19    try:
20        username = sys.argv[1]
21        status = dict.fromkeys(sys.argv[2:])
22    except:
23        print("\nusage: %s authemail proj1 proj2 proj3\n" % __file__)
24        sys.exit(1)
25    notifier = Growl.GrowlNotifier(notifications=['talk'],
26                            applicationIcon=Growl.Image.imageFromPath(ICON))
27    notifier.register()
28    base_url = 'https://' + username + ':' + getpass('Password: ') + '@' \
29                    + SERVER + '/p/%s/login/rpc'
30    while True:
31        log('Polling....')
32        for project in status.keys():
33            server = xmlrpclib.ServerProxy(base_url % project)
34            talks = server.talk.getTalks()
35            if not talks:
36                continue # nothing to query
37            multicall_msg = xmlrpclib.MultiCall(server)
38            multicall_seen = xmlrpclib.MultiCall(server)
39            for r in talks: # get latest message for talks + mark as seen
40                multicall_msg.talk.getMessages(r['room'],
41                            {'limit': 1, 'reverse': True})
42                multicall_seen.talk.markSeen(r['room'])
43            messages = [(r[0][0]['room'], r[0][0]['line']) for \
44                            r in multicall_msg().results]
45            multicall_seen()
46            if status[project] and messages != status[project]:
47                diff = sorted(list(set(messages).difference(status[project])))
48                log("'%s' changes: %s " % (project, repr(diff)))
49                notifier.notify('talk', project, 'Action in rooms: %s' % \
50                    ", ".join([str(room) for room, _ in diff]), sticky=STICKY)
51            status[project] = messages
52        time.sleep(SLEEPY)
53
54if __name__ == '__main__':
55    try:
56        main()
57    except KeyboardInterrupt:
58        print(''); sys.exit(0)