Sunday, December 14, 2014

Downloading Binary Files in a List

Downloading Binary Files

I was trying to figure out how to download video files from a website using SSL. At first I tried using download.file, but that didn’t work. After some digging, I found the package “downloader” which enables you to download files from SSL sites easily. Once the package is downloaded and installed, it’s simple to download the file as shown:

myurl<-"https://echodelivery.cvm.iastate.edu:8443/ess/echo/presentation/b650602b-b7a8-4e01-8ff7-aa06b6b66cc9/mediacontent.m4v"
require(downloader)
#mode="wb" indicates it's a binary file.  see ?download for more options
download(myurl, destfile="C:\\Users\\Mark\\Desktop\\STAT_510\\MyNewVideoName.m4v", mode="wb")

I then took this a step further and placed all the URLs into a csv file, along with their desired filename on my workstation after they were downloaded. The following code then reads in the csv file, and loops through each file, downloading it and saving it to the “path” directory

#CSV file containg fields: lecture_number, url, and filname
myfiles<-read.csv("c:\\users\\mark\\ClassVideos.csv", as.is = TRUE)

#directory where the files will be saved
path<-"C:\\Users\\Mark\\Desktop\\STAT_510\\"

#loop through files and download the url to path with filename=filname
for (i in 1:dim(myfiles)[i]){
  #note mode="wb" indicates this is a binary file
  download(url=myfiles[i,'url'], destfile=paste(path, myfiles[i,'filname'], sep=""), mode="wb")
  print(i)  
}

That’s about it!

No comments:

Post a Comment