Search code examples
pythontwittertweepy

How to reply_count & quote_count using tweepy 3.10.0?


I am trying to execute quote_count & reply_count using the Twitter Tweepy API, but I can't find proper updated documentation on how to do it.

https://developer.twitter.com/en/docs/twitter-api/metrics

I have some working code from Tweepy for Twitter API version 1 to get some data I use, but I cant find good info about how to extract reply_count & quote_count using Twitter API version 2 via Tweepy.

import tweepy

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, proxy=proxy)

public_tweets = api.user_timeline()

Solution

  • Tweepy v3.10.0 does not support Twitter API v2. You'll have to use the latest development version of Tweepy on the master branch or wait for Tweepy v4.0 to be released.

    As that documentation says, you need to pass the specific fields and expansions you want when making the API request. For example, for the version currently on the master branch, the equivalent of the public metrics example request in that documentation would be:

    response = client.get_tweets(
        ids=[1204084171334832128],
        tweet_fields=["public_metrics"],
        expansions=["attachments.media_keys"],
        media_fields=["public_metrics"]
    )
    

    Then, you can access them through the attributes of the Tweet and Media objects:

    >>> response
    Response(data=[<Tweet id=1204084171334832128 text=Tired of reading? We’ve got you covered. Learn about the capabilities of the Account Activity API in this video walkthrough with @tonyv00 from our DevRel team. 🍿 ⬇️ [. . .] >], includes={'media': [<Media media_key=13_1204080851740315648 type=video>]}, errors=[], meta={})
    >>> response.data[0].public_metrics
    {'retweet_count': 9, 'reply_count': 2, 'like_count': 52, 'quote_count': 2}
    >>> response.includes["media"][0].public_metrics
    {'view_count': 1946}