| Title: | Collecting 'TikTok' Data |
|---|---|
| Description: | Getting 'TikTok' data (<https://www.tiktok.com/>) through the official and unofficial APIs—in other words, you can track 'TikTok'. |
| Authors: | Johannes B. Gruber [aut, cre] (ORCID: <https://orcid.org/0000-0001-9177-1772>) |
| Maintainer: | Johannes B. Gruber <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.1.1.9000 |
| Built: | 2026-05-12 08:04:41 UTC |
| Source: | https://github.com/jbgruber/traktok |
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, see the
research API vignette
or hidden API vignette.
You can also view these locally with vignette("research-api", package = "traktok") and vignette("unofficial-api", package = "traktok").
auth_check(research = TRUE, hidden = TRUE, silent = FALSE, fail = FALSE)auth_check(research = TRUE, hidden = TRUE, silent = FALSE, fail = FALSE)
research,
|
turn check on/off for the research or hidden API. |
silent |
only return if check(s) were successful, no status on the screen |
fail |
fail if even basic authentication for the hidden API is missing. |
logical vector (invisible)
auth_check() au <- auth_check() if (isTRUE(au["research"])) { message("Ready to use the research API!") } if (isTRUE(au["hidden"])) { message("Ready to use all function of unofficial the API!") }auth_check() au <- auth_check() if (isTRUE(au["research"])) { message("Ready to use the research API!") } if (isTRUE(au["hidden"])) { message("Ready to use all function of unofficial the API!") }
Guides you through authentication for the Research API
auth_research(client_key, client_secret)auth_research(client_key, client_secret)
client_key |
Client key for authentication |
client_secret |
Client secret for authentication |
You need to apply for access to the API and get the key and secret from TikTok. See https://developers.tiktok.com/products/research-api/ for more information.
An authentication token (invisible).
## Not run: auth_research(client_key, client_secret) ## End(Not run)## Not run: auth_research(client_key, client_secret) ## End(Not run)
If tt_search_api or tt_comments_api fail after already getting
several pages, you can use this function to get all videos that have been
retrieved so far from memory. Does not work when the session has crashed. In
that case, look in tempdir() for an RDS file as a last resort.
last_query() last_comments()last_query() last_comments()
a list of unparsed videos or comments.
Print a traktok query as a tree
## S3 method for class 'traktok_query' print(x, ...)## S3 method for class 'traktok_query' print(x, ...)
x |
An object of class |
... |
Additional arguments passed to |
nothing. Prints traktok query.
query() |> query_and(field_name = "hashtag_name", operation = "EQ", field_values = "rstats") |> print()query() |> query_and(field_name = "hashtag_name", operation = "EQ", field_values = "rstats") |> print()
Print a traktok search results
## S3 method for class 'tt_results' print(x, ...)## S3 method for class 'tt_results' print(x, ...)
x |
An object of class |
... |
not used. |
nothing. Prints search results.
Create a traktok query from the given parameters.
query(and = NULL, or = NULL, not = NULL) query_and(q, field_name, operation, field_values) query_or(q, field_name, operation, field_values) query_not(q, field_name, operation, field_values)query(and = NULL, or = NULL, not = NULL) query_and(q, field_name, operation, field_values) query_or(q, field_name, operation, field_values) query_not(q, field_name, operation, field_values)
and, or, not
|
A list of AND/OR/NOT conditions. Must contain one
or multiple lists with |
q |
A traktok query created with |
field_name |
The field name to query against. One of: "create_date", "username", "region_code", "video_id", "hashtag_name", "keyword", "music_id", "effect_id", "video_length". |
operation |
One of: "EQ", "IN", "GT", "GTE", "LT", "LTE". |
field_values |
A vector of values to search for. |
TikTok's query consists of rather complicated lists dividing query elements into AND, OR and NOT:
and: The and conditions specify that all the conditions in the list must be met
or: The or conditions specify that at least one of the conditions in the list must be met
not: The not conditions specify that none of the conditions in the list must be met
The query can be constructed by writing the list for each entry
yourself, like in the first example. Alternatively, traktok
provides convenience functions to build up a query using
query_and, query_or, and query_not, which
make building a query a little easier. You can learn more at
https://developers.tiktok.com/doc/research-api-specs-query-videos#query.
A traktok query.
## Not run: # using query directly and supplying the list query(or = list( list( field_name = "hashtag_name", operation = "EQ", field_values = "rstats" ), list( field_name = "keyword", operation = "EQ", field_values = list("rstats", "API") ) )) # starting an empty query and building it up using the query_* functions query() |> query_or(field_name = "hashtag_name", operation = "EQ", field_values = "rstats") |> query_or(field_name = "keyword", operation = "IN", field_values = c("rstats", "API")) ## End(Not run)## Not run: # using query directly and supplying the list query(or = list( list( field_name = "hashtag_name", operation = "EQ", field_values = "rstats" ), list( field_name = "keyword", operation = "EQ", field_values = list("rstats", "API") ) )) # starting an empty query and building it up using the query_* functions query() |> query_or(field_name = "hashtag_name", operation = "EQ", field_values = "rstats") |> query_or(field_name = "keyword", operation = "IN", field_values = c("rstats", "API")) ## End(Not run)
tt_comments_api( video_id, fields = "all", start_cursor = 0L, max_pages = 1L, cache = TRUE, verbose = interactive(), token = NULL ) tt_comments( video_id, fields = "all", start_cursor = 0L, max_pages = 1L, cache = TRUE, verbose = interactive(), token = NULL )tt_comments_api( video_id, fields = "all", start_cursor = 0L, max_pages = 1L, cache = TRUE, verbose = interactive(), token = NULL ) tt_comments( video_id, fields = "all", start_cursor = 0L, max_pages = 1L, cache = TRUE, verbose = interactive(), token = NULL )
video_id |
The id or URL of a video |
fields |
The fields to be returned (defaults to all) |
start_cursor |
The starting cursor, i.e., how many results to skip (for picking up an old search). |
max_pages |
results are returned in batches/pages with 100 videos. How many should be requested before the function stops? |
cache |
should progress be saved in the current session? It can then be
retrieved with |
verbose |
should the function print status updates to the screen? |
token |
The authentication token (usually supplied automatically after running auth_research once). |
A data.frame of parsed comments.
## Not run: tt_comments("https://www.tiktok.com/@tiktok/video/7106594312292453675") # OR tt_comments("7106594312292453675") # OR tt_comments_api("7106594312292453675") ## End(Not run)## Not run: tt_comments("https://www.tiktok.com/@tiktok/video/7106594312292453675") # OR tt_comments("7106594312292453675") # OR tt_comments_api("7106594312292453675") ## End(Not run)
Get usernames of users who follows a user (tt_get_follower) or get who a user is following (tt_get_following).
tt_get_follower(...) tt_get_following(...)tt_get_follower(...) tt_get_following(...)
... |
arguments passed to tt_user_follower_api or
tt_get_follower_hidden. To use the research API, include |
a data.frame of followers and following of users
tt_playlist_api(playlist_id, verbose = interactive(), token = NULL) tt_playlist(playlist_id, verbose = interactive(), token = NULL)tt_playlist_api(playlist_id, verbose = interactive(), token = NULL) tt_playlist(playlist_id, verbose = interactive(), token = NULL)
playlist_id |
playlist ID or URL to a playlist. |
verbose |
should the function print status updates to the screen? |
token |
The authentication token (usually supplied automatically after running auth_research once). |
A data.frame video metadata.
Searches videos using either the Research API (if an authentication token is present, see auth_research) or otherwise the unofficial hidden API. See tt_search_api or tt_search_hidden respectively for information about these functions.
tt_search(...)tt_search(...)
... |
arguments passed to tt_search_api or
tt_search_hidden. To use the research API, include |
a data.frame of video metadata
This is the version of tt_search that explicitly uses Research API. Use tt_search_hidden for the unofficial API version.
tt_search_api( query, start_date = Sys.Date() - 1, end_date = Sys.Date(), fields = "all", start_cursor = 0L, search_id = NULL, is_random = FALSE, max_pages = 1, parse = TRUE, cache = TRUE, verbose = interactive(), token = NULL ) tt_query_videos( query, start_date = Sys.Date() - 1, end_date = Sys.Date(), fields = "all", start_cursor = 0L, search_id = NULL, is_random = FALSE, max_pages = 1, parse = TRUE, cache = TRUE, verbose = interactive(), token = NULL )tt_search_api( query, start_date = Sys.Date() - 1, end_date = Sys.Date(), fields = "all", start_cursor = 0L, search_id = NULL, is_random = FALSE, max_pages = 1, parse = TRUE, cache = TRUE, verbose = interactive(), token = NULL ) tt_query_videos( query, start_date = Sys.Date() - 1, end_date = Sys.Date(), fields = "all", start_cursor = 0L, search_id = NULL, is_random = FALSE, max_pages = 1, parse = TRUE, cache = TRUE, verbose = interactive(), token = NULL )
query |
A query string or object (see query). |
start_date, end_date
|
A start and end date to narrow the search (required; can be a maximum of 30 days apart). |
fields |
The fields to be returned (defaults to all) |
start_cursor |
The starting cursor, i.e., how many results to skip (for picking up an old search). |
search_id |
The search id (for picking up an old search). |
is_random |
Whether the query is random (defaults to FALSE). |
max_pages |
results are returned in batches/pages with 100 videos. How many should be requested before the function stops? |
parse |
Should the results be parsed? Otherwise, the original JSON object is returned as a nested list. |
cache |
should progress be saved in the current session? It can then be
retrieved with |
verbose |
should the function print status updates to the screen? |
token |
The authentication token (usually supplied automatically after running auth_research once). |
A data.frame of parsed TikTok videos (or a nested list).
## Not run: # look for a keyword or hashtag by default tt_search_api("rstats") # or build a more elaborate query query() |> query_and(field_name = "region_code", operation = "IN", field_values = c("JP", "US")) |> query_or(field_name = "hashtag_name", operation = "EQ", # rstats is the only hashtag field_values = "rstats") |> query_or(field_name = "keyword", operation = "IN", # rstats is one of the keywords field_values = "rstats") |> query_not(operation = "EQ", field_name = "video_length", field_values = "SHORT") |> tt_search_api() # when a search fails after a while, get the results and pick it back up # (only work with same parameters) last_pull <- last_query() query() |> query_and(field_name = "region_code", operation = "IN", field_values = c("JP", "US")) |> query_or(field_name = "hashtag_name", operation = "EQ", # rstats is the only hashtag field_values = "rstats") |> query_or(field_name = "keyword", operation = "IN", # rstats is one of the keywords field_values = "rstats") |> query_not(operation = "EQ", field_name = "video_length", field_values = "SHORT") |> tt_search_api(start_cursor = length(last_pull) + 1, search_id = attr(last_pull, "search_id")) ## End(Not run)## Not run: # look for a keyword or hashtag by default tt_search_api("rstats") # or build a more elaborate query query() |> query_and(field_name = "region_code", operation = "IN", field_values = c("JP", "US")) |> query_or(field_name = "hashtag_name", operation = "EQ", # rstats is the only hashtag field_values = "rstats") |> query_or(field_name = "keyword", operation = "IN", # rstats is one of the keywords field_values = "rstats") |> query_not(operation = "EQ", field_name = "video_length", field_values = "SHORT") |> tt_search_api() # when a search fails after a while, get the results and pick it back up # (only work with same parameters) last_pull <- last_query() query() |> query_and(field_name = "region_code", operation = "IN", field_values = c("JP", "US")) |> query_or(field_name = "hashtag_name", operation = "EQ", # rstats is the only hashtag field_values = "rstats") |> query_or(field_name = "keyword", operation = "IN", # rstats is one of the keywords field_values = "rstats") |> query_not(operation = "EQ", field_name = "video_length", field_values = "SHORT") |> tt_search_api(start_cursor = length(last_pull) + 1, search_id = attr(last_pull, "search_id")) ## End(Not run)
tt_user_follower_api( username, max_pages = 1, cache = TRUE, verbose = interactive(), token = NULL ) tt_user_following_api( username, max_pages = 1, cache = TRUE, verbose = interactive(), token = NULL )tt_user_follower_api( username, max_pages = 1, cache = TRUE, verbose = interactive(), token = NULL ) tt_user_following_api( username, max_pages = 1, cache = TRUE, verbose = interactive(), token = NULL )
username |
name(s) of the user(s) to be queried |
max_pages |
results are returned in batches/pages with 100 videos. How many should be requested before the function stops? |
cache |
should progress be saved in the current session? It can then be
retrieved with |
verbose |
should the function print status updates to the screen? |
token |
The authentication token (usually supplied automatically after running auth_research once). |
A data.frame containing follower of following account information.
## Not run: tt_user_follower_api("jbgruber") # OR tt_user_following_api("https://www.tiktok.com/@tiktok") # OR tt_get_follower("https://www.tiktok.com/@tiktok") ## End(Not run)## Not run: tt_user_follower_api("jbgruber") # OR tt_user_following_api("https://www.tiktok.com/@tiktok") # OR tt_get_follower("https://www.tiktok.com/@tiktok") ## End(Not run)
tt_user_info_api( username, fields = "all", verbose = interactive(), token = NULL ) tt_user_info(username, fields = "all", verbose = interactive(), token = NULL)tt_user_info_api( username, fields = "all", verbose = interactive(), token = NULL ) tt_user_info(username, fields = "all", verbose = interactive(), token = NULL)
username |
name(s) of the user(s) to be queried |
fields |
The fields to be returned (defaults to all) |
verbose |
should the function print status updates to the screen? |
token |
The authentication token (usually supplied automatically after running auth_research once). |
A data.frame of parsed TikTok videos the user has posted.
## Not run: tt_user_info_api("jbgruber") # OR tt_user_info_api("https://www.tiktok.com/@tiktok") # OR tt_user_info("https://www.tiktok.com/@tiktok") ## End(Not run)## Not run: tt_user_info_api("jbgruber") # OR tt_user_info_api("https://www.tiktok.com/@tiktok") # OR tt_user_info("https://www.tiktok.com/@tiktok") ## End(Not run)
tt_user_liked_videos_api( username, fields = "all", max_pages = 1, cache = TRUE, verbose = interactive(), token = NULL ) tt_get_liked( username, fields = "all", max_pages = 1, cache = TRUE, verbose = interactive(), token = NULL )tt_user_liked_videos_api( username, fields = "all", max_pages = 1, cache = TRUE, verbose = interactive(), token = NULL ) tt_get_liked( username, fields = "all", max_pages = 1, cache = TRUE, verbose = interactive(), token = NULL )
username |
name(s) of the user(s) to be queried |
fields |
The fields to be returned (defaults to all) |
max_pages |
results are returned in batches/pages with 100 videos. How many should be requested before the function stops? |
cache |
should progress be saved in the current session? It can then be
retrieved with |
verbose |
should the function print status updates to the screen? |
token |
The authentication token (usually supplied automatically after running auth_research once). |
A data.frame of parsed TikTok videos the user has posted.
## Not run: tt_get_liked("jbgruber") # OR tt_user_liked_videos_api("https://www.tiktok.com/@tiktok") # OR tt_user_liked_videos_api("https://www.tiktok.com/@tiktok") # note: none of these work because I could not find any account that # has likes public! ## End(Not run)## Not run: tt_get_liked("jbgruber") # OR tt_user_liked_videos_api("https://www.tiktok.com/@tiktok") # OR tt_user_liked_videos_api("https://www.tiktok.com/@tiktok") # note: none of these work because I could not find any account that # has likes public! ## End(Not run)
tt_user_pinned_videos_api( username, fields = "all", cache = TRUE, verbose = interactive(), token = NULL ) tt_get_pinned( username, fields = "all", cache = TRUE, verbose = interactive(), token = NULL )tt_user_pinned_videos_api( username, fields = "all", cache = TRUE, verbose = interactive(), token = NULL ) tt_get_pinned( username, fields = "all", cache = TRUE, verbose = interactive(), token = NULL )
username |
vector of user names (handles) or URLs to users' pages. |
fields |
The fields to be returned (defaults to all) |
cache |
should progress be saved in the current session? It can then be
retrieved with |
verbose |
should the function print status updates to the screen? |
token |
The authentication token (usually supplied automatically after running auth_research once). |
A data.frame of parsed TikTok videos the user has posted.
## Not run: tt_get_pinned("jbgruber") # OR tt_user_pinned_videos_api("https://www.tiktok.com/@tiktok") # OR tt_user_pinned_videos_api("https://www.tiktok.com/@tiktok") ## End(Not run)## Not run: tt_get_pinned("jbgruber") # OR tt_user_pinned_videos_api("https://www.tiktok.com/@tiktok") # OR tt_user_pinned_videos_api("https://www.tiktok.com/@tiktok") ## End(Not run)
tt_user_reposted_api( username, fields = "all", max_pages = 1, cache = TRUE, verbose = interactive(), token = NULL ) tt_get_reposted( username, fields = "all", max_pages = 1, cache = TRUE, verbose = interactive(), token = NULL )tt_user_reposted_api( username, fields = "all", max_pages = 1, cache = TRUE, verbose = interactive(), token = NULL ) tt_get_reposted( username, fields = "all", max_pages = 1, cache = TRUE, verbose = interactive(), token = NULL )
username |
name(s) of the user(s) to be queried |
fields |
The fields to be returned (defaults to all) |
max_pages |
results are returned in batches/pages with 100 videos. How many should be requested before the function stops? |
cache |
should progress be saved in the current session? It can then be
retrieved with |
verbose |
should the function print status updates to the screen? |
token |
The authentication token (usually supplied automatically after running auth_research once). |
A data.frame of parsed TikTok videos the user has posted.
## Not run: 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! ## End(Not run)## Not run: 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! ## End(Not run)
Get all videos posted by a user (or multiple user's for the Research API). Searches videos using either the Research API (if an authentication token is present, see auth_research) or otherwise the unofficial hidden API. See tt_user_videos_api or tt_user_videos_hidden respectively for information about these functions.
tt_user_videos(username, ...)tt_user_videos(username, ...)
username |
The username or usernames whose videos you want to retrieve. |
... |
Additional arguments to be passed to the |
a data.frame containing metadata of user posts.
## Not run: # Get hidden videos from the user "fpoe_at" tt_user_videos("fpoe_at") ## End(Not run)## Not run: # Get hidden videos from the user "fpoe_at" tt_user_videos("fpoe_at") ## End(Not run)
Get all videos posted by a user or multiple user's. This is a convenience
wrapper around tt_search_api that takes care of moving time
windows (search is limited to 30 days). This is the version of
tt_user_videos that explicitly uses Research API. Use
tt_user_videos_hidden for the unofficial API version.
tt_user_videos_api( username, since = "2020-01-01", to = Sys.Date(), verbose = interactive(), ... )tt_user_videos_api( username, since = "2020-01-01", to = Sys.Date(), verbose = interactive(), ... )
username |
The username or usernames whose videos you want to retrieve. |
since, to
|
limits from/to when to go through the account in 30 day windows. |
verbose |
should the function print status updates to the screen? |
... |
Additional arguments to be passed to the
|
a data.frame containing metadata of user posts.
## Not run: # Get videos from the user "fpoe_at" since October 2024 tt_user_videos_api("fpoe_at", since = "2024-10-01") # often makes sense to combine this with the account creation time from the # hidden URL fpoe_at_info <- tt_user_info_hidden(username = "fpoe_at") tt_user_videos_api("fpoe_at", since = fpoe_at_info$create_time) ## End(Not run)## Not run: # Get videos from the user "fpoe_at" since October 2024 tt_user_videos_api("fpoe_at", since = "2024-10-01") # often makes sense to combine this with the account creation time from the # hidden URL fpoe_at_info <- tt_user_info_hidden(username = "fpoe_at") tt_user_videos_api("fpoe_at", since = fpoe_at_info$create_time) ## End(Not run)