Search code examples
pythontweepytwitter-api-v2

How to query Twitter API public metrics for multiple `tweet_id`s using Tweepy


GOAL

Use Tweepy to to call the Twitter API to return the public_metrics (likes, retweets, quotes, replies) for each of multiple tweet_ids.

SOLUTION

client = tweepy.Client(bearer_token, wait_on_rate_limit=True)

gathered_tweets = []
for response in tweepy.Paginator(client.get_tweets,
                                 query = '1537245238095323136, 1536973965394157569',
                                 tweet_fields = ['public_metrics'],
                                 max_results=100):
    time.sleep(1)
    gathered_tweets.append(response)



result = []
user_dict = {}
# Loop through each response object
for response in gathered_tweets:
  # Take all of the users, and put them into a dictionary of dictionaries with the info we want to keep
  for user in response.includes['users']:
      user_dict[user.id] = {'username': user.username,
                            'created_at': user.created_at
                            }
  for tweet in response.data:
      # For each tweet, find the author's information
      author_info = user_dict[tweet.author_id]
      # Put all of the information we want to keep in a single dictionary for each tweet
      result.append({'author_id': tweet.author_id,
                   'tweet_id': tweet.id,
                   'retweets': tweet.public_metrics['retweet_count'],
                   'replies': tweet.public_metrics['reply_count'],
                   'likes': tweet.public_metrics['like_count'],
                   'quotes': tweet.public_metrics['quote_count']
                   })

ERROR MESSAGE

TypeError                                 Traceback (most recent call last)
<ipython-input-31-a0842c496bea> in <module>()
      9                                  query = '1537245238095323136,1536973965394157569',
     10                                  tweet_fields = ['public_metrics'],
---> 11                                  max_results=100):
     12     time.sleep(1)
     13     gathered_tweets.append(response)

/usr/local/lib/python3.7/dist-packages/tweepy/pagination.py in __next__(self)
     96             self.kwargs["pagination_token"] = pagination_token
     97 
---> 98         response = self.method(*self.args, **self.kwargs)
     99 
    100         self.previous_token = response.meta.get("previous_token")

TypeError: get_tweets() missing 1 required positional argument: 'ids'

ASSESSMENT

I get that I should be stating the ids differently, but not clear how.

I'm reading the get_tweets documentation here: https://docs.tweepy.org/en/stable/client.html#tweet-lookup

But it's not clear on the argument syntax.

The documentation states: " A comma separated list of Tweet IDs. Up to 100 are allowed in a single request. Make sure to not include a space between commas and fields."

I believe my syntax is in line with that, so I'm not getting what the error message means.

And guidance on this greatly appreciated.

Thx Doug


Solution

  • As for your original error, the error message clearly told you TypeError: get_tweets() missing 1 required positional argument: 'ids', and that line showed that ids was not passed. In the comments we resolved that to know that this was a minor typo as the query argument (in the line query = '1537245238095323136, 1536973965394157569',) should have been renamed to ids.

    Now for the subsequent error, max_results not working with get_tweets but working with search_recent_tweets can also be traced to the same issue. Reading through the documentation for get_tweets (source code) clearly shows that it does not accept a max_results argument, while search_recent_tweets (source code) does. You may have thought and make an assumption that the tweetpy.Paginator (source code) actually provide the support for max_results argument but again, reading both the documentation and the code clearly shows that is not the case. You can read further and then find that there is a flatten method (source code) that actually will provide way to the limit the results returned.

    Your issues can be resolved by careful reading of the documentation and sometimes the code of the underlying libraries you are using, and that will typically save you a lot more time than waiting around for an answer. Never make any assumptions about what the library does, it's often counterproductive to pass random things to a function when there is no knowledge of what arguments it accepts.