Add "on_data" method to stream listener to allow for access raw stream data.
authorJoshua <jroesslein@gmail.com>
Mon, 30 Nov 2009 05:08:24 +0000 (23:08 -0600)
committerJoshua <jroesslein@gmail.com>
Mon, 30 Nov 2009 05:08:24 +0000 (23:08 -0600)
CHANGELOG
tweepy/streaming.py

index 113c8347262e5b57c3fbd1df8ca03347dca1106a..2a5376d8fc5aebd340ef4fc3f8f58d1766aa7da1 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -12,6 +12,8 @@ during upgrade will be listed here.
           s = Stream('test', 'password', MyListener())
           s.sample(async=True)  # threaded mode
           s.filter(track=['pizza']) # synch./blocking mode
+    - Listener now has a "on_data" method which can be overridden to manually handle the
+        raw stream data.
 
 1.2 -> 1.3 [Current release]
 =====================
index de5e8188923ac6c078a9782c465ad3fe64e969ab..98d7057784f270ebd6f12b1217f812fba76237a2 100644 (file)
@@ -29,6 +29,28 @@ STREAM_VERSION = 1
 
 class StreamListener(object):
 
+    def __init__(self):
+        self.api = API()
+
+    def on_data(self, data):
+        """Called when raw data is received from connection.
+
+        Override this method if you wish to manually handle
+        the stream data. Return False to stop stream and close connection.
+        """
+
+        if 'in_reply_to_status_id' in data:
+            status = parse_status(json.loads(data), self.api)
+            if self.on_status(status) is False:
+                return False
+        elif 'delete' in data:
+            delete = json.loads(data)['delete']['status']
+            if self.on_delete(delete['id'], delete['user_id']) is False:
+                return False
+        elif 'limit' in data:
+            if self.on_limit(json.loads(data)['limit']['track']) is False:
+                return False
+
     def on_status(self, status):
         """Called when a new status arrives"""
         return
@@ -128,21 +150,10 @@ class Stream(object):
             else:
                 continue
 
-            # read data
+            # read data and pass into listener
             data = resp.read(length)
-
-            # turn json data into status object
-            if 'in_reply_to_status_id' in data:
-                status = parse_status(json.loads(data), self.api)
-                if self.listener.on_status(status) == False:
-                    self.running = False
-            elif 'delete' in data:
-                delete = json.loads(data)['delete']['status']
-                if self.listener.on_delete(delete['id'], delete['user_id']) == False:
-                    self.running = False
-            elif 'limit' in data:
-                if self.listener.on_limit(json.loads(data)['limit']['track']) == False:
-                    self.running = False
+            if self.listener.on_data(data) is False:
+                self.running = False
 
     def _start(self, async):
         self.running = True