diff --git a/DESCRIPTION b/DESCRIPTION index 3020709..4c93019 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: xfun Type: Package Title: Supporting Functions for Packages Maintained by 'Yihui Xie' -Version: 0.50.4 +Version: 0.50.5 Authors@R: c( person("Yihui", "Xie", role = c("aut", "cre", "cph"), email = "xie@yihui.name", comment = c(ORCID = "0000-0003-0645-5666", URL = "https://yihui.org")), person("Wush", "Wu", role = "ctb"), diff --git a/NEWS.md b/NEWS.md index 748eeed..09927a7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,8 @@ - `tojson()` supports more types of data now, and will indent sub-elements for lists. See the help page [`?xfun::tojson`](https://git.yihui.org/xfun/manual.html#sec:man-tojson) for details. +- Updated the default API key for `upload_imgur()` since the previous default key stopped working for some reason (thanks, @camille-s @jennybc, #97). + - `upload_imgur()` shows the error message instead of the error code when it fails to upload the image, which is more informative. # CHANGES IN xfun VERSION 0.50 diff --git a/R/image.R b/R/image.R index 05ddf1d..df7dc51 100644 --- a/R/image.R +++ b/R/image.R @@ -173,8 +173,8 @@ shrink_images = function( #' @param file Path to the image file to be uploaded. #' @param key Client ID for Imgur. It can be set via either the global option #' `xfun.upload_imgur.key` or the environment variable -#' `R_XFUN_UPLOAD_IMGUR_KEY` (see [xfun::env_option()]). If neither is set, -#' this uses a client ID registered by Yihui Xie. +#' `R_XFUN_UPLOAD_IMGUR_KEY` (see [env_option()]). If neither is set, this +#' uses a client ID registered by Yihui Xie. #' @param use_curl Whether to use the R package \pkg{curl} to upload the image. #' If `FALSE`, the system command `curl` will be used. #' @param include_xml Whether to include the XML response in the returned value. @@ -183,9 +183,10 @@ shrink_images = function( #' from Imgur (it will be parsed by \pkg{xml2} if available). See Imgur API in #' the references. #' @author Yihui Xie, adapted from the \pkg{imguR} package by Aaron Statham -#' @note Please register your own Imgur application to get your client ID; you -#' can certainly use mine, but this ID is in the public domain so everyone has -#' access to all images associated to it. +#' @note Please register your own Imgur application to get your client ID. You +#' can certainly use mine, but this ID is in the public domain and it might +#' reach Imgur's rate limit if too many people are using it in the same time +#' period or someone is trying to upload too many images at once. #' @references A demo: #' @export #' @examples \dontrun{ @@ -201,10 +202,14 @@ shrink_images = function( #' options(xfun.upload_imgur.key = 'your imgur key') #' } upload_imgur = function( - file, key = env_option('xfun.upload_imgur.key', '9f3460e67f308f6'), - use_curl = loadable('curl'), include_xml = FALSE + file, key = env_option('xfun.upload_imgur.key'), use_curl = loadable('curl'), + include_xml = FALSE ) { - if (!is.character(key)) stop('The Imgur API Key must be a character string!') + if (is.null(key) || key == '' || key == '9f3460e67f308f6') { + i = floor(as.numeric(Sys.time()) %% 3) + 1 + key = c("qyv(sq y 't)ws(", 'q (t"vxs%t "wr(', "xpyvq%vp'rwx)yq")[i] + key = decrypt(key, '1987052436fedcba') + } api = 'https://api.imgur.com/3/image.xml' hdr = paste('Authorization: Client-ID', key) if (use_curl) { diff --git a/R/string.R b/R/string.R index 062afce..c37337d 100644 --- a/R/string.R +++ b/R/string.R @@ -486,3 +486,11 @@ html_view = function(x, ...) { } one_string = function(x, ...) paste(x, ..., collapse = '\n') + +# en/decrypt a string via a character map (old and new must be 16 unique hex chars) +.crypt = function(x, old, new) { + x2 = chartr(old, new, as.character(charToRaw(x))) + rawToChar(as.raw(strtoi(x2, 16L))) +} +encrypt = function(x, key) .crypt(x, '0123456789abcdef', key) +decrypt = function(x, key) .crypt(x, key, '0123456789abcdef') diff --git a/man/upload_imgur.Rd b/man/upload_imgur.Rd index 427b672..0d9be03 100644 --- a/man/upload_imgur.Rd +++ b/man/upload_imgur.Rd @@ -6,7 +6,7 @@ \usage{ upload_imgur( file, - key = env_option("xfun.upload_imgur.key", "9f3460e67f308f6"), + key = env_option("xfun.upload_imgur.key"), use_curl = loadable("curl"), include_xml = FALSE ) @@ -16,8 +16,8 @@ upload_imgur( \item{key}{Client ID for Imgur. It can be set via either the global option \code{xfun.upload_imgur.key} or the environment variable -\code{R_XFUN_UPLOAD_IMGUR_KEY} (see \code{\link[=env_option]{env_option()}}). If neither is set, -this uses a client ID registered by Yihui Xie.} +\code{R_XFUN_UPLOAD_IMGUR_KEY} (see \code{\link[=env_option]{env_option()}}). If neither is set, this +uses a client ID registered by Yihui Xie.} \item{use_curl}{Whether to use the R package \pkg{curl} to upload the image. If \code{FALSE}, the system command \code{curl} will be used.} @@ -40,9 +40,10 @@ document with \pkg{knitr}: you can set the \verb{knitr::opts_knit$set(upload.fun any more, and it is ready to be published online. } \note{ -Please register your own Imgur application to get your client ID; you -can certainly use mine, but this ID is in the public domain so everyone has -access to all images associated to it. +Please register your own Imgur application to get your client ID. You +can certainly use mine, but this ID is in the public domain and it might +reach Imgur's rate limit if too many people are using it in the same time +period or someone is trying to upload too many images at once. } \examples{ \dontrun{