GOAL
Use Tweepy to to call the Twitter API to return the public_metrics
(likes
, retweets
, quotes
, replies
) for each of multiple tweet_id
s.
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