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.