]>
jfr.im git - z_archive/twitter.git/blob - twitter/stream.py
2c609187f7cda41b6f9870e65088df10e478a055
2 import urllib
.request
as urllib_request
3 import urllib
.error
as urllib_error
6 import urllib2
as urllib_request
7 import urllib2
as urllib_error
10 from .api
import TwitterCall
, wrap_response
12 class TwitterJSONIter(object):
14 def __init__(self
, handle
, uri
, arg_data
):
15 self
.decoder
= json
.JSONDecoder()
21 self
.buf
+= self
.handle
.read(1024)
23 utf8_buf
= self
.buf
.decode('utf8').lstrip()
24 res
, ptr
= self
.decoder
.raw_decode(utf8_buf
)
25 self
.buf
= utf8_buf
[ptr
:].encode('utf8')
26 yield wrap_response(res
, self
.handle
.headers
)
27 except ValueError as e
:
29 except urllib_error
.HTTPError
as e
:
30 raise TwitterHTTPError(e
, uri
, self
.format
, arg_data
)
32 class TwitterStreamCall(TwitterCall
):
33 def _handle_response(self
, req
, uri
, arg_data
):
34 handle
= urllib_request
.urlopen(req
,)
35 return iter(TwitterJSONIter(handle
, uri
, arg_data
))
37 class TwitterStream(TwitterStreamCall
):
39 self
, domain
="stream.twitter.com", secure
=False, auth
=None,
42 uriparts
+= (str(api_version
),)
44 TwitterStreamCall
.__init
__(
45 self
, auth
=auth
, format
="json", domain
=domain
,
46 callable_cls
=TwitterStreamCall
,
47 secure
=secure
, uriparts
=uriparts
)