Test du système de notifications en Python.
authorProgfou <jean-christophe.andre@auf.org>
Wed, 1 Feb 2012 08:45:00 +0000 (15:45 +0700)
committerProgfou <jean-christophe.andre@auf.org>
Wed, 1 Feb 2012 08:45:00 +0000 (15:45 +0700)
web/rsswatch.py [new file with mode: 0755]

diff --git a/web/rsswatch.py b/web/rsswatch.py
new file mode 100755 (executable)
index 0000000..151a092
--- /dev/null
@@ -0,0 +1,57 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+# Depends: python-notify python-feedparser
+import sys
+import os.path
+import time
+import pynotify
+import feedparser
+
+SLEEP_TIME = 1800 # seconds
+
+if __name__ == '__main__':
+    print "DEBUG: Initialization..."
+    progname = os.path.basename(sys.argv[0])
+
+    # initialize the notification system
+    if not pynotify.init(progname):
+        print "FATAL: Unable to initialize the notification system!"
+        sys.exit(1)
+    notifier = pynotify.Notification(progname, "Initialization")
+    notifier.show()
+
+    # initialize the data
+    feeds = []
+    for url in sys.argv[1:]:
+        feeds.append({'url': url, 'etag': None, 'modified': time.gmtime(0)})
+    print "DEBUG: Watched feeds: %s" % [f['url'] for f in feeds]
+
+    # main loop
+    while True: # loop forever
+        for feed in feeds:
+            print "DEBUG: processing feed: %s" % feed
+            # check for last data
+            data = feedparser.parse(feed['url'], etag=feed['etag'],
+                                                modified=feed['modified'])
+            # process the result
+            if 'status' not in data: # parse error
+                print "ERROR: %s: %s" % (feed['url'], data['bozo_exception'])
+            elif data['status'] == 304: # no change
+                pass
+            elif data['status'] == 200: # parse ok
+                # update the feed data
+                feed['etag'] = data['etag']
+                feed['modified'] = data['updated']
+                # sort the notification array
+                entries = data['entries']
+                entries.sort(key=lambda e: e['updated_parsed'])
+                # notify about each entry
+                for entry in entries:
+                    notifier.set_property('summary', entry['title'])
+                    notifier.set_property('body', entry['summary'])
+                    #notifier.set_urgency(pynotify.URGENCY_CRITICAL)
+                    notifier.show()
+        # wait a bit before checking again
+        time.sleep(SLEEP_TIME)
+    
+    sys.exit(0)