]> jfr.im git - z_archive/twitter.git/blob - twitter/stream.py
Improve documentation.
[z_archive/twitter.git] / twitter / stream.py
1 try:
2 import urllib.request as urllib_request
3 import urllib.error as urllib_error
4 import io
5 except ImportError:
6 import urllib2 as urllib_request
7 import urllib2 as urllib_error
8 import json
9
10 from .api import TwitterCall, wrap_response
11
12 class TwitterJSONIter(object):
13
14 def __init__(self, handle, uri, arg_data):
15 self.decoder = json.JSONDecoder()
16 self.handle = handle
17 self.buf = b""
18
19 def __iter__(self):
20 while True:
21 self.buf += self.handle.read(1024)
22 try:
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:
28 continue
29 except urllib_error.HTTPError as e:
30 raise TwitterHTTPError(e, uri, self.format, arg_data)
31
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))
36
37 class TwitterStream(TwitterStreamCall):
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 """
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)