rurlzipgtfsgtfstools

Download files in R with URLs that do not end with the file extensions


Does anyone have a trick up there sleeve for downloading GTFS using R when the URL doesn't end with ".zip"? For instance, this works:

download.file(url = "http://www.transperth.wa.gov.au/TimetablePDFs/GoogleTransit/Production/google_transit.zip", destfile = "temp.zip")

But the following create files of the right size that will not open:

download.file(url = "http://transitfeeds.com/p/ptv/497/latest/download", destfile = "temp.zip")

download.file(url = "http://transitfeeds.com/p/ptv/497/latest/download", destfile = "temp")

I suspect there is something fundamental I need to understand about urls but I don't know where to beging looking so any pointers would be appreciated.

Cheers,

Anthony


Solution

  • Your link is probably a redirect. Try using the httr package as described here R download file redirect error

    library(httr)
    
    url <- "http://transitfeeds.com/p/ptv/497/latest/download"    
    GET(
            url = url,
            write_disk("gtfs.zip"),
            verbose()
        ) -> res
    

    I was able to download the file and open it. If it works you can remove the verbose() part.