boB Rudis
4 years ago
15 changed files with 255 additions and 9 deletions
@ -0,0 +1,2 @@ |
|||
YEAR: 2019 |
|||
COPYRIGHT HOLDER: Bob Rudis |
@ -0,0 +1,21 @@ |
|||
# MIT License |
|||
|
|||
Copyright (c) 2019 Bob Rudis |
|||
|
|||
Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
of this software and associated documentation files (the "Software"), to deal |
|||
in the Software without restriction, including without limitation the rights |
|||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
copies of the Software, and to permit persons to whom the Software is |
|||
furnished to do so, subject to the following conditions: |
|||
|
|||
The above copyright notice and this permission notice shall be included in all |
|||
copies or substantial portions of the Software. |
|||
|
|||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|||
SOFTWARE. |
@ -1,4 +1,6 @@ |
|||
# Generated by roxygen2: do not edit by hand |
|||
|
|||
export(generate_screenshot) |
|||
import(httr) |
|||
import(magick) |
|||
importFrom(jsonlite,fromJSON) |
|||
|
@ -0,0 +1,7 @@ |
|||
httr::user_agent( |
|||
sprintf( |
|||
"statically package v%s: (<%s>)", |
|||
utils::packageVersion("statically"), |
|||
utils::packageDescription("statically")[["URL"]] |
|||
) |
|||
) -> .STATICALLY_UA |
@ -0,0 +1,53 @@ |
|||
#' Generate a webpage screenshot |
|||
#' |
|||
#' @param url the URL to screenshot |
|||
#' @param quality image quality. Defaults to 100 (i.e. 100%) |
|||
#' @param full_page generate a full page scren shot (may be very "long"). Default `FALSE`. |
|||
#' @param mobile generate a screenshot of a mobile version of the website? Default `FALSE`. |
|||
#' @param width screen width (pixels). Defaults to `NULL` which uses the Statically |
|||
#' default (which appears to be 1280). |
|||
#' @param type which image format to request. This function returns a `{magick}` object |
|||
#' regardless of what is specified here. Using a type other than "`jpeg`" |
|||
#' (the API default and this function's default) may result in subtly different |
|||
#' captures. |
|||
#' @param ... passed on to [httr::GET()]. |
|||
#' @return `{magick}` object |
|||
#' @export |
|||
#' @examples |
|||
#' generate_screenshot("https://cran.r-project.org") |
|||
generate_screenshot <- function(url, quality = 100L, full_page = FALSE, |
|||
mobile = FALSE, width = NULL, |
|||
type = c("jpeg", "png", "webp"), |
|||
...) { |
|||
|
|||
url <- sub("^http[s]*://", "", url[1]) |
|||
|
|||
type <- match.arg(tolower(type[1]), c("jpeg", "png", "webp")) |
|||
|
|||
full_page <- as.logical(full_page[1]) |
|||
full_page <- if (full_page) tolower(full_page) else NULL |
|||
|
|||
mobile <- as.logical(mobile[1]) |
|||
mobile <- if (mobile) tolower(mobile) else NULL |
|||
|
|||
quality <- as.integer(quality[1]) |
|||
stopifnot((quality > 0) && (quality <= 100)) |
|||
|
|||
httr::GET( |
|||
url = sprintf("https://cdn.statically.io/screenshot/%s", url), |
|||
query = list ( |
|||
type = type, |
|||
quality = quality, |
|||
fullPage = full_page, |
|||
mobile = mobile, |
|||
w = width |
|||
), |
|||
.STATICALLY_UA, |
|||
... |
|||
) -> res |
|||
|
|||
httr::stop_for_status(res) |
|||
|
|||
magick::image_read(res$content) |
|||
|
|||
} |
@ -1,9 +1,13 @@ |
|||
#' ... |
|||
#' |
|||
#' Generate Webpage Screenshots Using the Statically API |
|||
#' |
|||
#' Statically (<https://statically.io/screenshot/>) maintains a free |
|||
#' API to generate screenshots of webpages. Tools are provided to capture |
|||
#' website screenshots using this API. |
|||
#' |
|||
#' @md |
|||
#' @name statically |
|||
#' @keywords internal |
|||
#' @author Bob Rudis (bob@@rud.is) |
|||
#' @import httr |
|||
#' @import httr magick |
|||
#' @importFrom jsonlite fromJSON |
|||
"_PACKAGE" |
|||
|
@ -0,0 +1,89 @@ |
|||
|
|||
[![Project Status: Active – The project has reached a stable, usable |
|||
state and is being actively |
|||
developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active) |
|||
[![Signed |
|||
by](https://img.shields.io/badge/Keybase-Verified-brightgreen.svg)](https://keybase.io/hrbrmstr) |
|||
![Signed commit |
|||
%](https://img.shields.io/badge/Signed_Commits-100%25-lightgrey.svg) |
|||
[![Linux build |
|||
Status](https://travis-ci.org/hrbrmstr/statically.svg?branch=master)](https://travis-ci.org/hrbrmstr/statically) |
|||
![Minimal R |
|||
Version](https://img.shields.io/badge/R%3E%3D-3.2.0-blue.svg) |
|||
![License](https://img.shields.io/badge/License-MIT-blue.svg) |
|||
|
|||
# statically |
|||
|
|||
Generate Webpage Screenshots Using the Statically API |
|||
|
|||
## Description |
|||
|
|||
Statically (<https://statically.io/screenshot/>) maintains a free API to |
|||
generate screenshots of webpages. Tools are provided to capture website |
|||
screenshots using this API. |
|||
|
|||
## What’s Inside The Tin |
|||
|
|||
The following functions are implemented: |
|||
|
|||
- `generate_screenshot`: Generate a webpage screenshot |
|||
|
|||
## Installation |
|||
|
|||
``` r |
|||
remotes::install_git("https://git.rud.is/hrbrmstr/statically.git") |
|||
# or |
|||
remotes::install_git("https://git.sr.ht/~hrbrmstr/statically") |
|||
# or |
|||
remotes::install_gitlab("hrbrmstr/statically") |
|||
# or |
|||
remotes::install_bitbucket("hrbrmstr/statically") |
|||
``` |
|||
|
|||
NOTE: To use the ‘remotes’ install options you will need to have the |
|||
[{remotes} package](https://github.com/r-lib/remotes) installed. |
|||
|
|||
## Usage |
|||
|
|||
``` r |
|||
library(statically) |
|||
|
|||
# current version |
|||
packageVersion("statically") |
|||
## [1] '0.1.0' |
|||
``` |
|||
|
|||
Default: |
|||
|
|||
``` r |
|||
generate_screenshot("https://www.latimes.com/") |
|||
``` |
|||
|
|||
![](man/figures/README-ex1-1.png)<!-- --> |
|||
|
|||
Mobile: |
|||
|
|||
``` r |
|||
generate_screenshot("https://www.latimes.com/", mobile = TRUE) |
|||
``` |
|||
|
|||
![](man/figures/README-ex3-1.png)<!-- --> |
|||
|
|||
Full page (not shown b/c it’s super duper long): |
|||
|
|||
``` r |
|||
generate_screenshot("https://rud.is/b", full_page = TRUE) |
|||
``` |
|||
|
|||
## statically Metrics |
|||
|
|||
| Lang | \# Files | (%) | LoC | (%) | Blank lines | (%) | \# Lines | (%) | |
|||
| :--- | -------: | --: | --: | ---: | ----------: | ---: | -------: | ---: | |
|||
| R | 4 | 0.8 | 38 | 0.78 | 11 | 0.34 | 29 | 0.44 | |
|||
| Rmd | 1 | 0.2 | 11 | 0.22 | 21 | 0.66 | 37 | 0.56 | |
|||
|
|||
## Code of Conduct |
|||
|
|||
Please note that this project is released with a Contributor Code of |
|||
Conduct. By participating in this project you agree to abide by its |
|||
terms. |
After Width: | Height: | Size: 122 KiB |
After Width: | Height: | Size: 160 KiB |
After Width: | Height: | Size: 96 KiB |
@ -0,0 +1,44 @@ |
|||
% Generated by roxygen2: do not edit by hand |
|||
% Please edit documentation in R/generate-screenshot.R |
|||
\name{generate_screenshot} |
|||
\alias{generate_screenshot} |
|||
\title{Generate a webpage screenshot} |
|||
\usage{ |
|||
generate_screenshot( |
|||
url, |
|||
quality = 100L, |
|||
full_page = FALSE, |
|||
mobile = FALSE, |
|||
width = NULL, |
|||
type = c("jpeg", "png", "webp"), |
|||
... |
|||
) |
|||
} |
|||
\arguments{ |
|||
\item{url}{the URL to screenshot} |
|||
|
|||
\item{quality}{image quality. Defaults to 100 (i.e. 100\%)} |
|||
|
|||
\item{full_page}{generate a full page scren shot (may be very "long"). Default \code{FALSE}.} |
|||
|
|||
\item{mobile}{generate a screenshot of a mobile version of the website? Default \code{FALSE}.} |
|||
|
|||
\item{width}{screen width (pixels). Defaults to \code{NULL} which uses the Statically |
|||
default (which appears to be 1280).} |
|||
|
|||
\item{type}{which image format to request. This function returns a \code{{magick}} object |
|||
regardless of what is specified here. Using a type other than "\code{jpeg}" |
|||
(the API default and this function's default) may result in subtly different |
|||
captures.} |
|||
|
|||
\item{...}{passed on to \code{\link[httr:GET]{httr::GET()}}.} |
|||
} |
|||
\value{ |
|||
\code{{magick}} object |
|||
} |
|||
\description{ |
|||
Generate a webpage screenshot |
|||
} |
|||
\examples{ |
|||
generate_screenshot("https://cran.r-project.org") |
|||
} |
Loading…
Reference in new issue