The Stream API provides access to tweets in real-time. Streams can be filtered based on keywords, language, location, and more. Here's a simple example to track mentions of the word "tweepy":
#set up a new class using tweepy.StreamListener
class SimpleListener(tweepy.StreamListener):
def on_status(self, status):
#code to run each time the stream receives a status
print(status.text)
def on_direct_message(self, status):
#code to run each time the stream receives a direct message
print(status.text)
def on_data(self, status):
#code to run each time you receive some data (direct message, delete, profile update, status,...)
print(status.text)
def on_error(self, staus_code):
#code to run each time an error is received
if status_code == 420:
return False
else:
return True
#initialize the stream
tweepy_listener = SimpleListener()
tweepy_stream = tweepy.Stream(auth = api.auth, listener=tweepy_listener())
tweepy_stream.filter(track=['tweepy'])
You can track different keywords by changing the track
parameter.
<to add: examples of filtering based on locations, languages, etc.>
You can track data adressed to your account by using userstream()
instead of filter.
api.userstream(async=True)