R/bt_load_site.R

Defines functions bt_load_site

Documented in bt_load_site

# Roxygen documentation

#' An updated version of b_load_site
#'
#' Waits for the specified xpath node to be present in the HTML of webdriver's
#' current page.
#'
#' Requires webdriver to be in use, assigned to an object called 'remDr'.
#'
#'
#' @param xpath Provide the XPATH for which the function should wait.
#' @param wait Specify the number of seconds you want to wait. Default is 60.
#' @param abort Specifiy whether to cause an error in case path is not find
#'   after waiting time. Default is FALSE.
#'
#' @return Pauses the algorithm until XPATH appears.
#' @references www.globaltradealert.org



# Function infos and parameters  --------------------------------------------
bt_load_site = function(xpath=NULL,
                        wait=30,
                        abort=F,
                        wait.interval=1.5,
                        assign.html=F){

  html.load= htmlParse(remDr$getSource()[[1]], asText=T)
  message("Loading site...")

  refreshed=FALSE
  t=Sys.time()
  cat("waiting")

  safety = 0 # using Sys.time() not reliable on server
  while(refreshed==F
        & as.numeric(Sys.time()-t)<=wait
        & safety<=wait){
    cat(".")
    Sys.sleep(wait.interval)
    html.load = htmlParse(remDr$getSource()[[1]], asText=T)

    refreshed=length(xpathSApply(html.load, xpath, xmlValue))>0
    safety = safety+1

  }

  cat("\n")

  if(refreshed){

    message("Site's fresh!")
    if(assign.html){
      assign("html", html.load, envir=.GlobalEnv)
    }

  }else {

    if(abort){
      stop("Site does not respond")
    }else{
      message("Site does not respond")
    }

  }




  # legacy version
  # html <- htmlParse(remDr$getSource()[[1]], asText=T)
  # load=length(xpathSApply(html, xpath, xmlValue))
  #
  # sleep=1
  # while(load==0){
  #   print(paste("waiting ", sleep, sep=""))
  #   Sys.sleep(1)
  #
  #   html <- htmlParse(remDr$getSource()[[1]], asText=T)
  #   load=length(xpathSApply(html, xpath, xmlValue))
  #   sleep=sleep+1
  #
  #   if(sleep>wait){
  #
  #     if(abort){
  #       stop("site does not respond")
  #     }else{
  #       print("site does not respond")
  #     }
  #
  #     load=1
  #   }
  # }
  # print("loading complete")
}
global-trade-alert/gtabastiat documentation built on June 4, 2023, 6:40 a.m.