Merge Handling into TwythonStreamer, update examples

This commit is contained in:
Mike Helmick 2013-05-03 16:57:59 -04:00
parent c3e84bc8ee
commit 97a33ce8dd
5 changed files with 94 additions and 92 deletions

View file

@ -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')
```

View file

@ -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')

View file

@ -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')

View file

@ -21,5 +21,5 @@ __author__ = 'Ryan McGrath <ryan@venodesigns.net>'
__version__ = '2.9.0'
from .twython import Twython
from .streaming import TwythonStreamer, TwythonStreamHandler
from .streaming import TwythonStreamer
from .exceptions import TwythonError, TwythonRateLimitError, TwythonAuthError

View file

@ -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