I have been learning Python for about a month now and after watching several tutorials, I decided to give Tweepy a go, to analyze the extracted data. The thing though, is that Tweepy will not stop streaming tweets, no matter where I place the if statement. I am using Python 3.9 and Tweepy 3.10.
For instance, I would like it to stop once 10 tweets have been stored in the CSV.
Any thoughts?
# Stream Listener Class
class MyListener(tweepy.StreamListener):
def on_data(self, raw_data):
self.num_tweets = 0
self.file_name = 'path/tweet_stream.csv'
self.process_data(raw_data)
return True
def process_data(self, raw_data):
print(raw_data)
with open(self.file_name, mode='a') as f:
writer = csv.writer(f)
writer.writerow([raw_data, '\n'])
self.num_tweets += 1
if self.num_tweets < 10:
return True
else:
return False
# Creating the Stream
class MyStream():
def __init__(self, auth, listener):
self.stream = tweepy.Stream(auth=auth, listener=listener)
def start(self, keywords):
self.stream.filter(track=keywords)
# Starting
if __name__ == "__main__":
listener = MyListener()