· 6 years ago · Nov 18, 2019, 06:04 AM
1#' Create a new paste
2#'
3#' @md
4#' @param text of paste
5#' @param name name/title of paste
6#' @param format hint for syntax highlighting. Defaults to `text`. See
7#' [the detail page](https://pastebin.com/api#5) for more info.
8#' @param impersonate if `TRUE` then `PASTEBIN_USER` and `PASTEBIN_PASSWORD` _must_ be set
9#' in order to generate a user key to be applied with the API key. Don't blame me,
10#' blame [pastebin](https://pastebin.com/api#8).
11#' @param visibility one of `public`, `unlisted` or `private`. Defaults to `public`
12#' @param expires either `n` for never or an abbreviated time expiration string in the form
13#' of a digit (the "number of") and a units character `m` for minute(s),
14#' `d` for day(s), `w` for week(s). Defaults to `n` (never). See
15#' [the detail page](https://pastebin.com/api#6) for more info.
16#' @param pastebin_key pastebin API key
17#' @note The maximum size a paste can be is 512 kilobytes (0.5 megabytes). Pro members are
18#' allowed to create pastes up to 10 megabytes.
19#' @export
20new_paste <- function(text, name=NULL, format="text", impersonate=FALSE,
21 visibility=c("public", "unlisted", "private"),
22 expires="n", pastebin_key=pastebin_api_key()) {
23
24 expires <- gsub(" ", "", toupper(expires))
25
26 visibility <- match.arg(visibility, c("public", "unlisted", "private"))
27 visibility <- which(visibility == c("public", "unlisted", "private"))
28
29 params <- list(api_dev_key=pastebin_key,
30 api_option="paste",
31 api_paste_code=text,
32 api_paste_name=name,
33 api_paste_format=format,
34 api_user_key="",
35 api_paste_expire_date=expires,
36 api_paste_private=visibility)
37
38 if (impersonate) {
39
40 httr::POST("https://pastebin.com/api/api_login.php",
41 body=list(api_dev_key=pastebin_key,
42 api_user_name=Sys.getenv("PASTEBIN_USER"),
43 api_user_password=Sys.getenv("PASTEBIN_PASSWORD")),
44 encode="form") -> u_res
45
46 httr::stop_for_status(u_res)
47
48 params$api_user_key <- httr::content(u_res, as="text", encoding="UTF-8")
49
50 }
51
52 httr::POST("https://pastebin.com/api/api_post.php", body=params, encode="form") -> res
53
54 httr::stop_for_status(res)
55
56 httr::content(res, as="text", encoding="UTF-8")
57
58}
59
60
61© 2019 GitHub, Inc.