deltaKshatriya
deltaKshatriya

Reputation: 101

Tweepy Get Tweets in reply to a particular tweet

So I've been doing a lot of work with Tweepy and Twitter data mining, and one of the things I want to do is to be able to get all Tweets that are replies to a particular Tweet. I've seen the Search api, but I'm not sure how to use it nor how to search specifically for Tweets in reply to a specific Tweet. Anyone have any ideas? Thanks all.

Upvotes: 2

Views: 3873

Answers (2)

Malik Faiq
Malik Faiq

Reputation: 442

user_name = "@nameofuser"

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

while True:
    try:

        reply = replies.next()
        if not hasattr(reply, 'in_reply_to_status_id_str'):
            continue
        if str(reply.in_reply_to_status_id) == tweet_id:
           logging.info("reply of tweet:{}".format(reply.text))

    except tweepy.RateLimitError as e:
        logging.error("Twitter api rate limit reached".format(e))
        time.sleep(60)
        continue

    except tweepy.TweepError as e:
        logging.error("Tweepy error occured:{}".format(e))
        break

    except StopIteration:
        break

    except Exception as e:
        logger.error("Failed while fetching replies {}".format(e))
        break

Upvotes: 1

deltaKshatriya
deltaKshatriya

Reputation: 101

I've created a workaround that kind of works. The best way to do it is to search for mentions of a user, then filter those mentions by in_reply_to_id .

Upvotes: 0

Related Questions