]> jfr.im git - z_archive/twitter.git/blame - twitter/stream_example.py
Add better formatting to status output.
[z_archive/twitter.git] / twitter / stream_example.py
CommitLineData
640e695c
MV
1"""
2Example program for the Stream API. This prints public status messages
3from the "sample" stream as fast as possible.
4
5USAGE
6
7 twitter-stream-example <username> <password>
8
9"""
10
11from __future__ import print_function
12
13import sys
14
15from .stream import TwitterStream
16from .auth import UserPassAuth
17from .util import printNicely
18
19def 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'])