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