Skip to content

Instantly share code, notes, and snippets.

@schochastics
Last active November 20, 2022 19:40
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 schochastics/ed86633da6ca8b29c1b5aacafa06544a to your computer and use it in GitHub Desktop.
Save schochastics/ed86633da6ca8b29c1b5aacafa06544a to your computer and use it in GitHub Desktop.
# original python code: https://fosstodon.org/@jasonbaumgartner@infosec.exchange/109376317839501128
# mapping: https://twitter-elastic.pushshift.io/twitter_user/_mapping
library(curl)
library(jsonlite)
ngram_search <- function(q="",size = 10){
data <- list(query=list())
data[["query"]][["bool"]] <- list()
data[["query"]][["bool"]][["must"]] <- list()
match <- list()
match[["match"]] <- list(screen_name.ngram = q)
data[["query"]][["bool"]][["must"]] <- match
data[['sort']] <- list(followers_count = 'desc')
data[['size']] <- size
h <- new_handle(verbose = FALSE)
handle_setheaders(h,"Content-Type" = "application/json")
handle_setopt(handle = h, postfields=as.character(toJSON(data,auto_unbox = TRUE)), customrequest="GET")
c <- curl_fetch_memory(url = "https://twitter-elastic.pushshift.io/twitter_user/_search", handle=h)
df <- fromJSON(rawToChar(c$content))
tbl <- df$hits$hits
tbl
}
tbl <- ngram_search("NASA")
data.frame(screen_name = tbl$`_source`$screen_name, tbl$`_source`$followers_count)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment