Tools to Work with the 'Splash' JavaScript Rendering Service in R
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

108 satır
4.2KB

  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, http2 = FALSE,
  17. engine = c("webkit", "chromium")) {
  18. wait <- check_wait(wait)
  19. engine <- match.arg(engine[1], c("webkit", "chromium"))
  20. http2 <- ifelse(engine == "chromium", 1, as.integer(as.logical(http2[1])))
  21. list(
  22. url = url,
  23. timeout = timeout,
  24. wait = wait,
  25. viewport = jsonlite::unbox(viewport),
  26. response_body = as.numeric(response_body),
  27. http2 = http2,
  28. engine = engine
  29. ) -> params
  30. if (!missing(base_url)) params$base_url <- jsonlite::unbox(base_url)
  31. if (!missing(resource_timeout)) params$resource_timeout <- resource_timeout
  32. if (!missing(proxy)) params$proxy <- jsonlite::unbox(proxy)
  33. if (!missing(js)) params$js <- jsonlite::unbox(js)
  34. if (!missing(js_src)) params$js_src <- jsonlite::unbox(js_src)
  35. if (!missing(filters)) params$filters <- jsonlite::unbox(filters)
  36. if (!missing(allowed_domains)) params$allowed_domains <- jsonlite::unbox(allowed_domains)
  37. if (!missing(allowed_content_types)) params$allowed_content_types <- jsonlite::unbox(allowed_content_types)
  38. if (!missing(forbidden_content_types)) params$forbidden_content_types <- jsonlite::unbox(forbidden_content_types)
  39. if (!missing(images)) params$images <- as.numeric(images)
  40. if (!missing(headers)) params$headers <- headers
  41. if (!missing(body)) params$body <- jsonlite::unbox(body)
  42. if (!missing(http_method)) params$http_method <- jsonlite::unbox(http_method)
  43. if (!missing(save_args)) params$save_args <- jsonlite::unbox(save_args)
  44. if (!missing(load_args)) params$load_args <- jsonlite::unbox(load_args)
  45. if (is.null(splash_obj$user)) {
  46. res <- httr::GET(splash_url(splash_obj), path="render.har", encode="json", query=params)
  47. } else {
  48. res <- httr::GET(
  49. splash_url(splash_obj), path="render.har", encode="json", query=params,
  50. httr::authenticate(splash_obj$user, splash_obj$pass)
  51. )
  52. }
  53. check_or_report_status(res)
  54. out <- httr::content(res, as="text", encoding="UTF-8")
  55. spl <- jsonlite::fromJSON(out, flatten=FALSE, simplifyVector=FALSE)
  56. as_har(spl)
  57. }
  58. #' Turn a generic Splash HAR response into a HAR object
  59. #'
  60. #' @param splash_resp splash response object
  61. #' @export
  62. as_har <- function(splash_resp) {
  63. if (is.raw(splash_resp)) splash_resp <- jsonlite::fromJSON(rawToChar(splash_resp),
  64. flatten=FALSE,
  65. simplifyVector=FALSE)
  66. sphar <- list(
  67. log=list(
  68. version=splash_resp$log$version,
  69. creator=splash_resp$log$creator,
  70. browser=splash_resp$log$browser,
  71. pages=splash_resp$log$pages,
  72. entries=splash_resp$log$entries
  73. )
  74. )
  75. class(sphar$log$creator) <- c("harcreator", "list")
  76. class(sphar$log$version) <- c("harversion", "character")
  77. class(sphar$log$browser) <- c("harbrowser", "list")
  78. class(sphar$log$pages) <- c("harpages", "list")
  79. class(sphar$log$entries) <- c("harentries", "list")
  80. class(sphar$log) <- c("harlog", "list")
  81. class(sphar) <- c("har", "list")
  82. for (i in 1:length(sphar$log$pages)) class(sphar$log$pages[[i]]) <- c("harpage", "list")
  83. for (i in 1:length(sphar$log$entries)) {
  84. class(sphar$log$entries[[i]]) <- c("harentry", "list")
  85. class(sphar$log$entries[[i]]$request) <- c("harrequest", "list")
  86. class(sphar$log$entries[[i]]$response) <- c("harresponse", "list")
  87. }
  88. sphar
  89. }