Skip to content

Instantly share code, notes, and snippets.

@JBGruber
Created September 8, 2022 07:05
Show Gist options
  • Save JBGruber/5a640cc4657d5d609fcee770be6d1449 to your computer and use it in GitHub Desktop.
Save JBGruber/5a640cc4657d5d609fcee770be6d1449 to your computer and use it in GitHub Desktop.
Tired of searching through the #APSA online program to find your (Twitter) friends one by one? Run this R script line by line to find everyone you follow on Twitter in the program, or use the included `apsa_search()` to do your own searches.
#' Helps you get the neede packages
#'
#' @param x package name
install_missing <- function(x) {
if (!x %in% installed.packages()[, "Package"]) {
res <- menu(c("yes", "no"), title = paste0("Install missing package ", x, "?"))
if (identical(res, 1L)) {
if (x != "traktok") {
install.packages(x)
} else {
remotes::install_github("JBGruber/traktok")
}
} else {
message("Install missing package ", x, " manually")
}
}
invisible()
}
#' Search APSA 2022 online program
#'
#' @param searches vector of searches. Space is treated like AND.
#' @param cookies cookies.txt file. See
#' https://github.com/JBGruber/traktok#authentication. It works the same here,
#' just with https://convention2.allacademic.com/one/apsa/apsa22/.
#'
#' @return a tibble
apsa_search <- function(searches,
cookies) {
#pb <- progress::progress_bar$new(total = length(searches))
total <- length(searches)
purrr::map_df(seq_along(searches), function(i) {
#pb$tick()
search <- searches[i]
message("search ", i, " of ", total, ": ", search)
url <- paste0("https://convention2.allacademic.com/one/apsa/apsa22/index.php?",
"cmd=Online+Program+Search&",
"program_focus=fulltext_search&",
"search_mode=content&",
"offset=0&search_text=", gsub(" ", "+", search))
html <- httr2::request(url) %>%
httr2::req_options(cookie = traktok:::tt_read_cookies(cookies)) %>%
httr2::req_perform() %>%
httr2::resp_body_html()
events <- html %>%
rvest::html_elements("[id=\"event_list\"]>li")
purrr::map_df(events, function(e) {
links <- e %>%
rvest::html_element("a") %>%
rvest::html_attr("href")
names <- e %>%
rvest::html_element("[style=\"text-indent: -1em; padding-left: 2em; overflow: visible; white-space: normal;\"]") %>%
rvest::html_text()
tibble::tibble(
presentation = names,
link = links,
search_term = search,
search_link = url
)
})
}) %>%
dplyr::filter(!is.na(presentation) | !is.na(link))
}
# install missing
invisible(lapply(c("magrittr", "remotes", "httr2", "dplyr", "rvest", "rtweet"), install_missing))
library(magrittr)
# look up friends
friends <- rtweet::get_friends("JohannesBGruber") %>%
dplyr::pull(to_id) %>%
rtweet::lookup_users()
# search for for friends in the APSA program
presentations <- apsa_search(searches = friends$name,
cookies = "allacademic.com_cookies.txt")
# export to Excel file
rio::export(presentations, "aspa_presentation_twitter.xlsx")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment