R/voting_info.R

#' Отримайте короткий опис політики
#'
#' @param id Вектор, який містить id депутата (має складатись щонайменше з одного елементу). За замовчуванням має значення '2524', що відповідає першому за алфавітом нардепу - Олександру Абдулліну

#'
#' @param key Ваш персональний ключ API, отриманий на rada4you.org
#'
#' @return Датафрейм, що складається з шести змінних для кожного депутата: id, mp, party, rebellions, attended, possible
#'
#' @export

voting_info <- function(id = 4896, key) {

  voting <- data.frame()
  for(i in id) {

    request <- httr::GET(url = "https://rada4you.org/",
                         path = paste0("api/v1/divisions//", i, ".json"),
                         query = list(key = key))

    if(httr::status_code(request) != 200) {
      stop(
        message(paste('Помилка. Код відповіді сервера: '), httr::status_code(request)))

    } else {

      response <- httr::content(request, as = 'text')

      json <- jsonlite::fromJSON(response)

      print(request)

      null <- function(x) {
        if(
          is.null(x)
        )
          return("NA")
        return( x )
      }

      votes <- data.frame(
        id = json$id,
        name = json$name,
        date = json$date,
        time = json$clock_time,
        aye_votes = json$aye_votes,
        no_votes = json$no_votes,
        possible_turnout = json$possible_turnout,
        rebellions = json$rebellions,
        edited = null(json$edited),
        summary = null(json$summary),
        bill_id = null(json$bills$id),
        bill_number = null(paste0("№",json$bills$official_id)),
        bill_title = null(json$bills$title),
        bill_url = null(json$bills$url),

        stringsAsFactors = F)

      voting <- rbind.data.frame(voting, votes)

      voting$date <- as.Date(voting$date)
        }

  }


  return(voting)

}
savchukidze/rada4you1 documentation built on May 9, 2019, 7:38 a.m.