I\'m looking to have the Tweepy Streaming API stop pulling in tweets after I have stored x # of tweets in MongoDB.
I have tried IF and WHILE statements inside the c
You need to add a counter inside of your class in __init__
, and then increment it inside of on_status
. Then when the counter is below 20 it will insert a record into the collection. This could be done as show below:
def __init__(self, api=None):
super(StdOutListener, self).__init__()
self.num_tweets = 0
def on_status(self, status):
record = {'Text': status.text, 'Created At': status.created_at}
print record #See Tweepy documentation to learn how to access other fields
self.num_tweets += 1
if self.num_tweets < 20:
collection.insert(record)
return True
else:
return False