简体   繁体   中英

Why is there a delay when streaming tweets live?

I am trying to get live tweet data via stream with tweepy from a specific user, however I am finding there is exactly a 4 second delay from the exact timestamp the tweet is posted and the timestamp of the printed text from my tweepy program. Is this normal/expected or is there a way I can make my code more efficient? Thanks!

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


    def stream_tweets(self):
        # This handles Twitter authetification and the connection to Twitter Streaming API
        listener = TweetListener()
        auth = OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
        auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
        stream = Stream(auth, listener)

        # This line filter Twitter Streams to capture data by the keywords: 
        stream.filter(follow=['user_id'])


# # # # TWITTER STREAM LISTENER # # # #
class TweetListener(StreamListener):
    
    #This is a basic listener that just prints received tweets 
    
     #Only returns the tweets of given user
    def on_status(self, status):
        if status.user.id_str != 'user_id':
            return
        print(status.text)

    def on_data(self, data):
        try:
            json_load = json.loads(data) 
            text = json_load['text']
            if 'RT @' not in text:
                print(text)
                print(datetime.now()) 
            return True
        except BaseException as e:
            print("Error on_data %s" % str(e))
        return True
          

    def on_error(self, status):
        print(status)

if __name__ == '__main__':
    
    streamer=TwitterStreamer()
    streamer.stream_tweets()

That's more-or-less going to be true, yes. Latency depends on a number of things like network connection and location, but generally I'd expect a small delay of a few seconds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM