2016-09-08 6 views
1

프로젝트의 경우 다른 웹 사이트의 데이터 파일을 정기적으로 다운로드하여 해당 파일을 기반으로 표시기를 만들어야합니다.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) 

컬링 패키지로 헤더를 다운로드하거나 중복 다운로드를 피할 수있는 옵션을 지정할 수 있습니까?

답변

1

웹 서버의보고가 일관성이 있다고 가정하고 파일의 마지막 수정 날짜 기록을 유지해야하며 다운로드하기 전에 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 
관련 문제