From 97a33ce8dd5cb32334d454889d44ed1d828622bd Mon Sep 17 00:00:00 2001 From: Mike Helmick Date: Fri, 3 May 2013 16:57:59 -0400 Subject: [PATCH] Merge Handling into TwythonStreamer, update examples --- README.md | 11 ++-- README.rst | 11 ++-- examples/stream.py | 11 ++-- twython/__init__.py | 2 +- twython/streaming.py | 151 +++++++++++++++++++++++-------------------- 5 files changed, 94 insertions(+), 92 deletions(-) diff --git a/README.md b/README.md index 6d8bdc2..3504336 100644 --- a/README.md +++ b/README.md @@ -105,22 +105,19 @@ except TwythonAuthError as e: ##### Streaming API ```python -from twython import TwythonStreamer, TwythonStreamHandler +from twython import TwythonStreamer -class MyHandler(TwythonStreamHandler): +class MyStreamer(TwythonStreamer): def on_success(self, data): print data def on_error(self, status_code, data): print status_code, data -handler = MyHandler() - # Requires Authentication as of Twitter API v1.1 -stream = TwythonStreamer(APP_KEY, APP_SECRET, - OAUTH_TOKEN, OAUTH_TOKEN_SECRET, - handler) +stream = MyStreamer(APP_KEY, APP_SECRET, + OAUTH_TOKEN, OAUTH_TOKEN_SECRET) stream.statuses.filter(track='twitter') ``` diff --git a/README.rst b/README.rst index 9c5f58e..ab806cb 100644 --- a/README.rst +++ b/README.rst @@ -114,22 +114,19 @@ Streaming API :: - from twython import TwythonStreamer, TwythonStreamHandler + from twython import TwythonStreamer - class MyHandler(TwythonStreamHandler): + class MyStreamer(TwythonStreamer): def on_success(self, data): print data def on_error(self, status_code, data): print status_code, data - handler = MyHandler() - # Requires Authentication as of Twitter API v1.1 - stream = TwythonStreamer(APP_KEY, APP_SECRET, - OAUTH_TOKEN, OAUTH_TOKEN_SECRET, - handler) + stream = MyStreamer(APP_KEY, APP_SECRET, + OAUTH_TOKEN, OAUTH_TOKEN_SECRET) stream.statuses.filter(track='twitter') diff --git a/examples/stream.py b/examples/stream.py index 0fee30c..f5c5f1a 100644 --- a/examples/stream.py +++ b/examples/stream.py @@ -1,19 +1,16 @@ -from twython import TwythonStreamer, TwythonStreamHandler +from twython import TwythonStreamer -class MyHandler(TwythonStreamHandler): +class MyStreamer(TwythonStreamer): def on_success(self, data): print data def on_error(self, status_code, data): print status_code, data -handler = MyHandler() - # Requires Authentication as of Twitter API v1.1 -stream = TwythonStreamer(APP_KEY, APP_SECRET, - OAUTH_TOKEN, OAUTH_TOKEN_SECRET, - handler) +stream = MyStreamer(APP_KEY, APP_SECRET, + OAUTH_TOKEN, OAUTH_TOKEN_SECRET) stream.statuses.filter(track='twitter') #stream.user(track='twitter') diff --git a/twython/__init__.py b/twython/__init__.py index 481750e..23f2eef 100644 --- a/twython/__init__.py +++ b/twython/__init__.py @@ -21,5 +21,5 @@ __author__ = 'Ryan McGrath ' __version__ = '2.9.0' from .twython import Twython -from .streaming import TwythonStreamer, TwythonStreamHandler +from .streaming import TwythonStreamer from .exceptions import TwythonError, TwythonRateLimitError, TwythonAuthError diff --git a/twython/streaming.py b/twython/streaming.py index 0666367..2c04cd3 100644 --- a/twython/streaming.py +++ b/twython/streaming.py @@ -8,66 +8,6 @@ from requests_oauthlib import OAuth1 import time -class TwythonStreamHandler(object): - def on_success(self, data): - """Called when data has been successfull received from the stream - - Feel free to override this in your own handler. - See https://dev.twitter.com/docs/streaming-apis/messages for messages - sent along in stream responses. - - :param data: dict of data recieved from the stream - """ - - if 'delete' in data: - self.on_delete(data.get('delete')) - elif 'limit' in data: - self.on_limit(data.get('limit')) - elif 'disconnect' in data: - self.on_disconnect(data.get('disconnect')) - - def on_error(self, status_code, data): - """Called when stream returns non-200 status code - - :param status_code: Non-200 status code sent from stream - :param data: Error message sent from stream - """ - return - - def on_delete(self, data): - """Called when a deletion notice is received - - Twitter docs for deletion notices: http://spen.se/8qujd - - :param data: dict of data from the 'delete' key recieved from - the stream - """ - return data - - def on_limit(self, data): - """Called when a limit notice is received - - Twitter docs for limit notices: http://spen.se/hzt0b - - :param data: dict of data from the 'limit' key recieved from - the stream - """ - return data - - def on_disconnect(self, data): - """Called when a disconnect notice is received - - Twitter docs for disconnect notices: http://spen.se/xb6mm - - :param data: dict of data from the 'disconnect' key recieved from - the stream - """ - return data - - def on_timeout(self): - return - - class TwythonStreamStatuses(object): """Class for different statuses endpoints @@ -143,8 +83,7 @@ class TwythonStreamTypes(object): class TwythonStreamer(object): def __init__(self, app_key, app_secret, oauth_token, oauth_token_secret, - handler, timeout=300, retry_count=None, retry_in=10, - headers=None): + timeout=300, retry_count=None, retry_in=10, headers=None): """Streaming class for a friendly streaming user experience :param app_key: (required) Your applications key @@ -153,8 +92,12 @@ class TwythonStreamer(object): authenticated calls :param oauth_token_secret: (required) Used with oauth_token to make authenticated calls - :param handler: (required) Instance of TwythonStreamHandler to handle - stream responses + :param timeout: (optional) How long (in secs) the streamer should wait + for a response from Twitter Streaming API + :param retry_count: (optional) Number of times the API call should be + retired + :param retry_in: (optional) Amount of time (in secs) the previous + API call should be tried again :param headers: (optional) Custom headers to send along with the request """ @@ -175,8 +118,6 @@ class TwythonStreamer(object): self.api_version = '1.1' - self.handler = handler - self.retry_in = retry_in self.retry_count = retry_count @@ -200,11 +141,10 @@ class TwythonStreamer(object): else: response = func(url, data=params, timeout=self.timeout) except requests.exceptions.Timeout: - self.handler.on_timeout() + self.on_timeout() else: if response.status_code != 200: - self.handler.on_error(response.status_code, - response.content) + self.on_error(response.status_code, response.content) if self.retry_count and (self.retry_count - retry_counter) > 0: time.sleep(self.retry_in) @@ -218,7 +158,78 @@ class TwythonStreamer(object): for line in response.iter_lines(): if line: try: - self.handler.on_success(json.loads(line)) + self.on_success(json.loads(line)) except ValueError: raise TwythonStreamError('Response was not valid JSON, \ unable to decode.') + + def on_success(self, data): + """Called when data has been successfull received from the stream + + Feel free to override this to handle your streaming data how you + want it handled. + See https://dev.twitter.com/docs/streaming-apis/messages for messages + sent along in stream responses. + + :param data: dict of data recieved from the stream + """ + + if 'delete' in data: + self.on_delete(data.get('delete')) + elif 'limit' in data: + self.on_limit(data.get('limit')) + elif 'disconnect' in data: + self.on_disconnect(data.get('disconnect')) + + def on_error(self, status_code, data): + """Called when stream returns non-200 status code + + Feel free to override this to handle your streaming data how you + want it handled. + + :param status_code: Non-200 status code sent from stream + :param data: Error message sent from stream + """ + return + + def on_delete(self, data): + """Called when a deletion notice is received + + Feel free to override this to handle your streaming data how you + want it handled. + + Twitter docs for deletion notices: http://spen.se/8qujd + + :param data: dict of data from the 'delete' key recieved from + the stream + """ + return + + def on_limit(self, data): + """Called when a limit notice is received + + Feel free to override this to handle your streaming data how you + want it handled. + + Twitter docs for limit notices: http://spen.se/hzt0b + + :param data: dict of data from the 'limit' key recieved from + the stream + """ + return + + def on_disconnect(self, data): + """Called when a disconnect notice is received + + Feel free to override this to handle your streaming data how you + want it handled. + + Twitter docs for disconnect notices: http://spen.se/xb6mm + + :param data: dict of data from the 'disconnect' key recieved from + the stream + """ + return + + def on_timeout(self): + return