Skip to content

Instantly share code, notes, and snippets.

@uribo
Created January 31, 2016 05:15
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 uribo/9c5b813fd5d35b012cb6 to your computer and use it in GitHub Desktop.
Save uribo/9c5b813fd5d35b012cb6 to your computer and use it in GitHub Desktop.
Ask to you from R
#' Ask to you!
#'
#' @import RCurl
#' @importFrom xml2 read_html
#' @importFrom revst html_form
#' @param interviewee character. username
#' @param message character. sending text
#' @param as.anonymous logical
#' @param cookies path to cookies.txt for https://ask.fm
#' @examples
#' \dontrun{
#' ask2you(interviewee = "uri_ushi",
#' message = "Why are you using S-JIS?",
#' as.anonymous = TRUE,
#' cookies = "cookies.txt")
#' }
#' @name ask2you
ask2you <- function(interviewee,
message,
as.anonymous = TRUE,
cookies = "cookies.txt") {
url <- paste("https://ask.fm", interviewee, "ask", sep = "/")
curl <- RCurl::getCurlHandle(cookiefile = cookies,
cookiejar = cookies,
useragent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en - US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
auth_token <-
RCurl::getURLContent(url, curl = curl) %>%
xml2::read_html() %>%
rvest::html_form() %>%
.[[1]] %$% fields$authenticity_token$value
RCurl::postForm(
url,
`authenticity_token` = auth_token,
`question[question_text]` = message,
`question[anonymous]` = ifelse(as.anonymous == TRUE, "true", "false"),
curl = curl
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment