pythonpython-twittertwitter-rest-api

python twitter api returns inconsistent number of tweets each second


I'm trying to write my own twitter-fetch api, based on the python-twitter library. The goal is to fetch the last 5 tweets of Donald Trump. I'm getting a different number of tweets every other second or so. Sometimes it's 5 tweets, sometimes it's only 1.

import twitter

api = twitter.Api(consumer_key=config["credentials"]["key"],
                  consumer_secret=config["credentials"]["secret"],
                  application_only_auth=True,
                  tweet_mode='extended')

tweets = api.GetUserTimeline(
            screen_name="realDonaldTrump",
            count=5)

print("number of tweets: {}".format(len(tweets)))

Has anyone noticed this before with the twitter REST api using another language/package?

Has anyone noticed this before with the python-twitter package that I'm using?


Solution

  • This is normal behavior. See count in this doc: https://developer.twitter.com/en/docs/tweets/timelines/api-reference/get-statuses-user_timeline.html

    count specifies a max limit rather than a promise to deliver a specified number of tweets. From the 5 tweets that you request, Twitter may cull some for lack of relevance, etc.

    For your scenario, simply request many more tweets than 5 (ex. 100) and use only the first 5.