mirror of https://git.sr.ht/~hrbrmstr/splashr
boB Rudis
7 years ago
11 changed files with 329 additions and 8 deletions
@ -0,0 +1,53 @@ |
|||||
|
#' Retrieve the Docker image for Splash |
||||
|
#' |
||||
|
#' @md |
||||
|
#' @param host Docker host; defauolts to `localhost` |
||||
|
#' @return `harbor` `host` object |
||||
|
#' @export |
||||
|
#' @examples \dontrun{ |
||||
|
#' install_splash() |
||||
|
#' splash_container <- start_splash() |
||||
|
#' stop_splash(splash_container) |
||||
|
#' } |
||||
|
install_splash <- function(host = harbor::localhost) { |
||||
|
harbor::docker_pull(host, "scrapinghub/splash") |
||||
|
} |
||||
|
|
||||
|
#' Start a Splash server Docker container |
||||
|
#' |
||||
|
#' @param host Docker host; defauolts to `localhost` |
||||
|
#' @note you need Docker running on your system and have pulled the container with |
||||
|
#' [install_spash] for this to work. You should save the resultant `host` |
||||
|
#' object for use in [stop_splash]. |
||||
|
#' @return `harbor` `container` object |
||||
|
#' @export |
||||
|
#' @examples \dontrun{ |
||||
|
#' install_splash() |
||||
|
#' splash_container <- start_splash() |
||||
|
#' stop_splash(splash_container) |
||||
|
#' } |
||||
|
start_splash <- function(host = harbor::localhost) { |
||||
|
harbor::docker_run(host, |
||||
|
image = "scrapinghub/splash", |
||||
|
detach = TRUE, |
||||
|
docker_opts = c("-p", "5023:5023", |
||||
|
"-p", "8050:8050", |
||||
|
"-p", "8051:8051")) |
||||
|
} |
||||
|
|
||||
|
#' Stop a running a Splash server Docker container |
||||
|
#' |
||||
|
#' @param splash_container saved Splash container id from [start_splash] |
||||
|
#' @param splash_container Docker `container` object created by [start_splash] |
||||
|
#' @note you need Docker running on your system and have pulled the container with |
||||
|
#' [install_spash] and started the Splash container with [start_splash] for this |
||||
|
#' to work. You will need the `container` object from [start_splash] for this to work. |
||||
|
#' @export |
||||
|
#' @examples \dontrun{ |
||||
|
#' install_splash() |
||||
|
#' splash_container <- start_splash() |
||||
|
#' stop_splash(splash_container) |
||||
|
#' } |
||||
|
stop_splash <- function(splash_container) { |
||||
|
harbor::container_rm(splash_container, force=TRUE) |
||||
|
} |
@ -0,0 +1,92 @@ |
|||||
|
#' Retrieve or test content type of a HAR request object |
||||
|
#' |
||||
|
#' @param har_resp_obj HAR response object |
||||
|
#' @export |
||||
|
get_content_type <- function(har_resp_obj) { |
||||
|
ctype <- har_resp_obj$response$content$mimeType |
||||
|
if (ctype == "") return(NA_character_) |
||||
|
if (any(grepl(";", ctype))) ctype <- gsub(";.*$", "", ctype) |
||||
|
ctype |
||||
|
} |
||||
|
|
||||
|
#' @md |
||||
|
#' @rdname get_content_type |
||||
|
#' @param type content type to compare to (default: "`application/json`") |
||||
|
#' @export |
||||
|
is_content_type <- function(har_resp_obj, type="application/json") { |
||||
|
get_content_type(har_resp_obj) == type |
||||
|
} |
||||
|
|
||||
|
#' @rdname get_content_type |
||||
|
#' @export |
||||
|
is_json <- function(har_resp_obj) { is_content_type(har_resp_obj) } |
||||
|
|
||||
|
#' @rdname get_content_type |
||||
|
#' @export |
||||
|
is_xml <- function(har_resp_obj) { is_content_type(har_resp_obj, type="application/xml") } |
||||
|
|
||||
|
#' @rdname get_content_type |
||||
|
#' @export |
||||
|
is_css <- function(har_resp_obj) { is_content_type(har_resp_obj, type="text/css") } |
||||
|
|
||||
|
#' @rdname get_content_type |
||||
|
#' @export |
||||
|
is_plain <- function(har_resp_obj) { is_content_type(har_resp_obj, type="text/plain") } |
||||
|
|
||||
|
#' @rdname get_content_type |
||||
|
#' @export |
||||
|
is_binary <- function(har_resp_obj) { is_content_type(har_resp_obj, type="application/octet-stream") } |
||||
|
|
||||
|
#' @rdname get_content_type |
||||
|
#' @export |
||||
|
is_javascript <- function(har_resp_obj) { |
||||
|
is_content_type(har_resp_obj, type="text/javascript") | |
||||
|
is_content_type(har_resp_obj, type="text/x-javascript") |
||||
|
} |
||||
|
|
||||
|
#' @rdname get_content_type |
||||
|
#' @export |
||||
|
is_html <- function(har_resp_obj) { is_content_type(har_resp_obj, type="text/html") } |
||||
|
|
||||
|
#' @rdname get_content_type |
||||
|
#' @export |
||||
|
is_jpeg <- function(har_resp_obj) { is_content_type(har_resp_obj, type="image/jpeg") } |
||||
|
|
||||
|
#' @rdname get_content_type |
||||
|
#' @export |
||||
|
is_png <- function(har_resp_obj) { is_content_type(har_resp_obj, type="image/png") } |
||||
|
|
||||
|
#' @rdname get_content_type |
||||
|
#' @export |
||||
|
is_svg <- function(har_resp_obj) { is_content_type(har_resp_obj, type="image/svg+xml") } |
||||
|
|
||||
|
#' @rdname get_content_type |
||||
|
#' @export |
||||
|
is_gif <- function(har_resp_obj) { is_content_type(har_resp_obj, type="image/gif") } |
||||
|
|
||||
|
#' Retrieve size of content | body | headers |
||||
|
#' |
||||
|
#' @param har_resp_obj HAR response object |
||||
|
#' @export |
||||
|
get_content_size <- function(har_resp_obj) { |
||||
|
csize <- har_resp_obj$response$content$size |
||||
|
if (is.null(csize)) return(NA_real_) |
||||
|
return(as.numeric(csize)) |
||||
|
} |
||||
|
|
||||
|
#' @rdname get_content_size |
||||
|
#' @export |
||||
|
get_body_size <- function(har_resp_obj) { |
||||
|
bsize <- har_resp_obj$response$bodySize |
||||
|
if (is.null(bsize)) return(NA_real_) |
||||
|
return(as.numeric(bsize)) |
||||
|
} |
||||
|
|
||||
|
#' @rdname get_content_size |
||||
|
#' @export |
||||
|
get_headers_size <- function(har_resp_obj) { |
||||
|
hsize <- har_resp_obj$response$headersSize |
||||
|
if (is.null(hsize)) return(NA_real_) |
||||
|
return(as.numeric(hsize)) |
||||
|
} |
||||
|
|
@ -0,0 +1,20 @@ |
|||||
|
% Generated by roxygen2: do not edit by hand |
||||
|
% Please edit documentation in R/helpers.r |
||||
|
\name{get_content_size} |
||||
|
\alias{get_content_size} |
||||
|
\alias{get_body_size} |
||||
|
\alias{get_headers_size} |
||||
|
\title{Retrieve size of content | body | headers} |
||||
|
\usage{ |
||||
|
get_content_size(har_resp_obj) |
||||
|
|
||||
|
get_body_size(har_resp_obj) |
||||
|
|
||||
|
get_headers_size(har_resp_obj) |
||||
|
} |
||||
|
\arguments{ |
||||
|
\item{har_resp_obj}{HAR response object} |
||||
|
} |
||||
|
\description{ |
||||
|
Retrieve size of content | body | headers |
||||
|
} |
@ -0,0 +1,52 @@ |
|||||
|
% Generated by roxygen2: do not edit by hand |
||||
|
% Please edit documentation in R/helpers.r |
||||
|
\name{get_content_type} |
||||
|
\alias{get_content_type} |
||||
|
\alias{is_content_type} |
||||
|
\alias{is_json} |
||||
|
\alias{is_xml} |
||||
|
\alias{is_css} |
||||
|
\alias{is_plain} |
||||
|
\alias{is_binary} |
||||
|
\alias{is_javascript} |
||||
|
\alias{is_html} |
||||
|
\alias{is_jpeg} |
||||
|
\alias{is_png} |
||||
|
\alias{is_svg} |
||||
|
\alias{is_gif} |
||||
|
\title{Retrieve or test content type of a HAR request object} |
||||
|
\usage{ |
||||
|
get_content_type(har_resp_obj) |
||||
|
|
||||
|
is_content_type(har_resp_obj, type = "application/json") |
||||
|
|
||||
|
is_json(har_resp_obj) |
||||
|
|
||||
|
is_xml(har_resp_obj) |
||||
|
|
||||
|
is_css(har_resp_obj) |
||||
|
|
||||
|
is_plain(har_resp_obj) |
||||
|
|
||||
|
is_binary(har_resp_obj) |
||||
|
|
||||
|
is_javascript(har_resp_obj) |
||||
|
|
||||
|
is_html(har_resp_obj) |
||||
|
|
||||
|
is_jpeg(har_resp_obj) |
||||
|
|
||||
|
is_png(har_resp_obj) |
||||
|
|
||||
|
is_svg(har_resp_obj) |
||||
|
|
||||
|
is_gif(har_resp_obj) |
||||
|
} |
||||
|
\arguments{ |
||||
|
\item{har_resp_obj}{HAR response object} |
||||
|
|
||||
|
\item{type}{content type to compare to (default: "\code{application/json}")} |
||||
|
} |
||||
|
\description{ |
||||
|
Retrieve or test content type of a HAR request object |
||||
|
} |
Loading…
Reference in new issue