]> jfr.im git - z_archive/twitter.git/blob - twitter/stream_example.py
- Check rate limit using the command line tool. Patch by @stalkr_
[z_archive/twitter.git] / twitter / stream_example.py
1 """
2 Example program for the Stream API. This prints public status messages
3 from the "sample" stream as fast as possible.
4
5 USAGE
6
7 twitter-stream-example <username> <password>
8
9 """
10
11 from __future__ import print_function
12
13 import sys
14
15 from .stream import TwitterStream
16 from .auth import UserPassAuth
17 from .util import printNicely
18
19 def main(args=sys.argv[1:]):
20 if not args[1:]:
21 print(__doc__)
22 return 1
23
24 # When using twitter stream you must authorize. UserPass or OAuth.
25 stream = TwitterStream(auth=UserPassAuth(args[0], args[1]))
26
27 # Iterate over the sample stream.
28 tweet_iter = stream.statuses.sample()
29 for tweet in tweet_iter:
30 # You must test that your tweet has text. It might be a delete
31 # or data message.
32 if tweet.get('text'):
33 printNicely(tweet['text'])