2016-09-08 3 views
1

プロジェクトの場合、これらのファイルに基づいてインディケータを作成するために、異なるWebサイトから定期的にデータファイルをダウンロードする必要があります。R - curl - 変更時にのみリモートファイルをダウンロード

これらのファイルの更新頻度が大きく異なるため、リモートファイルが更新されたかどうかを効率的に検出する方法を探しています。

以下は、カールの-Iオプションを使用することをお勧めします。これはカールパッケージの使用方法をどのように変換しますか?似

何か:

https://superuser.com/questions/619592/get-modification-time-of-remote-file-over-http-in-bash-script

代替ソリューションは、ファイルサイズやmodifcation日のいずれかのヘッダを解析するように見える

PHP: Remote file size without downloading file

(小さなファイル付き)以下の私の試みを、ただし、完全なファイルをダウンロードします。

library(curl) 


req <- curl_fetch_memory("http://www.pcr.uu.se/digitalAssets/124/124932_1ucdponesided2015.rdata") 
str(req) 
object.size(req) 
parse_headers(req$headers) 

curlパッケージでヘッダーのみをダウンロードするか、冗長ダウンロードを避けるためのオプションを指定することができますか?

答えて

1

ファイルの最終更新日の履歴を保持しておく必要があります(Webサーバーのレポートが一貫していると仮定します)。ダウンロードする前にhttr::HEAD()で確認してください(つまり、その最終変更値はおそらくURLを持つデータフレーム内にあります)。

library(httr) 

URL <- "http://www.pcr.uu.se/digitalAssets/124/124932_1ucdponesided2015.rdata" 

#' Download a file only if it hasn't changed since \code{last_modified} 
#' 
#' @param URL url of file 
#' @param fil path to write file 
#' @param last_modified \code{POSIXct}. Ideally, the output from the first 
#'  successful run of \code{get_file()} 
#' @param overwrite overwrite the file if it exists? 
#' @param .verbose output a message if the file was unchanged? 
get_file <- function(URL, fil, last_modified=NULL, overwrite=TRUE, .verbose=TRUE) { 

    if ((!file.exists(fil)) || is.null(last_modified)) { 
    res <- GET(URL, write_disk(fil, overwrite)) 
    return(httr::parse_http_date(res$headers$`last-modified`)) 
    } else if (inherits(last_modified, "POSIXct")) { 
    res <- HEAD(URL) 
    cur_last_mod <- httr::parse_http_date(res$headers$`last-modified`) 
    if (cur_last_mod != last_modified) { 
     res <- GET(URL, write_disk(fil, overwrite)) 
     return(httr::parse_http_date(res$headers$`last-modified`)) 
    } 
    if (.verbose) message(sprintf("'%s' unchanged since %s", URL, last_modified)) 
    return(last_modified) 
    } 

} 

# first run == you don't know the last-modified date. 
# you need to pair this with the URL in some data structure for later use. 
last_mod <- get_file(URL, basename(URL)) 

class(last_mod) 
## [1] "POSIXct" "POSIXt" 

last_mod 
## [1] "2015-11-16 17:34:06 GMT" 

last_mod <- get_file(URL, basename(URL), last_mod) 
#> 'http://www.pcr.uu.se/digitalAssets/124/124932_1ucdponesided2015.rdata' unchanged since 2015-11-16 17:34:06 
関連する問題