]>
Commit | Line | Data |
---|---|---|
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']) |