]>
Commit | Line | Data |
---|---|---|
640e695c MV |
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 | ||
95d49800 | 7 | stream-example -t <token> -ts <token_secret> -ck <consumer_key> -cs <consumer_secret> |
640e695c MV |
8 | |
9 | """ | |
10 | ||
11 | from __future__ import print_function | |
12 | ||
95d49800 | 13 | import argparse |
640e695c | 14 | |
d3915c61 | 15 | from twitter.stream import TwitterStream, Timeout |
95d49800 AD |
16 | from twitter.oauth import OAuth |
17 | from twitter.util import printNicely | |
640e695c | 18 | |
640e695c | 19 | |
95d49800 AD |
20 | def parse_arguments(): |
21 | ||
22 | parser = argparse.ArgumentParser() | |
23 | ||
24 | parser.add_argument('-t', '--token', help='The Twitter Access Token.') | |
d3915c61 MV |
25 | parser.add_argument('-ts', '--token-secret', help='The Twitter Access Token Secret.') |
26 | parser.add_argument('-ck', '--consumer-key', help='The Twitter Consumer Key.') | |
27 | parser.add_argument('-cs', '--consumer-secret', help='The Twitter Consumer Secret.') | |
28 | parser.add_argument('-us', '--user-stream', action='store_true', help='Connect to the user stream endpoint.') | |
29 | parser.add_argument('-ss', '--site-stream', action='store_true', help='Connect to the site stream endpoint.') | |
30 | parser.add_argument('-to', '--timeout', help='Timeout for the stream (seconds)') | |
31 | parser.add_argument('-nb', '--no-block', action='store_true', help='Set stream to non-blocking') | |
95d49800 AD |
32 | return parser.parse_args() |
33 | ||
95d49800 | 34 | def main(): |
95d49800 AD |
35 | args = parse_arguments() |
36 | ||
12f26468 MV |
37 | if not all((args.token, args.token_secret, args.consumer_key, args.consumer_secret)): |
38 | print(__doc__) | |
39 | return 2 | |
40 | ||
95d49800 | 41 | # When using twitter stream you must authorize. |
7333aa5a AD |
42 | auth = OAuth(args.token, args.token_secret, args.consumer_key, args.consumer_secret) |
43 | if args.user_stream: | |
d3915c61 | 44 | stream = TwitterStream(auth=auth, domain='userstream.twitter.com', timeout=args.timeout, block=not args.no_block) |
7333aa5a AD |
45 | tweet_iter = stream.user() |
46 | elif args.site_stream: | |
47 | stream = TwitterStream(auth=auth, domain='sitestream.twitter.com') | |
48 | tweet_iter = stream.site() | |
49 | else: | |
967c2f68 | 50 | stream = TwitterStream(auth=auth) |
7333aa5a | 51 | tweet_iter = stream.statuses.sample() |
640e695c MV |
52 | |
53 | # Iterate over the sample stream. | |
640e695c MV |
54 | for tweet in tweet_iter: |
55 | # You must test that your tweet has text. It might be a delete | |
56 | # or data message. | |
d3915c61 MV |
57 | if tweet is None: |
58 | printNicely("-- None --") | |
59 | elif tweet is Timeout: | |
60 | printNicely("-- Timeout --") | |
61 | elif tweet.get('text'): | |
640e695c | 62 | printNicely(tweet['text']) |
95d49800 | 63 | |
95d49800 AD |
64 | if __name__ == '__main__': |
65 | main() |