R/download.r

Defines functions Download

Documented in Download

#' @include utils.r
NULL

#' Download files retryable
#'
#' @param URLs character/list. URLs to be downloaded.
#' @param names character/list. File names. Default \code{seq(URLs)}
#' @param method character. Method to be used for downloading files, equal to \code{download.file}. Default 'libcurl'
#' @param sleep numeric. Retry interval (second). Default 2
#' @param outdir character. Output directory.
#' @param verbose logical. Print progress. Default TRUE
#'
#' @importFrom utils download.file
#'
#' @return if success, return TRUE
#' @export
#'
#' @examples
#' \donttest{
#' Download('http://ftp.ensembl.org/pub', 'test.html')
#' file.remove('test.html')
#' }
#'
Download = function(URLs, names = NULL, method = NULL, sleep = NULL, outdir = NULL, verbose = TRUE){

  # check parameter #
  URLs   = as.character(unlist(URLs))
  names  = as.character(unlist(names) %|||% seq(URLs))
  method = as.character(method %|||% 'libcurl')
  sleep  = as.numeric(sleep)   %|||% 2
  outdir = as.character(outdir %|||% getwd())
  dir.create(outdir, FALSE, TRUE)

  # set index
  log = 'Download.log'
  writeLines('ok', log)

  for (i in seq(URLs)) {
    name = names[i]
    URL  = URLs[i]

    # check file
    if(!length(list.files(outdir, paste0('^', name, '$')))) {

      # download
      if(verbose) cat('-->', timer(), i, ': downloading for', name, '<--\n')
      tryCatch(download.file(URL, paste0(outdir, '/', name), method, TRUE),
               error = function(e) {
                 writeLines('Erro', log)
                 warning('--! ', timer(), ' ', i, ': download fail for ', name, ' !--') })

      # check index
      Log = readLines(log)
      if('ok' %in% Log & verbose) cat('-->', timer(), i, ': download ok for', name, '<--\n')

      # retry
      while ('Erro' %in% Log){
        writeLines('ok', log)
        tryCatch(download.file(URL, destfile = paste0(outdir, '/', name), method, TRUE),
                 error = function(e){
                   writeLines('Erro', log)
                   warning('--! ', timer(), ' ', i, ': download fail for ', name, ' !--') })

        # check index
        Log = readLines(log)
        if('ok' %in% Log & verbose) cat('-->', timer(), i, ': download ok for', name, '<--\n')

        # sleep
        Sys.sleep(sleep)
      }
    } else warning('--! ', timer(), ' ', i, ': file already exist for ', name, ' !--')
  }

  # return
  if(verbose) cat('-->', timer(), 'done <--\n')
  file.remove(log)
}
HatsuneCode/TrustVDJ documentation built on Aug. 13, 2022, 9:36 p.m.