Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. library(devtools)
  2. devtools::install_github('RobertMyles/congressbr')
  3. library(congressbr)
  4. library(tidyverse)
  5. library(stringr)
  6. library(lubridate)
  7.  
  8. `%p%` <- function(e1,e2) return(paste0(e1,e2))
  9. erros <- c()
  10. download_year <- function(year) {
  11. print(year)
  12. dados <- cam_plenary_bills(year)
  13. data_tny <- map_df(unique(dados$id_bill), cam_bill_info)
  14. data_year <- NULL
  15. for (i in 1:nrow(data_tny)) {
  16. print(i)
  17. type <- str_trim(data_tny$type_bill[i])
  18. number <- data_tny$number_bill[i]
  19. year_bill <- data_tny$year_bill[i]
  20. extrato <- extract_votes(type,number ,year_bill)
  21. data_year <- bind_rows(data_year, extrato)
  22. }
  23. data_year2 <- data_year %>%
  24. mutate(ano=year(dmy(date_decision))) %>%
  25. filter(ano==year)
  26.  
  27. saveRDS(data_year, file="data_votes/data_" %p% year %p% ".rds" )
  28. }
  29. extract_votes <- function(type, number, year) {
  30. print(type %p% number %p% year)
  31. votes <- tryCatch(cam_get_votes(type, number, year), error=function(e) e$message)
  32. if ( class(votes) == "character") {
  33. print(votes)
  34. if ( str_detect(votes, "This is not a main bill. Download is not possible")) {
  35. return(NULL)
  36. }
  37. else {
  38. stop("nao funcionou")
  39. }
  40. }
  41. if ( is.null(votes) ) return(NULL)
  42. # Removing other types of orientation
  43. orientation_removed <- colnames(votes)[grep("orient_", colnames(votes))]
  44. orientation_removed <- orientation_removed[(orientation_removed!="orient_GOV")]
  45. votes <- votes %>% dplyr::select(-one_of(orientation_removed))
  46. return(votes)
  47. }
  48.  
  49. map(2003:2011, download_year)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement