Skip to content

Instantly share code, notes, and snippets.

@mtrovo
Created July 29, 2011 01:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtrovo/1112928 to your computer and use it in GitHub Desktop.
Save mtrovo/1112928 to your computer and use it in GitHub Desktop.
Twitter Streaming API sample using the filter stream
#!/usr/bin/env python
#-*- coding:utf-8 -*-
from tweepy.streaming import StreamListener, Stream
from tweepy.auth import BasicAuthHandler
from tweepy.api import API
import tweepy
class MyStreamListener(StreamListener):
def __init__(self, api=None):
StreamListener.__init__(self, api=api)
def on_status(self, status):
print "@%s: %s" % (status.user.screen_name, status.text)
def on_error(self, status_code):
print "ERROR: ",status_code
def on_limit(self, track):
print "LIMIT: ", track
def on_timeout(self):
print "TIMEOUT!"
def filter_track():
track=["python"]
stream_auth = BasicAuthHandler('<USERNAME>', '<PASSWORD>')
api = API()
stream = Stream(stream_auth, MyStreamListener(api))
print 'start filter track ', ','.join(track)
stream.filter(track=track)
if __name__ == "__main__":
filter_track()
@tepie
Copy link

tepie commented Apr 28, 2012

good reference, i was trying to pass the oauth rather than the basic auth, thanks

@billstar
Copy link

Twitter's streaming API concepts doc says: "Basic Auth will eventually be deprecated on the Streaming API, but we have not planned or announced a timetable for this transition." I hope that AppEngine gets their http connection stuff straightened out before then.

@mtrovo
Copy link
Author

mtrovo commented Jul 20, 2012

@bstrathearn i saw this too, actually when I created this gist they already had this warning. I hope they don't deprecate it so soon, the last time I checked tweepy it still didn't support Streaming API login with OAuth.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment