cccneto / Ibamam

Base de dados sobre multas e autuações ambientais do Instituto Brasileiro do Meio Ambiente e dos Recursos Naturais Renováveis - IBAMA.
https://cccneto.github.io/Ibamam/
Other
12 stars 5 forks source link

get_dataset_ibamam: Error in open.connection(con, "rb"): SSL certificate problem: self signed certificate in #20

Open MatthieuStigler opened 2 years ago

MatthieuStigler commented 2 years ago

Thanks for this package!!

Trying to use get_dataset_ibamam, I get error: Error in open.connection(con, "rb"): SSL certificate problem: self signed certificate in certificate chain. This seems to be suffering form the same issue that datazoom.amazonia is facing: https://github.com/datazoompuc/datazoom.amazonia/issues/107

Unfortunately, the temporary workaround there to use options(download.file.method="curl", download.file.extra="-k -L") won't work, as get_dataset_ibamam() is using directly jsonlite::fromJSON?

Thanks!

library(Ibamam)
fines_applied_Para <- get_dataset_ibamam(dataset = "distribuidas", uf = "PA")
#> Error in open.connection(con, "rb"): SSL certificate problem: self signed certificate in certificate chain

## internal code:
texto_link <- "Arrecadacao/arrecadacaobenstutelados"
uf <- "PA"
jsonlite::fromJSON(paste0("http://dadosabertos.ibama.gov.br/dados/SICAFI/", 
                          uf, "/", texto_link, ".json"))
#> Error in open.connection(con, "rb"): SSL certificate problem: self signed certificate in certificate chain

## raw code:
tm <- tempfile()
download.file(paste0("http://dadosabertos.ibama.gov.br/dados/SICAFI/", 
                     uf, "/", texto_link, ".json"),
              destfile = tm)
#> Warning in download.file(paste0("http://dadosabertos.ibama.gov.br/dados/
#> SICAFI/", : URL 'https://dadosabertos.ibama.gov.br/dados/SICAFI/PA/Arrecadacao/
#> arrecadacaobenstutelados.json': status was 'SSL peer certificate or SSH remote
#> key was not OK'
#> Error in download.file(paste0("http://dadosabertos.ibama.gov.br/dados/SICAFI/", : cannot open URL 'http://dadosabertos.ibama.gov.br/dados/SICAFI/PA/Arrecadacao/arrecadacaobenstutelados.json'

## workaround
options(download.file.method="curl", download.file.extra="-k -L")

download.file(paste0("http://dadosabertos.ibama.gov.br/dados/SICAFI/", 
                     uf, "/", texto_link, ".json"),
              destfile = tm)

Created on 2022-06-29 by the reprex package (v2.0.1)

MatthieuStigler commented 2 years ago

Temporary workaround if someone is interested...

library(Ibamam)
fines_applied_Para <- get_dataset_ibamam(dataset = "distribuidas", uf = "PA")
#> Error in open.connection(con, "rb"): SSL certificate problem: self signed certificate in certificate chain

get_data_of_state_me <- function (uf, tipo_multa)  {
  if (tipo_multa == "arrecadadas") {
    texto_link <- "Arrecadacao/arrecadacaobenstutelados"
  }
  else if (tipo_multa == "distribuidas") {
    texto_link <- "Quantidade/multasDistribuidasBensTutelados"
  }
  else {
    stop("The argument 'tipo_multa' can receive one of the values:\n         'arrecadadas' or 'distribuidas'")
  }
  tmp <- tempfile()
  download.file(paste0("http://dadosabertos.ibama.gov.br/dados/SICAFI/", 
                       uf, "/", texto_link, ".json"),
                destfile = tmp)
  dataset <- jsonlite::fromJSON(tmp)
  dplyr::as_tibble(dataset$data)
}

options(download.file.method="curl", download.file.extra="-k -L")
assignInNamespace("get_data_of_state", get_data_of_state_me, "Ibamam")
fines_applied_Para <- get_dataset_ibamam(dataset = "distribuidas", uf = "PA")

Created on 2022-06-29 by the reprex package (v2.0.1)