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.

122 lines
5.7KB

  1. #' Return a json-encoded dictionary with information about javascript-rendered webpage.
  2. #'
  3. #' It can include HTML, PNG and other information, based on arguments passed.
  4. #'
  5. #' @md
  6. #' @rdname render_json
  7. #' @param html Whether to include HTML in output.
  8. #' @param png Whether to include PNG in output.
  9. #' @param jpeg Whether to include JPEG in output.
  10. #' @param iframes Whether to include information about child frames in output.
  11. #' @param script Whether to include the result of the custom executed javascript final
  12. #' statement in output
  13. #' @param console Whether to include the executed javascript console messages in output.
  14. #' @param history Whether to include the history of requests/responses for webpage main frame.
  15. #' Use it to get HTTP status codes and headers. Only information about "main"
  16. #' requests/responses is returned (i.e. information about related resources
  17. #' like images and AJAX queries is not returned). To get information about all
  18. #' requests and responses use `har` parameter.
  19. #' @param har Whether to include HAR in output. If `TRUE` the result will contain the same
  20. #' data as [render_har()] provides under `har` list entry. By default, response
  21. #' content is not included. To enable it use `response_body` parameter.
  22. #' @param response_body Used with `har` parameter.
  23. #' @return a huge `list`
  24. #' @family splash_renderers
  25. #' @inheritParams render_jpeg
  26. #' @note All "whether to include..." parameters are default `TRUE` except for `png` and
  27. #' `jpeg` and a custom `print` method is defined to stop your console from being
  28. #' overwhelmed with data. Use [str()] to inspect various portions of the result.
  29. #' @references [Splash docs](http://splash.readthedocs.io/en/stable/index.html)
  30. #' @export
  31. render_json <- function(splash_obj = splash_local, url, base_url=NULL, quality=75, width, height,
  32. timeout=30, resource_timeout, wait=0, render_all=FALSE,
  33. proxy, js, js_src, filters, allowed_domains, allowed_content_types,
  34. forbidden_content_types, viewport="1024x768", images, headers, body,
  35. http_method, save_args, load_args, html=TRUE, png=FALSE, jpeg=FALSE,
  36. iframes=TRUE, script=TRUE, console=TRUE, history=TRUE, har=TRUE,
  37. response_body=FALSE) {
  38. wait <- check_wait(wait)
  39. params <- list(url=url, timeout=timeout, wait=wait, viewport=jsonlite::unbox(viewport),
  40. quality=quality, render_all=as.numeric(render_all),
  41. html=as.numeric(html), png=as.numeric(png), jpeg=as.numeric(jpeg),
  42. iframes=as.numeric(iframes), script=as.numeric(script),
  43. console=as.numeric(console), history=as.numeric(history), har=as.numeric(har),
  44. response_body=as.numeric(response_body))
  45. if (!missing(width)) params$width <- width
  46. if (!missing(height)) params$height <- height
  47. if (!missing(base_url)) params$base_url <- jsonlite::unbox(base_url)
  48. if (!missing(resource_timeout)) params$resource_timeout <- resource_timeout
  49. if (!missing(proxy)) params$proxy <- jsonlite::unbox(proxy)
  50. if (!missing(js)) params$js <- jsonlite::unbox(js)
  51. if (!missing(js_src)) params$js_src <- jsonlite::unbox(js_src)
  52. if (!missing(filters)) params$filters <- jsonlite::unbox(filters)
  53. if (!missing(allowed_domains)) params$allowed_domains <- jsonlite::unbox(allowed_domains)
  54. if (!missing(allowed_content_types)) params$allowed_content_types <- jsonlite::unbox(allowed_content_types)
  55. if (!missing(forbidden_content_types)) params$forbidden_content_types <- jsonlite::unbox(forbidden_content_types)
  56. if (!missing(images)) params$images <- as.numeric(images)
  57. if (!missing(headers)) params$headers <- headers
  58. if (!missing(body)) params$body <- jsonlite::unbox(body)
  59. if (!missing(http_method)) params$http_method <- jsonlite::unbox(http_method)
  60. if (!missing(save_args)) params$save_args <- jsonlite::unbox(save_args)
  61. if (!missing(load_args)) params$load_args <- jsonlite::unbox(load_args)
  62. if (is.null(splash_obj$user)) {
  63. res <- httr::GET(splash_url(splash_obj), path="render.json", encode="json", query=params)
  64. } else {
  65. res <- httr::GET(
  66. splash_url(splash_obj), path="render.json", encode="json", query=params,
  67. httr::authenticate(splash_obj$user, splash_obj$pass)
  68. )
  69. }
  70. httr::stop_for_status(res)
  71. out <- httr::content(res, as="text", encoding="UTF-8")
  72. out <- jsonlite::fromJSON(out, flatten=FALSE, simplifyVector=FALSE)
  73. class(out) <- c("splash_json", class(out))
  74. if ("har" %in% names(out)) {
  75. sphar <- list(
  76. log = list(
  77. version = out$har$log$version,
  78. creator = out$har$log$creator,
  79. browser = out$har$log$browser,
  80. pages = out$har$log$pages,
  81. entries = out$har$log$entries
  82. )
  83. )
  84. class(sphar$log$creator) <- c("harcreator", "list")
  85. class(sphar$log$version) <- c("harversion", "character")
  86. class(sphar$log$browser) <- c("harbrowser", "list")
  87. class(sphar$log$pages) <- c("harpages", "list")
  88. class(sphar$log$entries) <- c("harentries", "list")
  89. class(sphar$log) <- c("harlog", "list")
  90. class(sphar) <- c("har", "list")
  91. for (i in 1:length(sphar$log$pages)) class(sphar$log$pages[[i]]) <- c("harpage", "list")
  92. for (i in 1:length(sphar$log$entries)) {
  93. class(sphar$log$entries[[i]]) <- c("harentry", "list")
  94. if (length(sphar$log$entries[[i]]$request) > 0) class(sphar$log$entries[[i]]$request) <- c("harrequest", "list")
  95. if (length(sphar$log$entries[[i]]$response) > 0) class(sphar$log$entries[[i]]$response) <- c("harresponse", "list")
  96. }
  97. out$har <- sphar
  98. }
  99. out
  100. }
  101. #' @export
  102. print.splash_json <- function(x, ...) {
  103. cat("<splashr render_json() object>")
  104. invisible(x)
  105. }