]>
Commit | Line | Data |
---|---|---|
640e695c MV |
1 | """ |
2 | Example program for the Stream API. This prints public status messages | |
b8e88ed9 | 3 | from the "sample" stream as fast as possible. Use -h for help. |
640e695c MV |
4 | """ |
5 | ||
6 | from __future__ import print_function | |
7 | ||
95d49800 | 8 | import argparse |
640e695c | 9 | |
03d8511c | 10 | from twitter.stream import TwitterStream, Timeout, HeartbeatTimeout, Hangup |
95d49800 | 11 | from twitter.oauth import OAuth |
c2176d4e | 12 | from twitter.oauth2 import OAuth2, read_bearer_token_file |
95d49800 | 13 | from twitter.util import printNicely |
640e695c | 14 | |
95d49800 AD |
15 | def parse_arguments(): |
16 | ||
b8e88ed9 | 17 | parser = argparse.ArgumentParser(description=__doc__ or "") |
95d49800 | 18 | |
b8e88ed9 MV |
19 | parser.add_argument('-t', '--token', required=True, help='The Twitter Access Token.') |
20 | parser.add_argument('-ts', '--token-secret', required=True, help='The Twitter Access Token Secret.') | |
21 | parser.add_argument('-ck', '--consumer-key', required=True, help='The Twitter Consumer Key.') | |
22 | parser.add_argument('-cs', '--consumer-secret', required=True, help='The Twitter Consumer Secret.') | |
d3915c61 MV |
23 | parser.add_argument('-us', '--user-stream', action='store_true', help='Connect to the user stream endpoint.') |
24 | parser.add_argument('-ss', '--site-stream', action='store_true', help='Connect to the site stream endpoint.') | |
03d8511c MV |
25 | parser.add_argument('-to', '--timeout', help='Timeout for the stream (seconds).') |
26 | parser.add_argument('-ht', '--heartbeat-timeout', help='Set heartbeat timeout.', default=90) | |
27 | parser.add_argument('-nb', '--no-block', action='store_true', help='Set stream to non-blocking.') | |
28 | parser.add_argument('-tt', '--track-keywords', help='Search the stream for specific text.') | |
95d49800 AD |
29 | return parser.parse_args() |
30 | ||
95d49800 | 31 | def main(): |
95d49800 AD |
32 | args = parse_arguments() |
33 | ||
34 | # When using twitter stream you must authorize. | |
7333aa5a | 35 | auth = OAuth(args.token, args.token_secret, args.consumer_key, args.consumer_secret) |
03d8511c MV |
36 | |
37 | # These arguments are optional: | |
38 | stream_args = dict( | |
39 | timeout=args.timeout, | |
40 | block=not args.no_block, | |
41 | heartbeat_timeout=args.heartbeat_timeout) | |
42 | ||
bb32b71b R |
43 | query_args = dict() |
44 | if args.track_keywords: | |
45 | query_args['track'] = args.track_keywords | |
46 | ||
7333aa5a | 47 | if args.user_stream: |
03d8511c | 48 | stream = TwitterStream(auth=auth, domain='userstream.twitter.com', **stream_args) |
bb32b71b | 49 | tweet_iter = stream.user(**query_args) |
7333aa5a | 50 | elif args.site_stream: |
03d8511c | 51 | stream = TwitterStream(auth=auth, domain='sitestream.twitter.com', **stream_args) |
bb32b71b | 52 | tweet_iter = stream.site(**query_args) |
7333aa5a | 53 | else: |
03d8511c | 54 | stream = TwitterStream(auth=auth, **stream_args) |
b8e88ed9 | 55 | if args.track_keywords: |
bb32b71b | 56 | tweet_iter = stream.statuses.filter(**query_args) |
42b9cdee MV |
57 | else: |
58 | tweet_iter = stream.statuses.sample() | |
640e695c MV |
59 | |
60 | # Iterate over the sample stream. | |
640e695c MV |
61 | for tweet in tweet_iter: |
62 | # You must test that your tweet has text. It might be a delete | |
63 | # or data message. | |
d3915c61 MV |
64 | if tweet is None: |
65 | printNicely("-- None --") | |
66 | elif tweet is Timeout: | |
67 | printNicely("-- Timeout --") | |
03d8511c MV |
68 | elif tweet is HeartbeatTimeout: |
69 | printNicely("-- Heartbeat Timeout --") | |
70 | elif tweet is Hangup: | |
71 | printNicely("-- Hangup --") | |
d3915c61 | 72 | elif tweet.get('text'): |
640e695c | 73 | printNicely(tweet['text']) |
03d8511c MV |
74 | else: |
75 | printNicely("-- Some data: " + str(tweet)) | |
95d49800 | 76 | |
95d49800 AD |
77 | if __name__ == '__main__': |
78 | main() |