0

I'm trying to get all the possible information from twitter filtered by hashtags or any research criteria. For me this is a complete new activity. I red something online about this and in the end I've got something like:

from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
 
import twitter_credentials # another file with the credentials 
 



# # # # TWITTER STREAMER # # # #
class TwitterStreamer():
    """
    Class for streaming and processing live tweets.
    """
    def __init__(self):
        pass

    def stream_tweets(self, fetched_tweets_filename, hash_tag_list):
        listener = StdOutListener(fetched_tweets_filename)
        auth = OAuthHandler(twitter_credentials.CONSUMER_KEY, twitter_credentials.CONSUMER_SECRET)
        auth.set_access_token(twitter_credentials.ACCESS_TOKEN, twitter_credentials.ACCESS_TOKEN_SECRET)
        stream = Stream(auth, listener)

        
        stream.filter(track=hash_tag_list)


# # # # TWITTER STREAM LISTENER # # # #
class StdOutListener(StreamListener):
    def __init__(self, fetched_tweets_filename):
        self.fetched_tweets_filename = fetched_tweets_filename

    def on_data(self, data):
        try:
            print(data)
            with open(self.fetched_tweets_filename, 'a') as tf:
                tf.write(data)
            return True
        except BaseException as e:
            print("Error on_data %s" % str(e))
        return True
          

    def on_error(self, status):
        if status == 420:
            return False
        print(status)

 
if __name__ == '__main__':
 
    hash_tag_list = ["omg"]# here is the filter to use
    fetched_tweets_filename = "tweets.json"

    twitter_streamer = TwitterStreamer()
    twitter_streamer.stream_tweets(fetched_tweets_filename, hash_tag_list)

Probably I did not understand at 100% this type of code because it works perfectly and it give me back all the information that I need but it gives me only the data related to the day I run it. I would need instead also the data of the previous 2/3 weeks. Can someone help me with this my little issue? Thx a lot

Pietro
  • 72
  • 4
  • 1
    Looking at this code, I'm guessing it's not going to get past tweets because it listens for and retrieves tweets in real time. – Trenton McKinney Aug 26 '20 at 18:46
  • You probably want to look for other questions that download past tweets. [python parse data from twitter with tweepy](https://www.google.com/search?q=python+parse+data+from+twitter+with+tweepy+site:stackoverflow.com&sxsrf=ALeKk02MdnbdU2XAGArblwe-V9sppjSuVA:1598467624157&sa=X&ved=2ahUKEwjowbD3w7nrAhV3HzQIHV4xCSoQrQIoBHoECA8QBQ&biw=1920&bih=975) – Trenton McKinney Aug 26 '20 at 18:48
  • 1
    Does this answer your question? [How to extract data from a Tweepy object into a pandas dataframe?](https://stackoverflow.com/questions/58666135/how-to-extract-data-from-a-tweepy-object-into-a-pandas-dataframe) – Trenton McKinney Aug 26 '20 at 18:50
  • Does this answer your question? [tweepy get tweets between two dates](https://stackoverflow.com/questions/49731259) – Trenton McKinney Aug 26 '20 at 18:51

0 Answers0