From f85201f429b752dcaec4b39172570018c2f620a6 Mon Sep 17 00:00:00 2001 From: Joshua Date: Sun, 29 Nov 2009 23:08:24 -0600 Subject: [PATCH] Add "on_data" method to stream listener to allow for access raw stream data. --- CHANGELOG | 2 ++ tweepy/streaming.py | 39 +++++++++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 113c834..2a5376d 100644 --- 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] ===================== diff --git a/tweepy/streaming.py b/tweepy/streaming.py index de5e818..98d7057 100644 --- a/tweepy/streaming.py +++ b/tweepy/streaming.py @@ -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 -- 2.25.1