Skip to content

Instantly share code, notes, and snippets.

@brshallo
Last active April 17, 2022 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brshallo/119d6a1f858e0e5c20d77212dee8891a to your computer and use it in GitHub Desktop.
Save brshallo/119d6a1f858e0e5c20d77212dee8891a to your computer and use it in GitHub Desktop.
Function to get all various types of engagements from Twitter for a user
library(rjson)
library(httr)
library(jsonlite)
library(dplyr)
library(purrr)
library(lubridate)
library(rtweet)
library(tidyr)
get_engagements <- function(user_id,
min_date = today() - ddays(7),
bearer_token = Sys.getenv("TWITTER_BEARER")){
headers <- c(`Authorization` = sprintf('Bearer %s', bearer_token))
# FAVORITED
favorites <- get_favorites(user_id) %>%
filter(created_at > min_date)
##
# TWEETS
url_handle <- glue::glue("https://api.twitter.com/2/users/{user_id}/tweets?max_results=100", user_id = user_id)
params <- list(tweet.fields = "public_metrics,created_at,in_reply_to_user_id,referenced_tweets")
response <- httr::GET(url = url_handle,
httr::add_headers(.headers = headers),
query = params)
obj <- httr::content(response, as = "text")
json_data <- jsonlite::fromJSON(obj, flatten = TRUE)$data %>%
as_tibble() %>%
filter(ymd_hms(created_at) > today() - ddays(7))
## FAVORITERS
liked_tweets <- json_data %>%
filter(public_metrics.like_count > 0)
tweet_ids <- liked_tweets$id
get_favoriters <- function(tweet_id){
url_handle <- glue::glue("https://api.twitter.com/2/tweets/{status_id}/liking_users", status_id = tweet_id)
response <- httr::GET(url = url_handle,
httr::add_headers(.headers = headers))
obj <- httr::content(response, as = "text")
x <- rjson::fromJSON(obj)
x$data %>%
map_dfr(as_tibble)
}
tweet_favoriters <-
map_dfr(tweet_ids, ~ bind_cols(tibble(liked_status_id = .x),
get_favoriters(.x))) %>%
rename(user_id = id)
# REFERENCERS
statuses_referenced <- bind_rows(json_data$referenced_tweets) %>%
rename(status_id = id)
users_referenced <- rtweet::lookup_tweets(statuses_referenced$status_id)
# QUOTERS
tweet_ids_quoters <- json_data %>%
# filter(public_metrics.reply_count > 0 | public_metrics.quote_count > 0 | public_metrics.retweet_count > 0) %>%
filter(public_metrics.quote_count > 0) %>%
pull(id)
search_tweets_urls <- function(tweet_id){
rtweet::search_tweets(
glue::glue("url:{tweet_id}",
tweet_id = tweet_id)
)
}
if(length(tweet_ids_quoters) > 0){
quoters <- map_dfr(tweet_ids_quoters, search_tweets_urls)
# for some reason... doesn't return all url's with this...
if(nrow(quoters) > 0) {
quoters <- quoters %>%
filter(is_quote) %>%
filter(ymd_hms(created_at) > today() - ddays(7)) %>%
as_tibble()
} else quoters <- NULL
} else quoters <- NULL
# RETWEETERS
tweet_ids_rt <- json_data %>%
filter(public_metrics.retweet_count > 0) %>%
select(status_id = id)
retweeters <- tweet_ids_rt %>%
mutate(retweeters = map(status_id, rtweet::get_retweeters)) %>%
unnest(retweeters)
# REPLIERS & MENTIONS
get_mentions_v2 <- function(user_id){
url_handle <- glue::glue("https://api.twitter.com/2/users/{user_id}/mentions", user_id = user_id)
response <- httr::GET(url = url_handle,
httr::add_headers(.headers = headers))
obj <- httr::content(response, as = "text")
x <- rjson::fromJSON(obj)
x$data %>%
map_dfr(as_tibble)
}
tweets_mentions <- get_mentions_v2(user_id)
repliers_mentions <- lookup_tweets(tweets_mentions$id)
# BINDING EVERYTHING
list(favorites = favorites,
favoriters = tweet_favoriters,
references = users_referenced,
quoters = quoters,
retweeters = retweeters,
referencers = repliers_mentions)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment