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.

86 lines
3.4KB

  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. #' @return a [HARtools] `har` object
  10. #' @references [Splash docs](http://splash.readthedocs.io/en/stable/index.html)
  11. #' @export
  12. render_har <- function(splash_obj = splash_local, url, base_url, response_body=FALSE, timeout=30, resource_timeout, wait=0,
  13. proxy, js, js_src, filters, allowed_domains, allowed_content_types,
  14. forbidden_content_types, viewport="1024x768", images, headers, body,
  15. http_method, save_args, load_args) {
  16. params <- list(url=url, timeout=timeout, wait=wait, viewport=viewport,
  17. response_body=as.numeric(response_body))
  18. if (!missing(base_url)) params$base_url <- base_url
  19. if (!missing(resource_timeout)) params$resource_timeout <- resource_timeout
  20. if (!missing(proxy)) proxy$base_url <- proxy
  21. if (!missing(js)) params$js <- js
  22. if (!missing(js_src)) params$js_src <- js_src
  23. if (!missing(filters)) params$filters <- filters
  24. if (!missing(allowed_domains)) params$allowed_domains <- allowed_domains
  25. if (!missing(allowed_content_types)) params$allowed_content_types <- allowed_content_types
  26. if (!missing(forbidden_content_types)) params$forbidden_content_types <- forbidden_content_types
  27. if (!missing(images)) params$images <- images
  28. if (!missing(headers)) params$headers <- headers
  29. if (!missing(body)) params$body <- body
  30. if (!missing(http_method)) params$http_method <- http_method
  31. if (!missing(save_args)) params$save_args <- save_args
  32. if (!missing(load_args)) params$load_args <- load_args
  33. res <- httr::GET(splash_url(splash_obj), path="render.har", encode="json", query=params)
  34. httr::stop_for_status(res)
  35. out <- httr::content(res, as="text", encoding="UTF-8")
  36. spl <- jsonlite::fromJSON(out, flatten=FALSE, simplifyVector=FALSE)
  37. as_har(spl)
  38. }
  39. #' Turn a generic Splash HAR response into a HAR object
  40. #'
  41. #' @param splash_resp splash response object
  42. #' @export
  43. as_har <- function(splash_resp) {
  44. if (is.raw(splash_resp)) splash_resp <- jsonlite::fromJSON(rawToChar(splash_resp),
  45. flatten=FALSE,
  46. simplifyVector=FALSE)
  47. sphar <- list(
  48. log=list(
  49. version=splash_resp$log$version,
  50. creator=splash_resp$log$creator,
  51. browser=splash_resp$log$browser,
  52. pages=splash_resp$log$pages,
  53. entries=splash_resp$log$entries
  54. )
  55. )
  56. class(sphar$log$creator) <- c("harcreator", "list")
  57. class(sphar$log$version) <- c("harversion", "character")
  58. class(sphar$log$browser) <- c("harbrowser", "list")
  59. class(sphar$log$pages) <- c("harpages", "list")
  60. class(sphar$log$entries) <- c("harentries", "list")
  61. class(sphar$log) <- c("harlog", "list")
  62. class(sphar) <- c("har", "list")
  63. for (i in 1:length(sphar$log$pages)) class(sphar$log$pages[[i]]) <- c("harpage", "list")
  64. for (i in 1:length(sphar$log$entries)) {
  65. class(sphar$log$entries[[i]]) <- c("harentry", "list")
  66. class(sphar$log$entries[[i]]$request) <- c("harrequest", "list")
  67. class(sphar$log$entries[[i]]$response) <- c("harresponse", "list")
  68. }
  69. sphar
  70. }