]>
jfr.im git - z_archive/twitter.git/blob - twitter/stream.py
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 Interface to the Twitter Stream API (stream.twitter.com). This can
40 be used pretty much the same as the Twitter class except the
41 result of calling a method will be an iterator that yields objects
42 decoded from the stream. For example::
44 twitter_stream = TwitterStream(auth=UserPassAuth('joe', 'joespassword'))
45 iterator = twitter_stream.statuses.sample()
47 for tweet in iterator:
48 ...do something with this tweet...
50 The iterator will yield tweets forever and ever (until the stream
51 breaks at which point it raises a TwitterHTTPError.)
54 self
, domain
="stream.twitter.com", secure
=False, auth
=None,
57 uriparts
+= (str(api_version
),)
59 TwitterStreamCall
.__init
__(
60 self
, auth
=auth
, format
="json", domain
=domain
,
61 callable_cls
=TwitterStreamCall
,
62 secure
=secure
, uriparts
=uriparts
)