Getting tweet replies to a particular tweet from a particular user
Asked Answered
O

5

17

I am trying to go through tweets of a particular user and get all replies on that tweet. I found that the APIv1.1 of twitter does not directly support it.

Is there a hack or a workaround on getting the replies for a particular tweet. I am using python Streaming API.

Obliteration answered 28/4, 2015 at 19:52 Comment(0)
D
23

There is a workaround using the REST API.

You will need the id_str and @username of the author of the original tweet you want to find replies to.

You should use the Search API for the "@username" of the author. Go through the results looking for the 'in_reply_to_status_id' field to compare to the id_str of the specific tweet you want replies for.

Duenas answered 30/4, 2015 at 13:51 Comment(1)
Can you write the way you would program this?Corrigendum
D
6

Here's a work around to fetch replies of a tweet made by "username" using the rest API using tweepy

1) Find the tweet_id of the tweet for which the replies are required to be fetched

2) Using the api's search method query the following (q="@username", since_id=tweet_id) and retrieve all tweets since tweet_id

3) the results matching the in_reply_to_status_id to tweet_id is the replies for the post.

Dunaway answered 27/7, 2015 at 8:1 Comment(1)
how would you program this?Corrigendum
I
6

I figured out the exact code to fetch replies to a Tweet made by the original author. In addition to fetching the reply, Twitter users mostly reply to the reply to make a thread (which makes it different to fetch the entire thread made by the original author).

Here's a simple recursion that I wrote which solves my problem. This function update the urls list with the URLs of all the replies and replies to the replies of the author.

def update_urls(tweet, api, urls):
    tweet_id = tweet.id
    user_name = tweet.user.screen_name
    max_id = None
    replies = tweepy.Cursor(api.search, q='to:{}'.format(user_name),
                                since_id=tweet_id, max_id=max_id, tweet_mode='extended').items()
    
    for reply in replies:
        if(reply.in_reply_to_status_id == tweet_id):
            urls.append(get_twitter_url(user_name, reply.id))
            try:
                for reply_to_reply in update_urls(reply, api, urls):
                    pass
            except Exception:
                pass
        max_id = reply.id
    return urls

Here are some additional functions that you might need if you plan to use the update_urls function:

def get_api():
    auth=tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_key, access_secret)
    api = tweepy.API(auth, wait_on_rate_limit=True)
    return api

def get_tweet(url):
    tweet_id = url.split('/')[-1]
    api = get_api()
    tweet = api.get_status(tweet_id)
    return tweet

def get_twitter_url(user_name, status_id):
    return "https://twitter.com/" + str(user_name) + "/status/" + str(status_id)

running the exact code:

api = get_api()
tweet = get_tweet(url)
urls = [url]
urls = update_urls(tweet, api, urls)

In case you want to fetch the content for a particular URL, just call get_tweet(url) and use the tweet object to get tweet.text, tweet.user, etc information.

Interventionist answered 6/2, 2020 at 14:41 Comment(0)
W
5
replies=[] 
non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)  
for full_tweets in tweepy.Cursor(api.user_timeline,screen_name=name,timeout=999999).items(10):
  for tweet in tweepy.Cursor(api.search,q='to:'+name,result_type='recent',timeout=999999).items(1000):
    if hasattr(tweet, 'in_reply_to_status_id_str'):
      if (tweet.in_reply_to_status_id_str==full_tweets.id_str):
        replies.append(tweet.text)
  print("Tweet :",full_tweets.text.translate(non_bmp_map))
  for elements in replies:
       print("Replies :",elements)
  replies.clear()

The above code will fetch 10 recent tweets of an user(name) along with the replies to that particular tweet.The replies will be saved on to a list named replies. You can retrieve more tweets by increasing the items count (eg:items(100)).

Withershins answered 15/3, 2018 at 13:6 Comment(0)
P
1

Following function uses username and tweet_id to return list of all the text of the replies to that particular tweet_id: (I assume that api has already been declared in the program.)

def get_tweet_thread(username,tweet_id):
    replies = tweepy.Cursor(api.search, q='to:{}'.format(username),since_id=tweet_id, tweet_mode='extended').items()

    replied_thread = list()
    for reply in replies:
        if(reply._json['in_reply_to_status_id'] == tweet_id):
             replied_thread.append(reply._json['full_text'])
        
    return(replied_thread)
Pignut answered 7/6, 2021 at 12:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.