Merge Handling into TwythonStreamer, update examples
This commit is contained in:
parent
c3e84bc8ee
commit
97a33ce8dd
5 changed files with 94 additions and 92 deletions
11
README.md
11
README.md
|
|
@ -105,22 +105,19 @@ except TwythonAuthError as e:
|
||||||
##### Streaming API
|
##### Streaming API
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from twython import TwythonStreamer, TwythonStreamHandler
|
from twython import TwythonStreamer
|
||||||
|
|
||||||
|
|
||||||
class MyHandler(TwythonStreamHandler):
|
class MyStreamer(TwythonStreamer):
|
||||||
def on_success(self, data):
|
def on_success(self, data):
|
||||||
print data
|
print data
|
||||||
|
|
||||||
def on_error(self, status_code, data):
|
def on_error(self, status_code, data):
|
||||||
print status_code, data
|
print status_code, data
|
||||||
|
|
||||||
handler = MyHandler()
|
|
||||||
|
|
||||||
# Requires Authentication as of Twitter API v1.1
|
# Requires Authentication as of Twitter API v1.1
|
||||||
stream = TwythonStreamer(APP_KEY, APP_SECRET,
|
stream = MyStreamer(APP_KEY, APP_SECRET,
|
||||||
OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
|
OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
|
||||||
handler)
|
|
||||||
|
|
||||||
stream.statuses.filter(track='twitter')
|
stream.statuses.filter(track='twitter')
|
||||||
```
|
```
|
||||||
|
|
|
||||||
11
README.rst
11
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):
|
def on_success(self, data):
|
||||||
print data
|
print data
|
||||||
|
|
||||||
def on_error(self, status_code, data):
|
def on_error(self, status_code, data):
|
||||||
print status_code, data
|
print status_code, data
|
||||||
|
|
||||||
handler = MyHandler()
|
|
||||||
|
|
||||||
# Requires Authentication as of Twitter API v1.1
|
# Requires Authentication as of Twitter API v1.1
|
||||||
stream = TwythonStreamer(APP_KEY, APP_SECRET,
|
stream = MyStreamer(APP_KEY, APP_SECRET,
|
||||||
OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
|
OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
|
||||||
handler)
|
|
||||||
|
|
||||||
stream.statuses.filter(track='twitter')
|
stream.statuses.filter(track='twitter')
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,16 @@
|
||||||
from twython import TwythonStreamer, TwythonStreamHandler
|
from twython import TwythonStreamer
|
||||||
|
|
||||||
|
|
||||||
class MyHandler(TwythonStreamHandler):
|
class MyStreamer(TwythonStreamer):
|
||||||
def on_success(self, data):
|
def on_success(self, data):
|
||||||
print data
|
print data
|
||||||
|
|
||||||
def on_error(self, status_code, data):
|
def on_error(self, status_code, data):
|
||||||
print status_code, data
|
print status_code, data
|
||||||
|
|
||||||
handler = MyHandler()
|
|
||||||
|
|
||||||
# Requires Authentication as of Twitter API v1.1
|
# Requires Authentication as of Twitter API v1.1
|
||||||
stream = TwythonStreamer(APP_KEY, APP_SECRET,
|
stream = MyStreamer(APP_KEY, APP_SECRET,
|
||||||
OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
|
OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
|
||||||
handler)
|
|
||||||
|
|
||||||
stream.statuses.filter(track='twitter')
|
stream.statuses.filter(track='twitter')
|
||||||
#stream.user(track='twitter')
|
#stream.user(track='twitter')
|
||||||
|
|
|
||||||
|
|
@ -21,5 +21,5 @@ __author__ = 'Ryan McGrath <ryan@venodesigns.net>'
|
||||||
__version__ = '2.9.0'
|
__version__ = '2.9.0'
|
||||||
|
|
||||||
from .twython import Twython
|
from .twython import Twython
|
||||||
from .streaming import TwythonStreamer, TwythonStreamHandler
|
from .streaming import TwythonStreamer
|
||||||
from .exceptions import TwythonError, TwythonRateLimitError, TwythonAuthError
|
from .exceptions import TwythonError, TwythonRateLimitError, TwythonAuthError
|
||||||
|
|
|
||||||
|
|
@ -8,66 +8,6 @@ from requests_oauthlib import OAuth1
|
||||||
import time
|
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 TwythonStreamStatuses(object):
|
||||||
"""Class for different statuses endpoints
|
"""Class for different statuses endpoints
|
||||||
|
|
||||||
|
|
@ -143,8 +83,7 @@ class TwythonStreamTypes(object):
|
||||||
|
|
||||||
class TwythonStreamer(object):
|
class TwythonStreamer(object):
|
||||||
def __init__(self, app_key, app_secret, oauth_token, oauth_token_secret,
|
def __init__(self, app_key, app_secret, oauth_token, oauth_token_secret,
|
||||||
handler, timeout=300, retry_count=None, retry_in=10,
|
timeout=300, retry_count=None, retry_in=10, headers=None):
|
||||||
headers=None):
|
|
||||||
"""Streaming class for a friendly streaming user experience
|
"""Streaming class for a friendly streaming user experience
|
||||||
|
|
||||||
:param app_key: (required) Your applications key
|
:param app_key: (required) Your applications key
|
||||||
|
|
@ -153,8 +92,12 @@ class TwythonStreamer(object):
|
||||||
authenticated calls
|
authenticated calls
|
||||||
:param oauth_token_secret: (required) Used with oauth_token to make
|
:param oauth_token_secret: (required) Used with oauth_token to make
|
||||||
authenticated calls
|
authenticated calls
|
||||||
:param handler: (required) Instance of TwythonStreamHandler to handle
|
:param timeout: (optional) How long (in secs) the streamer should wait
|
||||||
stream responses
|
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
|
:param headers: (optional) Custom headers to send along with the
|
||||||
request
|
request
|
||||||
"""
|
"""
|
||||||
|
|
@ -175,8 +118,6 @@ class TwythonStreamer(object):
|
||||||
|
|
||||||
self.api_version = '1.1'
|
self.api_version = '1.1'
|
||||||
|
|
||||||
self.handler = handler
|
|
||||||
|
|
||||||
self.retry_in = retry_in
|
self.retry_in = retry_in
|
||||||
self.retry_count = retry_count
|
self.retry_count = retry_count
|
||||||
|
|
||||||
|
|
@ -200,11 +141,10 @@ class TwythonStreamer(object):
|
||||||
else:
|
else:
|
||||||
response = func(url, data=params, timeout=self.timeout)
|
response = func(url, data=params, timeout=self.timeout)
|
||||||
except requests.exceptions.Timeout:
|
except requests.exceptions.Timeout:
|
||||||
self.handler.on_timeout()
|
self.on_timeout()
|
||||||
else:
|
else:
|
||||||
if response.status_code != 200:
|
if response.status_code != 200:
|
||||||
self.handler.on_error(response.status_code,
|
self.on_error(response.status_code, response.content)
|
||||||
response.content)
|
|
||||||
|
|
||||||
if self.retry_count and (self.retry_count - retry_counter) > 0:
|
if self.retry_count and (self.retry_count - retry_counter) > 0:
|
||||||
time.sleep(self.retry_in)
|
time.sleep(self.retry_in)
|
||||||
|
|
@ -218,7 +158,78 @@ class TwythonStreamer(object):
|
||||||
for line in response.iter_lines():
|
for line in response.iter_lines():
|
||||||
if line:
|
if line:
|
||||||
try:
|
try:
|
||||||
self.handler.on_success(json.loads(line))
|
self.on_success(json.loads(line))
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise TwythonStreamError('Response was not valid JSON, \
|
raise TwythonStreamError('Response was not valid JSON, \
|
||||||
unable to decode.')
|
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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue