Skip to content

Commit

Permalink
edit get_secret
Browse files Browse the repository at this point in the history
  • Loading branch information
mkearney committed Feb 13, 2020
1 parent 84f7338 commit 53cbc8c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@
^\.github/workflows/pkgdown\.yaml$
^\.github$
^\.github/workflows/R-CMD-check\.yaml$
^tweetbotornot2\.Rcheck$
^tweetbotornot2.*\.tar\.gz$
^tweetbotornot2.*\.tgz$
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
README_cache
inst/doc
working.R
tweetbotornot2.Rcheck/
tweetbotornot2*.tar.gz
tweetbotornot2*.tgz
47 changes: 35 additions & 12 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,52 @@ prep_xgb_model <- function() {
xgboost::xgb.Booster.complete(tweetbotornot_xgb_model)
}

get_secret <- function(x) {
get_secret <- function(x, ...) {
stopifnot(
is.character(x),
length(x) == 1L
)
if ((key <- Sys.getenv(x)) != "") {
return(key)
}
if (grepl("(TOKEN|KEY)$", x)) {
x <- paste0(sub("_(KEY|PAT|TOKEN|SECRET)$", "", x), c("", "_KEY", "_PAT", "_TOKEN"))
} else {
x <- paste0(sub("_(KEY|PAT|TOKEN|SECRET)$", "", x), c("", "_KEY", "_PAT", "_SECRET"))
e <- Sys.getenv()
x <- grep(x, names(e), ..., value = TRUE)
if (length(x) == 0) {
return("")
}
if (length(x) > 1L) {
warning("Found multiple environment variables")
x <- x[1]
}
Sys.getenv(x)
}

check_token_or_create_from_access_keys <- function(token = NULL) {
if (!is.null(token)) {
return(token)
}
x <- Sys.getenv(x)
if (any(x != "")) {
return(x[x != ""][1])
if (eval(parse(text = 'exists("twitter_tokens", envir = rtweet:::.state)'))) {
return(token)
}
""
create_token_from_secrets()
}

create_token_from_secrets <- function() {
if (file.exists("rtweet_token.rds") &&
!isFALSE(x <- tryCatch(readRDS("rtweet_token.rds"), error = function(e) FALSE))) {
return(x)
}
if (file.exists("rtweet_token.rds") &&
!isFALSE(x <- tryCatch(readRDS("rtweet_token.rds"), error = function(e) FALSE))) {
if (file.exists(".rtweet_token.rds") &&
!isFALSE(x <- tryCatch(readRDS(".rtweet_token.rds"), error = function(e) FALSE))) {
return(x)
}
home <- normalizePath("~")
if (file.exists(file.path(home, ".rtweet_token.rds")) &&
!isFALSE(x <- tryCatch(readRDS(file.path(home, ".rtweet_token.rds")), error = function(e) FALSE))) {
return(x)
}
if (file.exists(file.path(home, "rtweet_token.rds")) &&
!isFALSE(x <- tryCatch(readRDS(file.path(home, "rtweet_token.rds")), error = function(e) FALSE))) {
return(x)
}
rtweet_token()
Expand Down Expand Up @@ -230,7 +253,7 @@ cleanup_users_string <- function(x) {

rtweet_token <- function(access_token = NULL, access_secret = NULL) {
if (is.null(access_token)) {
access_token <- unname(get_secret("TWITTER_ACCESS_TOKEN"))
access_token <- unname(get_secret("TWITTER_ACCESS_(TOKEN|KEY)"))
}
if (is.null(access_secret)) {
access_secret <- unname(get_secret("TWITTER_ACCESS_SECRET"))
Expand Down

0 comments on commit 53cbc8c

Please sign in to comment.