6

I am using the Python tweepy library.

I was successfully able to extract the 'liked' and 're-tweet' counts of a tweet using the below code:

# Get count of handles who are following you
def get_followers_count(handle):
    user = api.get_user(handle)
    return user.followers_count

# Get count of handles that you are following
def get_friends_count(handle):
    user = api.get_user(handle)
    return user.friends_count

# Get count of tweets for a handle
def get_status_count(handle):
    user = api.get_user(handle)
    return user.statuses_count

# Get count of tweets liked by user
def get_favourite_count(handle):
    user = api.get_user(handle)
    return user.favourits_count

However, I couldn't find a way to get the reply counts of a particular tweet.

Is it possible to get reply count of a tweet using tweepy or any other library like twython or even twitter4j?

Michael0x2a
  • 58,192
  • 30
  • 175
  • 224
Prakash P
  • 3,582
  • 4
  • 38
  • 66

1 Answers1

2

The example code below shows how you could implement a solution to finding all replies for a single tweet. It utilizes the twitter search operator to:<account> and grabs all tweets that have replies to that account. By using since_id=tweet_id, the tweets returned from api.search are restricted to those created after the time the post was created. After these tweets are obtained, the in_reply_to_status_id attribute is used to check if the tweet captured is a reply to the tweet of interest.

auth = tweepy.OAuthHandler(API_KEY, API_SECRET_KEY)
api = tweepy.API(auth)

user = 'MollyNagle3'
tweet_id = 1368278040300650497
t = api.search(q=f'to:{user}', since_id=tweet_id,)

replies = 0
for i in range(len(t)):

    if t[i].in_reply_to_status_id == tweet_id:
        replies += 1
print(replies)

The limitation of this code is that is inefficient. It grabs more tweets than necessary. However, it seems that it is the best way to do this. Also, if you want to get replies of a very old tweet, you can implement the parameter max_id in api.search to limit the length of time you search over for replies.