This shows you the differences between two versions of the page.
— |
twitter-streaming-py [2017/01/12 14:01] (current) |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Twitter Streaming Script ====== | ||
+ | |||
+ | <code python> | ||
+ | #Import the necessary methods from tweepy library | ||
+ | from tweepy.streaming import StreamListener | ||
+ | from tweepy import OAuthHandler | ||
+ | from tweepy import Stream | ||
+ | |||
+ | #Variables that contains the user credentials to access Twitter API | ||
+ | access_token = "xxxx" | ||
+ | access_token_secret = "yyyy" | ||
+ | consumer_key = "zzzz" | ||
+ | consumer_secret = "wwww" | ||
+ | |||
+ | |||
+ | #This is a basic listener that just prints received tweets to stdout. | ||
+ | class StdOutListener(StreamListener): | ||
+ | |||
+ | def on_data(self, data): | ||
+ | print data | ||
+ | return True | ||
+ | |||
+ | def on_error(self, status): | ||
+ | print status | ||
+ | |||
+ | |||
+ | if __name__ == '__main__': | ||
+ | |||
+ | #This handles Twitter authetification and the connection to Twitter Streaming API | ||
+ | l = StdOutListener() | ||
+ | auth = OAuthHandler(consumer_key, consumer_secret) | ||
+ | auth.set_access_token(access_token, access_token_secret) | ||
+ | stream = Stream(auth, l) | ||
+ | |||
+ | #This line filter Twitter Streams to capture data by the keywords: 'python', 'javascript', 'ruby' | ||
+ | stream.filter(track=['architecture','#architecture','Architecture','ARCHITECTURE']) | ||
+ | </code> | ||