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]
=====================
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
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