Skip to content

Instantly share code, notes, and snippets.

@wesslen
Last active March 1, 2021 20:30
Show Gist options
  • Select an option

  • Save wesslen/963fc122745b856ea73c84475c725138 to your computer and use it in GitHub Desktop.

Select an option

Save wesslen/963fc122745b856ea73c84475c725138 to your computer and use it in GitHub Desktop.
get replies for a specific tweet using rtweet
## issues:
## -only gets replies within last ~7 days to the post due to public REST API limits
## -counts don't necessarily align with total replies via browser, perhaps due to private accounts (?)
get_replies <- function(tweetid){
# get status information for given tweet
t <- rtweet::lookup_statuses(statuses = tweetid, token = ryan_rtweets)
# use search API to find all tweets directed to the poster
# and keep only replied to that status
z <- rtweet::search_tweets(q = paste0("to:",t$screen_name),
sinceID = tweetid,
n = 3200,
retryonratelimit = TRUE,
token = ryan_rtweets)
if(nrow(z)!=0){
# keep only replies to tweetid status
z <- subset(z, reply_to_status_id == tweetid)
# combine original tweet and replies (created option to include or not original tweets)
# z <- rbind(t, z)
# return dataframe
return(z)
} else {
# save users that returned no replies
# prevent running first two steps if see that users has no replies in RESTapi
}
}
tweetid <- "998970022474199041"
z <- get_replies(tweetid)
# to get multiple; install purrr if you do not have it
tweetid <- c("998662565222895618","998602734386458626")
z <- purrr::map_df(tweetid, get_replies)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment