]> jfr.im git - z_archive/twitter.git/blame - twitter/stream.py
Improve documentation.
[z_archive/twitter.git] / twitter / stream.py
CommitLineData
dd648a25
MV
1try:
2 import urllib.request as urllib_request
3 import urllib.error as urllib_error
b8fd1206 4 import io
dd648a25
MV
5except ImportError:
6 import urllib2 as urllib_request
7 import urllib2 as urllib_error
8import json
9
10from .api import TwitterCall, wrap_response
11
12class TwitterJSONIter(object):
13
14 def __init__(self, handle, uri, arg_data):
15 self.decoder = json.JSONDecoder()
16 self.handle = handle
b8fd1206 17 self.buf = b""
dd648a25
MV
18
19 def __iter__(self):
20 while True:
640e695c 21 self.buf += self.handle.read(1024)
dd648a25 22 try:
b8fd1206
MV
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')
dd648a25
MV
26 yield wrap_response(res, self.handle.headers)
27 except ValueError as e:
28 continue
29 except urllib_error.HTTPError as e:
30 raise TwitterHTTPError(e, uri, self.format, arg_data)
31
32class 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))
36
37class TwitterStream(TwitterStreamCall):
24950891
MV
38 """
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::
43
44 twitter_stream = TwitterStream(auth=UserPassAuth('joe', 'joespassword'))
45 iterator = twitter_stream.statuses.sample()
46
47 for tweet in iterator:
48 ...do something with this tweet...
49
50 The iterator will yield tweets forever and ever (until the stream
51 breaks at which point it raises a TwitterHTTPError.)
52 """
dd648a25
MV
53 def __init__(
54 self, domain="stream.twitter.com", secure=False, auth=None,
55 api_version='1'):
56 uriparts = ()
57 uriparts += (str(api_version),)
58
59 TwitterStreamCall.__init__(
60 self, auth=auth, format="json", domain=domain,
61 callable_cls=TwitterStreamCall,
62 secure=secure, uriparts=uriparts)