Skip to content

Commit

Permalink
Attempt to (re-)establish a session up to 5 times
Browse files Browse the repository at this point in the history
The attempt to establish a session could result in a curl handle that
does not have a cookie, in which case the user will not be able to
import data.

Establish a connection and check if it resulted in a cookie. Try again,
up to 5 times, if it does not. Throw an error if the attempt fails 5
times.

Hopefully most users will not receive errors because of this, combined
with the second attempt to import data if the first attempt fails,

See #157.
  • Loading branch information
joshuaulrich committed May 23, 2017
1 parent 97e4df6 commit d54e593
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions R/getSymbols.R
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,29 @@ formals(loadSymbols) <- loadSymbols.formals
h <- get0("_handle_", .quantmodEnv)

if (is.null(h) || force.new) {
tmp <- tempfile()
on.exit(unlink(tmp))

# create 'h' if it doesn't exist yet
if (!force.new) {
h <- list()
}

# establish session
h$ch <- curl::new_handle()
curl::curl_download("https://finance.yahoo.com", tmp, handle = h$ch)
new.session <- function(h) {
tmp <- tempfile()
on.exit(unlink(tmp))

for (i in 1:5) {
curl::curl_download("https://finance.yahoo.com", tmp, handle = h)
if (NROW(curl::handle_cookies(h)) > 0)
break;
}

if (NROW(curl::handle_cookies(h)) == 0)
stop("Could not establish session after 5 attempts.")

return(h)
}

h$ch <- new.session(curl::new_handle())

n <- if (unclass(Sys.time()) %% 1L >= 0.5) 1L else 2L
query.srv <- paste0("https://query", n, ".finance.yahoo.com/",
Expand Down

0 comments on commit d54e593

Please sign in to comment.