From ca92e4d1fec536c158a9fc4933308ce1dc28a9ce Mon Sep 17 00:00:00 2001 From: msperlin Date: Sun, 1 May 2022 10:03:29 -0300 Subject: [PATCH] fixes #5 (issue with api limits) --- R/yf_get.R | 17 +++++++++++++++++ R/yf_get_single_ticker.R | 3 +-- tests/testthat/test-yf_collection.R | 22 ++++++++++++++-------- tests/testthat/test-yf_get.R | 3 +++ 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/R/yf_get.R b/R/yf_get.R index 3e9dcf7..d0836b4 100644 --- a/R/yf_get.R +++ b/R/yf_get.R @@ -150,6 +150,23 @@ yf_get <- function(tickers, # disable dplyr group message options(dplyr.summarise.inform = FALSE) + # check if using do_parallel = TRUE + # 20220501 Yahoo finance started setting limits to api calls, which + # invalidates the use of any parallel computation + if (do_parallel) { + my_message <- stringr::str_glue( + "Since 2022-04-25, Yahoo Finance started to set limits to api calls, ", + "resulting in 401 errors. When using parallel computations for fetching ", + "data, the limit is reached easily. Said that, the parallel option is now", + " disabled by default. Please set do_parallel = FALSE to use this function.", + "\n\n", + "Returning empty dataframe.") + + cli::cli_alert_danger(my_message) + return(data.frame()) + + } + # first screen msgs if (!be_quiet) { diff --git a/R/yf_get_single_ticker.R b/R/yf_get_single_ticker.R index 97b610b..4fbddce 100644 --- a/R/yf_get_single_ticker.R +++ b/R/yf_get_single_ticker.R @@ -11,8 +11,7 @@ yf_get_single_ticker <- function(ticker, be_quiet = FALSE, thresh_bad_data) { if (!be_quiet) { - my_msg <- set_cli_msg("({i_ticker}/{length_tickers}) Fetching data for ', - '{ticker}") + my_msg <- set_cli_msg("({i_ticker}/{length_tickers}) Fetching data for {ticker}") cli::cli_alert_info(my_msg) } diff --git a/tests/testthat/test-yf_collection.R b/tests/testthat/test-yf_collection.R index 1421c9d..e45b229 100644 --- a/tests/testthat/test-yf_collection.R +++ b/tests/testthat/test-yf_collection.R @@ -10,24 +10,30 @@ test_that("Test of yf_get_available_indices()", { test_that("Test of yf_get_index_comp()", { - if (!covr::in_covr()) { - skip_if_offline() - skip_on_cran() # too heavy for cran - } + if (!covr::in_covr()) { + skip_if_offline() + skip_on_cran() # too heavy for cran + } - available_indices <- yf_get_available_indices() + available_indices <- yf_get_available_indices() - for (i_index in available_indices) { + for (i_index in available_indices) { - expect_true(tibble::is_tibble(yf_get_index_comp(i_index))) + expect_true(tibble::is_tibble(yf_get_index_comp(i_index))) - } + } }) test_that("Test of yf_collection_get()", { + # 20220501 yf now sets api limits, which invalidates any parallel computation + skip( + paste0("Skipping since parallel is not supported due to YF api limits, ", + "and collections are large datasets for single session download.") + ) + # parallel test for collections n_workers <- floor(parallel::detectCores()/2) future::plan(future::multisession, workers = n_workers) diff --git a/tests/testthat/test-yf_get.R b/tests/testthat/test-yf_get.R index c8947d3..8fef00a 100644 --- a/tests/testthat/test-yf_get.R +++ b/tests/testthat/test-yf_get.R @@ -66,6 +66,9 @@ test_that(desc = "Test of yf_get()", { test_that(desc = "Test of yf_get(): do_parallel = TRUE", { + # 20220501 yf now sets api limits, which invalidates any parallel computation + skip("Skipping since parallel is not supported due to YF api limits") + if (!covr::in_covr()) { skip_if_offline() skip_on_cran() # too heavy for cran