Toniiiio / sivis

Turn browser clicks into reproducible scraping code.
10 stars 1 forks source link

Capture httr::verbose without printing to console #11

Open Toniiiio opened 4 years ago

Toniiiio commented 4 years ago

Code looks like:

    verbose_log_name <- "verbose3.log"
    con <- file(verbose_log_name)
    sink(file = con, append = TRUE)
    sink(con, append = TRUE, type = "message")

    sivis$resp_content[[sivis$url]] <- tryCatch(expr = suppressMessages(
      POST(
        url = sivis$url,
        httr::add_headers(.headers = sivis$headers),
        body = sivis$hdr_body,
        #httr::verbose(),
        httr::timeout(sivis$timeout)
      )
    ),
    error = function(e){
      warning(glue::glue("GET request with headers failed with {e}. Will attempt request without headers next."))
      return(NULL)
    })

    sink()
    sink(type = "message")
    sivis$verboseLog <- verbose_log_name %>% readLines %>% paste(collapse = "\n")
    unlink(verbose_log_name)

But that either prints to console or if not added it does not write to file.