]> jfr.im git - z_archive/twitter.git/blobdiff - twitter/stream.py
Be safe about looking for the Content-Encoding header.
[z_archive/twitter.git] / twitter / stream.py
index 2fd9cd88da489e3e809407fa52b05f56b8c2da78..45b46c0e1bab19e518dd25bcc697c67ea7b3a588 100644 (file)
@@ -7,6 +7,7 @@ except ImportError:
     import urllib2 as urllib_error
 import json
 from ssl import SSLError
     import urllib2 as urllib_error
 import json
 from ssl import SSLError
+import socket
 
 from .api import TwitterCall, wrap_response
 
 
 from .api import TwitterCall, wrap_response
 
@@ -20,6 +21,7 @@ class TwitterJSONIter(object):
 
     def __iter__(self):
         sock = self.handle.fp._sock.fp._sock
 
     def __iter__(self):
         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:
         if not self.block:
             sock.setblocking(False)
         while True:
@@ -51,19 +53,20 @@ def handle_stream_response(req, uri, arg_data, block):
     return iter(TwitterJSONIter(handle, uri, arg_data, block))
 
 class TwitterStreamCall(TwitterCall):
     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):
         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):
     """
         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'))
         iterator = twitter_stream.statuses.sample()
 
         twitter_stream = TwitterStream(auth=UserPassAuth('joe', 'joespassword'))
         iterator = twitter_stream.statuses.sample()
@@ -74,7 +77,7 @@ class TwitterStream(TwitterStreamCall):
     The iterator will yield tweets forever and ever (until the stream
     breaks at which point it raises a TwitterHTTPError.)
 
     The iterator will yield tweets forever and ever (until the stream
     breaks at which point it raises a TwitterHTTPError.)
 
-    The `block` paramater controls if the stream is blocking. Default
+    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.
     """
     is blocking (True). When set to False, the iterator will
     occasionally yield None when there is no available message.
     """