From 395d1fc2b394a04b1f80fea5c4c7555e4e7ddacf Mon Sep 17 00:00:00 2001 From: JBGruber Date: Tue, 8 Oct 2024 16:13:29 +0200 Subject: [PATCH] add auth_check function --- R/auth_check.r | 38 ++++++++++++++++++++++++++++++++++++++ R/auth_hidden.r | 3 ++- R/utils.R | 2 +- man/auth_check.Rd | 31 +++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 R/auth_check.r create mode 100644 man/auth_check.Rd diff --git a/R/auth_check.r b/R/auth_check.r new file mode 100644 index 0000000..eeb8a74 --- /dev/null +++ b/R/auth_check.r @@ -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) +} diff --git a/R/auth_hidden.r b/R/auth_hidden.r index 4aca723..ecb97f3 100644 --- a/R/auth_hidden.r +++ b/R/auth_hidden.r @@ -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/") diff --git a/R/utils.R b/R/utils.R index 25df7e8..61ada2d 100644 --- a/R/utils.R +++ b/R/utils.R @@ -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 } - diff --git a/man/auth_check.Rd b/man/auth_check.Rd new file mode 100644 index 0000000..bcccf81 --- /dev/null +++ b/man/auth_check.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/auth_check.r +\name{auth_check} +\alias{auth_check} +\title{Check whether you are authenticated} +\usage{ +auth_check(research = TRUE, hidden = TRUE, silent = FALSE) +} +\arguments{ +\item{research, hidden}{turn check on/off for the research or hidden API.} + +\item{silent}{only return if check(s) were successful, no status on the +screen} +} +\value{ +logical vector (invisible) +} +\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. +} +\examples{ +auth_check() +}