X-Git-Url: https://jfr.im/git/z_archive/twitter.git/blobdiff_plain/2300838f1d3eb7e6b5b339c19b2ec8447b178e3f..04c483abc25f76d9e1faa7c27a14e89464eadc4c:/twitter/stream.py diff --git a/twitter/stream.py b/twitter/stream.py index 1ee9098..071f6b0 100644 --- a/twitter/stream.py +++ b/twitter/stream.py @@ -7,6 +7,8 @@ except ImportError: import urllib2 as urllib_error import json from ssl import SSLError +import socket +import sys from .api import TwitterCall, wrap_response @@ -19,7 +21,11 @@ class TwitterJSONIter(object): self.block = block def __iter__(self): - sock = self.handle.fp._sock.fp._sock + if sys.version_info >= (3, 0): + sock = self.handle.fp.raw._sock + else: + sock = self.handle.fp._sock.fp._sock + sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) if not self.block: sock.setblocking(False) while True: @@ -51,21 +57,22 @@ def handle_stream_response(req, uri, arg_data, block): return iter(TwitterJSONIter(handle, uri, arg_data, block)) class TwitterStreamCall(TwitterCall): - def _handle_response(self, req, uri, arg_data): + def _handle_response(self, req, uri, arg_data, _timeout=None): return handle_stream_response(req, uri, arg_data, block=True) class TwitterStreamCallNonBlocking(TwitterCall): - def _handle_response(self, req, uri, arg_data): + def _handle_response(self, req, uri, arg_data, _timeout=None): return handle_stream_response(req, uri, arg_data, block=False) class TwitterStream(TwitterStreamCall): """ - Interface to the Twitter Stream API (stream.twitter.com). This can - be used pretty much the same as the Twitter class except the - result of calling a method will be an iterator that yields objects - decoded from the stream. For example:: + The TwitterStream object is an interface to the Twitter Stream API + (stream.twitter.com). This can be used pretty much the same as the + Twitter class except the result of calling a method will be an + iterator that yields objects decoded from the stream. For + example:: - twitter_stream = TwitterStream(auth=UserPassAuth('joe', 'joespassword')) + twitter_stream = TwitterStream(auth=OAuth(...)) iterator = twitter_stream.statuses.sample() for tweet in iterator: @@ -74,11 +81,13 @@ class TwitterStream(TwitterStreamCall): The iterator will yield tweets forever and ever (until the stream breaks at which point it raises a TwitterHTTPError.) - The `bloc` paramater controls if the stream is blocking. + The `block` parameter controls if the stream is blocking. Default + is blocking (True). When set to False, the iterator will + occasionally yield None when there is no available message. """ def __init__( self, domain="stream.twitter.com", secure=True, auth=None, - api_version='1', block=True): + api_version='1.1', block=True): uriparts = () uriparts += (str(api_version),)