X-Git-Url: https://jfr.im/git/z_archive/twitter.git/blobdiff_plain/40db782dcb9584b10a3307a5841c7e62780d60e4..ef99d73454c85384ada9b11abf29571f504e6db8:/twitter/stream.py diff --git a/twitter/stream.py b/twitter/stream.py index 83079d5..0d8d86f 100644 --- a/twitter/stream.py +++ b/twitter/stream.py @@ -17,6 +17,8 @@ class TwitterJSONIter(object): def __init__(self, handle, uri, arg_data, block=True, timeout=None): self.decoder = json.JSONDecoder() self.handle = handle + self.uri = uri + self.arg_data = arg_data self.buf = b"" self.block = block self.timeout = timeout @@ -34,6 +36,9 @@ class TwitterJSONIter(object): while True: try: utf8_buf = self.buf.decode('utf8').lstrip() + if utf8_buf and utf8_buf[0] != '{': # Remove the hex delimiter length and extra whitespace. + utf8_buf = utf8_buf.lstrip('0123456789abcdefABCDEF') + utf8_buf = utf8_buf.lstrip() res, ptr = self.decoder.raw_decode(utf8_buf) self.buf = utf8_buf[ptr:].encode('utf8') yield wrap_response(res, self.handle.headers) @@ -44,8 +49,8 @@ class TwitterJSONIter(object): pass else: yield None - except urllib_error.HTTPError as e: - raise TwitterHTTPError(e, uri, self.format, arg_data) + except urllib_error.HTTPError as e: # Probably unnecessary, no dynamic url calls in the try block. + raise TwitterHTTPError(e, self.uri, 'json', self.arg_data) # this is a non-blocking read (ie, it will return if any data is available) try: if self.timeout: @@ -57,13 +62,15 @@ class TwitterJSONIter(object): else: yield {"timeout":True} else: - self.buf += sock.recv(1024) + self.buf += sock.recv(2048) except SSLError as e: if (not self.block or self.timeout) and (e.errno == 2): # Apparently this means there was nothing in the socket buf pass else: raise + except urllib_error.HTTPError as e: + raise TwitterHTTPError(e, self.uri, 'json', self.arg_data) def handle_stream_response(req, uri, arg_data, block, timeout=None): handle = urllib_request.urlopen(req,) @@ -119,4 +126,4 @@ class TwitterStream(TwitterStreamCall): TwitterStreamCall.__init__( self, auth=auth, format="json", domain=domain, callable_cls=call_cls, - secure=secure, uriparts=uriparts, timeout=timeout) + secure=secure, uriparts=uriparts, timeout=timeout, gzip=False)