Skip to content

Commit

Permalink
New imgur_upload with xml2 and httr (#1433)
Browse files Browse the repository at this point in the history
* use xml2 and httr for imgur_upload

* generate documentation

* add packages to suggests

* stop upload if pkg not installed

* use internal function to check package

* Revert "generate documentation"

This reverts commit b1d655b.

* do not check for package installation

* assume encoding is UTF8

* add myself as new contributor
  • Loading branch information
cderv authored and yihui committed Sep 20, 2017
1 parent 4db8c9a commit 0751802
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
3 changes: 3 additions & 0 deletions DESCRIPTION
Expand Up @@ -15,6 +15,7 @@ Authors@R: c(
person("Ben", "Baumer", role = "ctb"),
person("Brian", "Diggs", role = "ctb"),
person("Cassio", "Pereira", role = "ctb"),
person("Christophe", "Dervieux", role = "ctb"),
person("David", "Robinson", role = "ctb"),
person("Donald", "Arseneau", role = c("ctb", "cph"), comment = "the framed package at inst/misc/framed.sty"),
person("Doug", "Hemken", role = "ctb"),
Expand Down Expand Up @@ -109,6 +110,8 @@ Suggests:
png,
jpeg,
XML,
xml2,
httr,
RCurl,
DBI (>= 0.4-1),
tibble
Expand Down
22 changes: 13 additions & 9 deletions R/utils-upload.R
@@ -1,7 +1,7 @@
#' Upload an image to imgur.com
#'
#' This function uses the \pkg{RCurl} package to upload a image to
#' \url{imgur.com}, and parses the XML response to a list with \pkg{XML} which
#' This function uses the \pkg{httr} package to upload a image to
#' \url{imgur.com}, and parses the XML response to a list with \pkg{xml2} which
#' contains information about the image in the Imgur website.
#'
#' When the output format from \code{\link{knit}()} is HTML or Markdown, this
Expand Down Expand Up @@ -35,13 +35,17 @@
#' opts_knit$set(upload.fun = function(file) imgur_upload(file, key = 'your imgur key'))
#' }
imgur_upload = function(file, key = '9f3460e67f308f6') {

if (!is.character(key)) stop('The Imgur API Key must be a character string!')
res = RCurl::postForm(
'https://api.imgur.com/3/image.xml', image = RCurl::fileUpload(file),
.opts = RCurl::curlOptions(httpheader = c(Authorization = paste('Client-ID', key)),
cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))
resp <- httr::POST(url = "https://api.imgur.com/3/image.xml",
config = httr::add_headers(Authorization = paste("Client-ID", key)),
body = list(image = httr::upload_file(file)))
httr::stop_for_status(resp, task = "Fail to upload")
info_media <- httr::parse_media(resp$headers[["Content-Type"]])
if (info_media$complete != "text/xml") stop("Fail to upload; response is not XML")
res <- xml2::as_list(
xml2::read_xml(x = httr::content(resp, as = "raw"))
)
res = XML::xmlToList(res)
if (is.null(res$link)) stop('failed to upload ', file)
structure(res$link, XML = res)
if (is.null(res$link[[1]])) stop('failed to upload ', file)
structure(res$link[[1]], XML = res)
}

0 comments on commit 0751802

Please sign in to comment.