Tools to Work with the 'Splash' JavaScript Rendering Service in R
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.9KB

  1. #' Return information about Splash interaction with a website in HAR format.
  2. #'
  3. #' It includes information about requests made, responses received, timings, headers, etc and
  4. #' is incredibly detailed, full of information on every component loaded.
  5. #'
  6. #' @md
  7. #' @param response_body When `TRUE`, response content is included in the HAR records
  8. #' @inheritParams render_html
  9. #' @family splash_renderers
  10. #' @return a [HARtools] `har` object
  11. #' @references [Splash docs](http://splash.readthedocs.io/en/stable/index.html)
  12. #' @export
  13. render_har <- function(splash_obj = splash_local, url, base_url, response_body=FALSE, timeout=30, resource_timeout, wait=0,
  14. proxy, js, js_src, filters, allowed_domains, allowed_content_types,
  15. forbidden_content_types, viewport="1024x768", images, headers, body,
  16. http_method, save_args, load_args) {
  17. wait <- check_wait(wait)
  18. params <- list(url=url, timeout=timeout, wait=wait, viewport=jsonlite::unbox(viewport),
  19. response_body=as.numeric(response_body))
  20. if (!missing(base_url)) params$base_url <- jsonlite::unbox(base_url)
  21. if (!missing(resource_timeout)) params$resource_timeout <- resource_timeout
  22. if (!missing(proxy)) params$proxy <- jsonlite::unbox(proxy)
  23. if (!missing(js)) params$js <- jsonlite::unbox(js)
  24. if (!missing(js_src)) params$js_src <- jsonlite::unbox(js_src)
  25. if (!missing(filters)) params$filters <- jsonlite::unbox(filters)
  26. if (!missing(allowed_domains)) params$allowed_domains <- jsonlite::unbox(allowed_domains)
  27. if (!missing(allowed_content_types)) params$allowed_content_types <- jsonlite::unbox(allowed_content_types)
  28. if (!missing(forbidden_content_types)) params$forbidden_content_types <- jsonlite::unbox(forbidden_content_types)
  29. if (!missing(images)) params$images <- as.numeric(images)
  30. if (!missing(headers)) params$headers <- headers
  31. if (!missing(body)) params$body <- jsonlite::unbox(body)
  32. if (!missing(http_method)) params$http_method <- jsonlite::unbox(http_method)
  33. if (!missing(save_args)) params$save_args <- jsonlite::unbox(save_args)
  34. if (!missing(load_args)) params$load_args <- jsonlite::unbox(load_args)
  35. if (is.null(splash_obj$user)) {
  36. res <- httr::GET(splash_url(splash_obj), path="render.har", encode="json", query=params)
  37. } else {
  38. res <- httr::GET(
  39. splash_url(splash_obj), path="render.html", encode="json", query=params,
  40. httr::authenticate(splash_obj$user, splash_obj$pass)
  41. )
  42. }
  43. httr::stop_for_status(res)
  44. out <- httr::content(res, as="text", encoding="UTF-8")
  45. spl <- jsonlite::fromJSON(out, flatten=FALSE, simplifyVector=FALSE)
  46. as_har(spl)
  47. }
  48. #' Turn a generic Splash HAR response into a HAR object
  49. #'
  50. #' @param splash_resp splash response object
  51. #' @export
  52. as_har <- function(splash_resp) {
  53. if (is.raw(splash_resp)) splash_resp <- jsonlite::fromJSON(rawToChar(splash_resp),
  54. flatten=FALSE,
  55. simplifyVector=FALSE)
  56. sphar <- list(
  57. log=list(
  58. version=splash_resp$log$version,
  59. creator=splash_resp$log$creator,
  60. browser=splash_resp$log$browser,
  61. pages=splash_resp$log$pages,
  62. entries=splash_resp$log$entries
  63. )
  64. )
  65. class(sphar$log$creator) <- c("harcreator", "list")
  66. class(sphar$log$version) <- c("harversion", "character")
  67. class(sphar$log$browser) <- c("harbrowser", "list")
  68. class(sphar$log$pages) <- c("harpages", "list")
  69. class(sphar$log$entries) <- c("harentries", "list")
  70. class(sphar$log) <- c("harlog", "list")
  71. class(sphar) <- c("har", "list")
  72. for (i in 1:length(sphar$log$pages)) class(sphar$log$pages[[i]]) <- c("harpage", "list")
  73. for (i in 1:length(sphar$log$entries)) {
  74. class(sphar$log$entries[[i]]) <- c("harentry", "list")
  75. class(sphar$log$entries[[i]]$request) <- c("harrequest", "list")
  76. class(sphar$log$entries[[i]]$response) <- c("harresponse", "list")
  77. }
  78. sphar
  79. }