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 json | |
import tweepy | |
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: | |
return True | |
def on_error(self, status): | |
print "Stream on_error: ", status | |
def connect(): | |
# Authenticate | |
auth = tweepy.OAuthHandler('Consumer key', 'Consumer secret') | |
auth.set_access_token('Access token', 'Access token secret') | |
return auth | |
def stream(): | |
try: | |
# Read stream | |
stream = tweepy.Stream(connect(), StdOutListener()) | |
#stream.filter(track=['word']) | |
stream.sample() | |
except Exception: | |
print 'could not connect to Twitter' | |
if __name__ == '__main__': | |
stream() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment