list2form : gestion des erreurs et des petits fichiers (merci cgi.py…)
[progfou.git] / web / rsswatch.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # Depends: python-notify python-feedparser
4 import sys
5 import os.path
6 import time
7 import pynotify
8 import feedparser
9
10 SLEEP_TIME = 1800 # seconds
11
12 if __name__ == '__main__':
13 print "DEBUG: Initialization..."
14 progname = os.path.basename(sys.argv[0])
15
16 # initialize the notification system
17 if not pynotify.init(progname):
18 print "FATAL: Unable to initialize the notification system!"
19 sys.exit(1)
20 notifier = pynotify.Notification(progname, "Initialization")
21 notifier.show()
22
23 # initialize the data
24 feeds = []
25 for url in sys.argv[1:]:
26 feeds.append({'url': url, 'etag': None, 'modified': time.gmtime(0)})
27 print "DEBUG: Watched feeds: %s" % [f['url'] for f in feeds]
28
29 # main loop
30 while True: # loop forever
31 for feed in feeds:
32 print "DEBUG: processing feed: %s" % feed
33 # check for last data
34 data = feedparser.parse(feed['url'], etag=feed['etag'],
35 modified=feed['modified'])
36 # process the result
37 if 'status' not in data: # parse error
38 print "ERROR: %s: %s" % (feed['url'], data['bozo_exception'])
39 elif data['status'] == 304: # no change
40 pass
41 elif data['status'] == 200: # parse ok
42 # update the feed data
43 feed['etag'] = data['etag']
44 feed['modified'] = data['updated']
45 # sort the notification array
46 entries = data['entries']
47 entries.sort(key=lambda e: e['updated_parsed'])
48 # notify about each entry
49 for entry in entries:
50 notifier.set_property('summary', entry['title'])
51 notifier.set_property('body', entry['summary'])
52 #notifier.set_urgency(pynotify.URGENCY_CRITICAL)
53 notifier.show()
54 # wait a bit before checking again
55 time.sleep(SLEEP_TIME)
56
57 sys.exit(0)