R/mps_policies.R

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

mps_policies <- function(id, key) {

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

    request <- httr::GET(url = "https://rada4you.org/",
                         path = paste0("api/v1/people/", 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)

      depbio <- data.frame(
        id = json$id,
        mp = paste(json$latest_member$name$first, json$latest_member$name$last),
        party = json$latest_member$party,
        electorate = json$latest_member$electorate,
        policy_id = json$policy_comparisons$policy$id,
        policy_name = json$policy_comparisons$policy$name,
        description = json$policy_comparisons$policy$description,
        provisional = json$policy_comparisons$policy$provisional,
        agreement = json$policy_comparisons$agreement,
        voted = json$policy_comparisons$voted,

                  stringsAsFactors = F)

      depbioall <- rbind.data.frame(depbioall, depbio)

      library(magrittr)

      depbioall$party <- depbioall$party %>%
        stringr::str_replace_all(pattern = "Фракція Політичної партії \"НАРОДНИЙ ФРОНТ\"",
                                 replacement = 'Народний фронт') %>%
        stringr::str_replace_all(pattern = "Фракція ПАРТІЇ \"БЛОК ПЕТРА ПОРОШЕНКА\"",
                                 replacement = 'Блок Петра Порошенка') %>%
        stringr::str_replace_all(pattern = "Фракція політичної партії \"Всеукраїнське об'єднання \"Батьківщина\" у Верховній Раді України",
                                 replacement = 'ВО "Батьківщина"') %>%
        stringr::str_replace_all(pattern = "Фракція Радикальної партії Олега Ляшка",
                                 replacement = 'Радикальна партія Олега Ляшка') %>%
        stringr::str_replace_all(pattern = "Група \"Воля народу\"",
                                 replacement = 'група "Воля народу"') %>%
        stringr::str_replace_all(pattern = "Фракція Політичної партії \"Об'єднання \"САМОПОМІЧ\"" ,
                                 replacement = 'Об’єднання "Самопоміч"') %>%
        stringr::str_replace_all(pattern = "Група \"Партія \"Відродження\"" ,
                                 replacement = 'група "Відродження"') %>%
        stringr::str_replace_all(pattern = "Фракція Політичної партії \"Опозиційний блок\" у Верховній Раді України восьмого скликання",
                                 replacement = 'Опозиційний блок')
      Sys.sleep(1)

      detach(package:magrittr)
    }

  }


  return(depbioall)

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