Skip to content

Commit

Permalink
add auth_check function
Browse files Browse the repository at this point in the history
  • Loading branch information
JBGruber committed Oct 8, 2024
1 parent 76fd13e commit 395d1fc
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
38 changes: 38 additions & 0 deletions R/auth_check.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#' Check whether you are authenticated
#'
#' @description \ifelse{html}{\figure{api-both.svg}{options:
#' alt='[Both]'}}{\strong{[Both]}}
#'
#' Check if the necessary token or cookies are stored on your computer
#' already. By default, the function checks for the authentication of the
#' research and hidden API. To learn how you can authenticate, look at the
#' vignette for the research (\code{vignette("research-api", package =
#' "traktok")}) or hidden (\code{vignette("unofficial-api", package =
#' "traktok")}) API.
#'
#' @param research,hidden turn check on/off for the research or hidden API.
#' @param silent only return if check(s) were successful, no status on the
#' screen
#'
#' @return logical vector (invisible)
#' @export
#'
#' @examples
#' auth_check()
auth_check <- function(research = TRUE, hidden = TRUE, silent = FALSE) {
auth <- vector()
if (research) {
if (!isFALSE(get_token(auth = FALSE))) {
auth <- c(research = TRUE)
if (!silent) cli::cli_alert_success("Research API authenticated")
}
}
if (hidden) {
cookies <- try(cookiemonster::get_cookies("^(www.)*tiktok.com"))
if (is.data.frame(cookies) && "tt_chain_token" %in% cookies$name) {
auth <- c(auth, hidden = TRUE)
if (!silent) cli::cli_alert_success("Hidden API authenticated")
}
}
invisible(auth)
}
3 changes: 2 additions & 1 deletion R/auth_hidden.r
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ auth_hidden <- function(cookiefile, live = interactive()) {
"Supply either a cookiefile (see {.url https://jbgruber.github.io/traktok/",
"articles/unofficial-api.html#authentication})"
)
if (live) {
if (live && isTRUE(utils::askYesNo("Do you want to try live authentication using Chrome? (experimental)"))) {

rlang::check_installed("rvest", reason = "to use this function", version = "1.0.4")

sess <- rvest::read_html_live("https://www.tiktok.com/")
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ as_tibble_onerow <- function(l) {
tibble::as_tibble(l)
}


is_datetime <- function(x) {
methods::is(x, "POSIXct") +
methods::is(x, "POSIXlt") +
methods::is(x, "Date") > 0
}

31 changes: 31 additions & 0 deletions man/auth_check.Rd

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

0 comments on commit 395d1fc

Please sign in to comment.