Skip to content

Commit

Permalink
implement reposted endpoint (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
JBGruber committed Oct 8, 2024
1 parent 5abe891 commit 47d8477
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 28 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export(tt_get_follower_hidden)
export(tt_get_following)
export(tt_get_following_hidden)
export(tt_get_liked)
export(tt_get_pinned)
export(tt_get_reposted)
export(tt_json)
export(tt_playlist_api)
export(tt_query_videos)
Expand All @@ -32,6 +34,7 @@ export(tt_user_info_api)
export(tt_user_info_hidden)
export(tt_user_liked_videos_api)
export(tt_user_pinned_videos_api)
export(tt_user_reposted_api)
export(tt_user_videos)
export(tt_user_videos_hidden)
export(tt_videos)
Expand Down
113 changes: 111 additions & 2 deletions R/api_research.r
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ tt_query_request <- function(endpoint,
#'
#' @examples
#' \dontrun{
#' tt_user_liked_videos_api("jbgruber")
#' tt_get_liked("jbgruber")
#' # OR
#' tt_user_liked_videos_api("https://www.tiktok.com/@tiktok")
#' # OR
Expand Down Expand Up @@ -336,6 +336,115 @@ tt_user_liked_videos_api <- function(username,
}


#' Lookup which videos were liked by a user using the research API
#'
#' @description \ifelse{html}{\figure{api-research.svg}{options: alt='[Works on:
#' Research API]'}}{\strong{[Works on: Research API]}}
#'
#' @param username name(s) of the user(s) to be queried
#' @param fields The fields to be returned (defaults to all)
#' @inheritParams tt_search_api
#'
#' @return A data.frame of parsed TikTok videos the user has posted
#' @export
#'
#' @examples
#' \dontrun{
#' tt_get_reposted("jbgruber")
#' # OR
#' tt_user_reposted_api("https://www.tiktok.com/@tiktok")
#' # OR
#' tt_user_reposted_api("https://www.tiktok.com/@tiktok")
#'
#' # note: none of these work because nobody has this enabled!
#' }
tt_user_reposted_api <- function(username,
fields = "all",
max_pages = 1,
cache = TRUE,
verbose = TRUE,
token = NULL) {

purrr::map(username, function(u) {
# if username is given as URL
if (grepl("/", u)) {
u <- extract_regex(
u,
"(?<=.com/@)(.+?)(?=\\?|$|/)"
)
}
if (verbose) cli::cli_progress_step(msg = "Getting user {u}",
msg_done = "Got user {u}")
the$result <- TRUE
if (is.null(token)) token <- get_token()

if (fields == "all") {
fields <- c(
"id",
"create_time",
"username",
"region_code",
"video_description",
"music_id",
"like_count",
"comment_count",
"share_count",
"view_count",
"hashtag_names",
"is_stem_verified",
"favourites_count",
"video_duration"
) |>
paste0(collapse = ",")
}

res <- list(data = list(has_more = TRUE, cursor = NULL))
the$page <- 0L
videos <- list()
# iterate over pages
while (purrr::pluck(res, "data", "has_more", .default = FALSE) && the$page < max_pages) {
the$page <- the$page + 1
the$cursor <- purrr::pluck(res, "data", "cursor")

res <- tt_user_request(endpoint = "reposted_videos/",
username = u,
fields = fields,
cursor = the$cursor,
token = token)

videos <- c(videos, purrr::pluck(res, "data", "reposted_videos"))
if (cache) {
the$videos <- videos
}
}

videos2 <- videos |>
purrr::map(as_tibble_onerow) |>
dplyr::bind_rows() |>
# somehow, the order changes between, calls. So I fix it here
dplyr::relocate("id",
"username",
"create_time",
"video_description",
"region_code",
"video_duration",
"view_count",
"like_count",
"comment_count",
"share_count",
"music_id")

videos <- tibble::add_column(videos, reposted_by_user = u)
if (verbose) cli::cli_progress_done(
result = ifelse(length(videos) > 1, "done", "failed")
)

return(videos)
}) |>
dplyr::bind_rows()
}


#' Lookup which videos were pinned by a user using the research API
#'
#' @description \ifelse{html}{\figure{api-research.svg}{options: alt='[Works on:
Expand All @@ -348,7 +457,7 @@ tt_user_liked_videos_api <- function(username,
#'
#' @examples
#' \dontrun{
#' tt_user_pinned_videos_api("jbgruber")
#' tt_get_pinned("jbgruber")
#' # OR
#' tt_user_pinned_videos_api("https://www.tiktok.com/@tiktok")
#' # OR
Expand Down
54 changes: 50 additions & 4 deletions R/shorthands.r
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,65 @@ tt_user_info <- tt_user_info_api
tt_get_liked <- tt_user_liked_videos_api


#' @rdname tt_user_reposted_api
#' @export
tt_get_reposted <- tt_user_reposted_api


#' @rdname tt_user_pinned_videos_api
#' @export
tt_get_pinned <- tt_user_pinned_videos_api


#' @rdname tt_comments_api
#' @export
tt_comments <- tt_comments_api


#' @rdname tt_get_following_hidden
#' Get followers and following of users
#'
#' @description \ifelse{html}{\figure{api-both.svg}{options:
#' alt='[Both]'}}{\strong{[Both]}}
#'
#' Get usernames of users who follows a user (tt_get_follower) or get who a
#' user is following (tt_get_following).
#'
#' @param ... arguments passed to \link{tt_user_follower_api} or
#' \link{tt_get_follower_hidden}. To use the research API, include \code{token}
#' (e.g., \code{token = NULL}).
#'
#' @return a data.frame
#' @export
tt_get_follower <- tt_get_follower_hidden
tt_get_follower <- function(...) {

params <- list(...)
token <- params$token
params$token <- NULL
if (is.null(token)) token <- get_token(auth = FALSE)
if (isFALSE(token)) {
tt_get_follower_hidden(...)
} else {
tt_user_follower_api(..., token)
}

}


#' @rdname tt_get_following_hidden
#' @rdname tt_get_follower
#' @export
tt_get_following <- tt_get_following_hidden
tt_get_following <- function(...) {

params <- list(...)
token <- params$token
params$token <- NULL
if (is.null(token)) token <- get_token(auth = FALSE)
if (isFALSE(token)) {
tt_get_following_hidden(...)
} else {
tt_user_following_api(..., token)
}

}


#' Get json file from a TikTok URL
Expand Down
26 changes: 26 additions & 0 deletions man/tt_get_follower.Rd

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

20 changes: 1 addition & 19 deletions man/tt_get_following_hidden.Rd

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

2 changes: 1 addition & 1 deletion man/tt_user_liked_videos_api.Rd

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

13 changes: 11 additions & 2 deletions man/tt_user_pinned_videos_api.Rd

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

60 changes: 60 additions & 0 deletions man/tt_user_reposted_api.Rd

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

0 comments on commit 47d8477

Please sign in to comment.