New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor use_course() #276

Closed
jennybc opened this Issue Feb 21, 2018 · 1 comment

Comments

Projects
None yet
1 participant
@jennybc
Member

jennybc commented Feb 21, 2018

Some code shared by @jeroen in a Slack conversation about use_course()

library(curl)

download_with_cd <- function(url, destdir = "."){
  h <- new_handle(noprogress = FALSE)
  tmp <- tempfile()
  curl::curl_fetch_disk(url, tmp, handle = h)
  headers <- curl::parse_headers_list(handle_data(h)$headers)
  filename <- stringr::str_match(headers[["content-disposition"]], "filename=.*")[1]
  filename <- sub("filename=", "", filename)
  outfile <- normalizePath(file.path(destdir, filename), mustWork = FALSE)
  file.rename(tmp, outfile)
  return(outfile)
}

download_with_cd("https://cran.ocpu.io/ggplot2/data/diamonds/csv", destdir = "~")

Other notes:

  • If you use curl_fetch_disk you can check the status code also from handle_data().
  • Using curl_download() might be easier -- automatically throws an error if the result is not http 200
  • the trick is all functions in curl take a handle, and you can always use handle_data() to access the metadata after the request

@jennybc jennybc added the feature label Feb 21, 2018

@jennybc

This comment has been minimized.

Show comment
Hide comment
@jennybc

jennybc Mar 8, 2018

Member

Progress-wise, at the very minimum, we should print “starting download”. Even better would be to access curl's progress reporting. cc @hadley

Member

jennybc commented Mar 8, 2018

Progress-wise, at the very minimum, we should print “starting download”. Even better would be to access curl's progress reporting. cc @hadley

jennybc added a commit that referenced this issue Jun 1, 2018

Refactor use_course() and gain progress reporting
Fixes #276

Thanks @jeroen for all the pointers!

Key things:

  * Explicit use of a handle is how we hook into progress
    reporting, even though curl_download() doesn't expose
  * curl_download() has a built-in check for http status 200
  * Using a custom progressfunction because default doesn't
    always look right in RStudio
  * Switch to fs functions for file system work

@jennybc jennybc closed this in #380 Jun 1, 2018

jennybc added a commit that referenced this issue Jun 1, 2018

Refactor use_course() and gain progress reporting (#380)
* Refactor use_course() and gain progress reporting

Fixes #276

Thanks @jeroen for all the pointers!

Key things:

  * Explicit use of a handle is how we hook into progress
    reporting, even though curl_download() doesn't expose
  * curl_download() has a built-in check for http status 200
  * Using a custom progressfunction because default doesn't
    always look right in RStudio
  * Switch to fs functions for file system work

* Newline management

* Add NEWS bullet

* Wording
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment