Skip to content

Commit

Permalink
remove docker param
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminguinaudeau committed Nov 29, 2020
1 parent 2538ed8 commit 0f24e87
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 21 deletions.
10 changes: 5 additions & 5 deletions R/comment.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#' @export
tk_comment <- function(post_id, verbose = T, vpn = F, docker = F){
tk_comment <- function(post_id, verbose = T, vpn = F){

response <- tibble::tibble()
count <- sample(30:50, 1)
Expand All @@ -10,11 +10,11 @@ tk_comment <- function(post_id, verbose = T, vpn = F, docker = F){
cursor <- seq(max_cursor - 1000, max_cursor - 1, 50)

urls <- get_url("comment", query_1 = post_id, n = count, cursor = cursor)
fins <- get_signature(urls, docker = docker)
fins <- get_signature(urls)

index <- 1
while(has_more & index <= 20){
res <- get_data(url = fins[index], docker = docker, vpn = vpn, cookie = Sys.getenv("TIKTOK_ID_COOKIE"))
res <- get_data(url = fins[index], vpn = vpn, cookie = Sys.getenv("TIKTOK_ID_COOKIE"))

if(res$status_code == "8"){
stop("Please update your logged in cookie.")
Expand Down Expand Up @@ -52,7 +52,7 @@ tk_comment <- function(post_id, verbose = T, vpn = F, docker = F){

# Need to be maintained
#' @export
tk_reply <- function(comment_id, post_id, id_cookie, docker = NULL, verbose = T, time_out = 10){
tk_reply <- function(comment_id, post_id, id_cookie, verbose = T, time_out = 10){

response <- tibble::tibble()
count <- sample(50:100, 1)
Expand All @@ -63,7 +63,7 @@ tk_reply <- function(comment_id, post_id, id_cookie, docker = NULL, verbose = T,
cursor <- seq(max_cursor - 1000, max_cursor - 1, 50)
cat("\rCursor: ", max_cursor, " Comments: ", nrow(response))
urls <- get_url("reply", query_1 = comment_id, query_2 = post_id, n = count, cursor = cursor)
fins <- get_signature(urls, docker = docker)
fins <- get_signature(urls)

index <- 1
while(has_more & index <= 20){
Expand Down
2 changes: 0 additions & 2 deletions R/get_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ get_n <- function(scope, n = 10000, start_date = lubridate::dmy("01-01-1900"), c
#' @export
get_data <- function(url, parse = T, vpn = F, cookie = "", time_out = 10){



if(!stringr::str_detect(url, "&_signature=")){
final_url <- get_signature(url)
} else {
Expand Down
40 changes: 26 additions & 14 deletions tests/testthat/test-api.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ if (Sys.getenv("USER") != "travis") {
test_that("tk_init and signature", {

expect_error(tk_info(scope = "user", query = "willsmith"), "Tiktokr was not initialized. Please run tk_init()")
Sys.setenv("TIKTOK_DOCKER" = "")
tk_init()
expect_gt(stringr::str_length(get_signature("")), 16)
Sys.setenv("TIKTOK_DOCKER" = "TRUE")
tk_init()
Sys.setenv("TIKTOK_DOCKER" = "")

})

tk_init_docker()


tk_auth(cookie = Sys.getenv("TIKTOK_COOKIE_TEST"), id_cookie = Sys.getenv("TIKTOK_ID_COOKIE_TEST"),
ua = 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Mobile/15E148 Safari/604.1')
Expand All @@ -32,28 +36,30 @@ if (Sys.getenv("USER") != "travis") {

test_that("signature Puppetteer process", {

# Puppetteer Signature

sig <- get_signature("http://tiktok.com/trends", docker = F)
Sys.setenv("TIKTOK_DOCKER" = "TRUE")
sig <- get_signature("http://tiktok.com/trends")
expect_true(stringr::str_detect(sig, "&_signature="))
expect_true(stringr::str_detect(sig, "(?<=&_signature=).{10}"))

})

test_that("signature docker process", {

# Docker Signature
sig <- get_signature("http://tiktok.com/trends", docker = T)
Sys.setenv("TIKTOK_DOCKER" = "TRUE")
sig <- get_signature("http://tiktok.com/trends")
expect_true(stringr::str_detect(sig, "&_signature="))
expect_true(stringr::str_detect(sig, "(?<=&_signature=).{10}"))


# What happens if container is off

if(length(system("docker ps -a -f 'name=tiktoksignature'", intern = T)) == 2){
system("docker stop tiktoksignature", intern = T)
}
expect_message(get_signature("http://tiktok.com/trends", docker = T), "Container was stopped. Starting container")
expect_silent(get_signature("http://tiktok.com/trends", docker = T))

expect_message(get_signature("http://tiktok.com/trends"), "Container was stopped. Starting container")
expect_silent(get_signature("http://tiktok.com/trends"))
Sys.setenv("TIKTOK_DOCKER" = "")
})

## Captcha ----
Expand Down Expand Up @@ -109,12 +115,14 @@ if (Sys.getenv("USER") != "travis") {

# Docker User

user_post <- tk_posts(scope = "user", query = "willsmith", n = 40, docker = T)
Sys.setenv("TIKTOK_DOCKER" = "TRUE")
user_post <- tk_posts(scope = "user", query = "willsmith", n = 40)
expect_gt(nrow(user_post), 2)
expect_true("desc" %in% names(user_post))
expect_gt(ncol(user_post), 90)
user_post_ <- tk_posts(scope = "user", query = "willsmith", n = 100, docker = T)
user_post_ <- tk_posts(scope = "user", query = "willsmith", n = 100)
expect_gt(nrow(dplyr::anti_join(user_post_, user_post, by = "id")), 0)
Sys.setenv("TIKTOK_DOCKER" = "")

# Wrong User ID

Expand Down Expand Up @@ -168,12 +176,14 @@ if (Sys.getenv("USER") != "travis") {

# Hashtag Docker

hashtag <- tk_posts(scope = "hashtag", query = "maincharacter", n = 25, docker = T)
Sys.setenv("TIKTOK_DOCKER" = "TRUE")
hashtag <- tk_posts(scope = "hashtag", query = "maincharacter", n = 25)
expect_gt(nrow(hashtag), 20)
expect_true("desc" %in% names(hashtag))
expect_gt(ncol(hashtag), 60)
hashtag_ <- tk_posts(scope = "hashtag", query = "maincharacter", n = 60, docker = T)
hashtag_ <- tk_posts(scope = "hashtag", query = "maincharacter", n = 60)
expect_gt(nrow(dplyr::anti_join(hashtag_, hashtag, by = "id")), 0)
Sys.setenv("TIKTOK_DOCKER" = "")

# Wrong Hashtag

Expand Down Expand Up @@ -224,12 +234,14 @@ if (Sys.getenv("USER") != "travis") {

# Docker Signature

music <- tk_posts(scope = "music", query = "6782187241935505410", n = 25, docker = T)
Sys.setenv("TIKTOK_DOCKER" = "TRUE")
music <- tk_posts(scope = "music", query = "6782187241935505410", n = 25)
expect_gt(nrow(music), 20)
expect_true("desc" %in% names(music))
expect_gt(ncol(music), 60)
music_ <- tk_posts(scope = "music", query = "6782187241935505410", n = 60, docker = T)
music_ <- tk_posts(scope = "music", query = "6782187241935505410", n = 60)
expect_gt(nrow(dplyr::anti_join(music_, music, by = "id")), 0)
Sys.setenv("TIKTOK_DOCKER" = "")


# Wrong Music ID
Expand Down

0 comments on commit 0f24e87

Please sign in to comment.