Skip to content

Instantly share code, notes, and snippets.

@dbonadiman
Last active December 20, 2015 17:59
Show Gist options
  • Save dbonadiman/6172952 to your computer and use it in GitHub Desktop.
Save dbonadiman/6172952 to your computer and use it in GitHub Desktop.
a simple way to access the twitter stream
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