Skip to content

Commit

Permalink
fix #97: provide new keys to upload_imgur()
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed Jan 14, 2025
1 parent 60c3bb9 commit 116d689
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]", comment = c(ORCID = "0000-0003-0645-5666", URL = "https://yihui.org")),
person("Wush", "Wu", role = "ctb"),
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 13 additions & 8 deletions R/image.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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: <https://yihui.org/knitr/demo/upload/>
#' @export
#' @examples \dontrun{
Expand All @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions R/string.R
Original file line number Diff line number Diff line change
Expand Up @@ -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')
13 changes: 7 additions & 6 deletions man/upload_imgur.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 116d689

Please sign in to comment.