Skip to content

Commit

Permalink
fix cran test fail
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchypark committed Feb 17, 2024
1 parent f29d84d commit c0ba6b5
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 70 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ importFrom(httr2,resp_body_string)
importFrom(httr2,url_parse)
importFrom(jsonlite,fromJSON)
importFrom(magrittr,"%>%")
importFrom(purrr,when)
importFrom(rvest,html_attr)
importFrom(rvest,html_node)
importFrom(rvest,html_nodes)
Expand Down
15 changes: 6 additions & 9 deletions R/getCategory.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,29 +81,26 @@ getMainCategory <- function() {
#' }
getSubCategory <- function(sid1 = 100) {
httr2::request("http://news.naver.com") %>%
httr2::req_url_path("main/main.naver") %>%
httr2::req_url_query(mode = "LSD") %>%
httr2::req_url_query(mid = "shm") %>%
httr2::req_url_query(sid1 = sid1) %>%
httr2::req_url_path("section") %>%
httr2::req_url_path_append(sid1) %>%
httr2::req_user_agent("N2H4 by chanyub.park <[email protected]>") %>%
httr2::req_perform() %>%
httr2::resp_body_html() -> hobj

hobj %>%
rvest::html_nodes("div.snb ul.nav li a") %>%
rvest::html_nodes("a.ct_snb_nav_item_link") %>%
rvest::html_text() %>%
trimws() -> titles

links <- rvest::html_nodes(hobj, "div.snb ul.nav li a")
links <- rvest::html_nodes(hobj, "a.ct_snb_nav_item_link")
links <- rvest::html_attr(links, "href")
links <- paste0("http://news.naver.com", links)

urls <-
tibble::tibble(sub_cate_name = titles,
url = links)
urls <- urls[grep("sid2=", urls$url),]
sid2 <- sapply(strsplit(urls$url, "="), function(x) {
x[5]
sid2 <- sapply(strsplit(urls$url, "/"), function(x) {
x[7]
})
urls <-
tibble::tibble(sub_cate_name = urls$sub_cate_name,
Expand Down
41 changes: 25 additions & 16 deletions R/getComment.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ getAllComment <- function(turl) {
get_comment(turl, "all", "df")
}

#' @importFrom purrr when
#' @importFrom httr2 req_perform resp_body_string
#' @importFrom jsonlite fromJSON
get_comment <- function(turl,
Expand All @@ -49,22 +48,30 @@ get_comment <- function(turl,
. <- NULL
type <- match.arg(type)

count %>%
purrr::when(. == "all" ~ "all",
!is.numeric(.) ~ "error",
. > 100 ~ "over",
. <= 100 ~ "base",
~ "error") -> count_case
if (count == "all") {
count_case <- "all"
} else if (!is.numeric(count)) {
count_case <- "error"
} else if (count > 100) {
count_case <- "over"
} else if (count <= 100) {
count_case <- "base"
} else {
count_case <- "error"
}

if (count_case == "error") {
stop(paste0("count param can accept number or 'all'. your input: ", count))
}

turl <- get_real_url(turl)

count_case %>%
purrr::when(. == "base" ~ count,
~ 100) %>%
result <- 100
if (count_case == "base") {
result <- count
}

result %>%
req_build_comment(turl, ., NULL) %>%
httr2::req_perform() %>%
httr2::resp_body_string() %>%
Expand All @@ -78,12 +85,14 @@ get_comment <- function(turl,
return(transform_return(dat, type))
}

purrr::when(count_case == "all" ~ total,
total >= count ~ count,
total < count ~ {
warning("Request more than the actual total count, and use actual total count.")
total
}) -> tarsize
if(count_case == "all") {
tarsize <- total
} else if(total >= count) {
tarsize <- count
} else if(total < count) {
warning("Request more than the actual total count, and use actual total count.")
tarsize <- total
}

res <- list()
res[[1]] <- transform_return(dat, "df")
Expand Down
39 changes: 24 additions & 15 deletions R/getCommentHistory.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ getAllCommentHistory <- function(turl,
get_comment_history(turl, commentNo, "all", "df")
}

#' @importFrom purrr when
#' @importFrom httr2 req_perform
get_comment_history <- function(turl,
commentNo,
Expand All @@ -51,21 +50,29 @@ get_comment_history <- function(turl,
. <- .x <- NULL
type <- match.arg(type)

count %>%
purrr::when(. == "all" ~ "all",!is.numeric(.) ~ "error",
. > 100 ~ "over",
. <= 100 ~ "base",
~ "error") -> count_case
if (count == "all") {
count_case <- "all"
} else if (!is.numeric(count)) {
count_case <- "error"
} else if (count > 100) {
count_case <- "over"
} else if (count <= 100) {
count_case <- "base"
} else {
count_case <- "error"
}

if (count_case == "error") {
stop(paste0("count param can accept number or 'all'. your input: ", count))
}

turl <- get_real_url(turl)

count_case %>%
purrr::when(. == "base" ~ count,
~ 100) %>%
result <- 100
if (count_case == "base") {
result <- count
}
result %>%
req_build_comment_history(turl, commentNo, ., NULL) %>%
httr2::req_perform() %>%
httr2::resp_body_string() %>%
Expand All @@ -79,12 +86,14 @@ get_comment_history <- function(turl,
return(transform_return(dat, type))
}

purrr::when(count_case == "all" ~ total,
total >= count ~ count,
total < count ~ {
warning("Request more than the actual total count, and use actual total count.")
total
}) -> tarsize
if(count_case == "all") {
tarsize <- total
} else if(total >= count) {
tarsize <- count
} else if(total < count) {
warning("Request more than the actual total count, and use actual total count.")
tarsize <- total
}

res <- list()
res[[1]] <- transform_return(dat, "df")
Expand Down
3 changes: 3 additions & 0 deletions R/getMaxPageNum.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
#' @export
#' @importFrom rvest html_node html_text
#' @importFrom httr2 request req_url_query req_method req_perform resp_body_html
#' @keywords internal
#' @examples
#' \dontrun{
#' getMaxPageNum("https://news.naver.com/main/list.naver?mode=LS2D&mid=shm&sid1=103&sid2=376")
#' }

getMaxPageNum <- function(turl, max = 100) {
lifecycle::deprecate_soft("1.0.0", "when()", I("`if`"))

httr2::request(turl) %>%
httr2::req_url_query(page = max) %>%
httr2::req_method("GET") %>%
Expand Down
1 change: 1 addition & 0 deletions R/getUrlList.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#' @export
#' @importFrom rvest html_nodes html_attr html_text
#' @importFrom httr2 request req_user_agent req_method req_perform resp_body_html
#' @keywords internal
#' @examples
#' \dontrun{
#' getUrlList("https://news.naver.com/main/list.naver?mode=LS2D&mid=shm&sid1=103&sid2=376")
Expand Down
Binary file modified R/sysdata.rda
Binary file not shown.
3 changes: 1 addition & 2 deletions cran-comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@

0 errors ✔ | 0 warnings ✔ | 2 notes ✖

* I all the time honest. leak of English. cran policy must translate other langauge.
English is not only langauge in the earth. I remove cache function in package.

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

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

1 change: 1 addition & 0 deletions man/getMaxPageNum.Rd

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

1 change: 1 addition & 0 deletions man/getUrlList.Rd

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

28 changes: 2 additions & 26 deletions tests/testthat/test-func.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test_that("getAllComment", {
skip_on_cran()
url <-
"https://n.news.naver.com/mnews/article/015/0002303155?sid=100"
test <- expect_warning(getAllComment(url))
test <- getAllComment(url)
expect_equal(test$contents, c("test", "test2", "test"))
})

Expand All @@ -48,7 +48,7 @@ test_that("getAllCommentHistory", {
url <-
"https://n.news.naver.com/mnews/article/001/0009205077?sid=102"
test <- getComment(url)
dat <- expect_warning(getAllCommentHistory(url, test$commentNo))
dat <- getAllCommentHistory(url, test$commentNo)
expect_equal(dat$contents, c("test", "test2","test","test"))
})

Expand Down Expand Up @@ -95,27 +95,3 @@ test_that("getSubCategory", {
test <- getSubCategory()
expect_equal(test$sid2[1], "264")
})

test_that("getMaxPageNum", {
skip_on_cran()
url <-
"https://news.naver.com/main/list.naver?sid2=254&sid1=102&mid=shm&mode=LS2D&date=20170427"
test <- getMaxPageNum(url)
expect_equal(test, 1)

url <- "https://news.naver.com/main/list.naver?mode=LS2D&mid=shm&sid2=260&sid1=101&date=20220901"
test <- getMaxPageNum(url)
expect_equal(test, 23)

url <- "https://news.naver.com/main/list.naver?mode=LS2D&sid2=265&sid1=100&mid=shm&date=20220829"
test <- getMaxPageNum(url)
expect_equal(test, 108)
})

test_that("getUrlList", {
skip_on_cran()
url <-
"https://news.naver.com/main/list.naver?sid2=267&sid1=100&mid=shm&mode=LS2D&date=20170101"
test <- getUrlList(url)
expect_identical(dim(test), c(20L, 2L))
})

0 comments on commit c0ba6b5

Please sign in to comment.