Last active
December 20, 2015 17:59
-
-
Save dbonadiman/6172952 to your computer and use it in GitHub Desktop.
a simple way to access the twitter stream
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import sys | |
| import json | |
| import tweepy | |
| ######## | |
| # Tweepy data listener | |
| ######## | |
| class StdOutListener(tweepy.StreamListener): | |
| def do_stuff(self, pdata): | |
| print pdata['text'] | |
| def on_data(self, data): | |
| pdata = "" | |
| try: | |
| pdata = json.loads(data) | |
| self.do_stuff(pdata) | |
| return True | |
| except Exception as e: | |
| return True | |
| def on_error(self, status): | |
| print "Stream on_error: ", status | |
| ####### | |
| # Init | |
| ####### | |
| if __name__ == '__main__': | |
| try: | |
| # Authenticate | |
| auth = tweepy.OAuthHandler('Consumer key','Consumer secret') | |
| auth.set_access_token('Access token','Access token secret') | |
| # Read stream | |
| stream = tweepy.Stream(auth, StdOutListener()) | |
| #stream.filter(track=['word']) | |
| stream.sample() | |
| except Exception as e: | |
| print 'could not connect to Twitter' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment