R/publishers.R

Defines functions is_publisher_available translate_publisher openes_load_publishers

Documented in is_publisher_available openes_load_publishers translate_publisher

#' Request all available publishers from \url{https://datos.gob.es/}
#'
#' @return a \code{\link[tibble]{tibble}} with two columns: publisher_code and publishers
#' @export
#'
#' @examples
#'
#'\donttest{
#' openes_load_publishers()
#'}
openes_load_publishers <- function() {
  # Specify random huge number just to avoid running out of pages
  # in the future.
  resp <- get_resp_paginated(path_publishers(), 1000)
  # Delete the publisher URL because they ALL lead to the same
  # URL which is this one http://datos.gob.es/recurso/sector-publico/org/Organismo#data
  list_tibbles <- lapply(resp$result$items, function(x) tibble::as_tibble(x[-1]))
  publisher_df <- Reduce(rbind, list_tibbles)
  names(publisher_df) <- c('publisher_code', 'publisher')
  publisher_df[!duplicated(publisher_df$publisher_code), ]
}

#' Available publishers that `opendataes` can read
#'
#' @return a \code{\link[tibble]{tibble}} with two columns: publishers and publisher_code
#'
#' @seealso \code{\link{openes_load_publishers}}
#' @export
#'
#' @examples
#'
#' publishers_available
publishers_available <- tibble::tibble(
  publishers = c("Ayuntamiento de Barcelona", "Ayuntamiento de Madrid", "Ayuntamiento de Valencia",
                 "Ayuntamiento de Las Palmas de Gran Canaria", "Ayuntamiento de Bilbao",
                 "Ayuntamiento de Zaragoza", "Ayuntamiento de Malaga", "Ayuntamiento de Gijon",
                 "Ayuntamiento de Santander", "Ayuntamiento de Alcobendas", "Junta de Castilla y Leon"),
  publisher_code = c("L01080193", "L01280796", "L01462508", "L01350167", "L01480209", "L01502973",
                     "L01290672", "L01330241", "L01390759","L01280066", "A07002862")
)

#' Translate publisher code to publisher name
#'
#' @param code A publisher code
translate_publisher <- function(code) {
  all_publishers <- openes_load_publishers()
  all_publishers
  index <- which(all_publishers$publisher_code == code)
  if (length(index) == 0) return("Publisher not available")
  all_publishers$publisher[index]
}

#' Check if publisher is available in opendataes
#'
#' @param data_list A data_list similar to resp$result$items[[1]] that contains information on a dataset
is_publisher_available <- function(data_list) {
  publisher_code <- tolower(extract_publisher_code(data_list))
  res <- ifelse(publisher_code %in% tolower(publishers_available$publisher_code),
                TRUE, FALSE)

  res
}
rOpenSpain/opendataes documentation built on April 18, 2021, 4:55 p.m.