Created
April 23, 2017 21:38
-
-
Save duarteguilherme/c37b0ba95a81c5bc64ec460b365cc5f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(devtools) | |
devtools::install_github('RobertMyles/congressbr') | |
library(congressbr) | |
library(tidyverse) | |
library(stringr) | |
library(lubridate) | |
`%p%` <- function(e1,e2) return(paste0(e1,e2)) | |
erros <- c() | |
download_year <- function(year) { | |
print(year) | |
dados <- cam_plenary_bills(year) | |
data_tny <- map_df(unique(dados$id_bill), cam_bill_info) | |
data_year <- NULL | |
for (i in 1:nrow(data_tny)) { | |
print(i) | |
type <- str_trim(data_tny$type_bill[i]) | |
number <- data_tny$number_bill[i] | |
year_bill <- data_tny$year_bill[i] | |
extrato <- extract_votes(type,number ,year_bill) | |
data_year <- bind_rows(data_year, extrato) | |
} | |
data_year2 <- data_year %>% | |
mutate(ano=year(dmy(date_decision))) %>% | |
filter(ano==year) | |
saveRDS(data_year, file="data_votes/data_" %p% year %p% ".rds" ) | |
} | |
extract_votes <- function(type, number, year) { | |
print(type %p% number %p% year) | |
votes <- tryCatch(cam_get_votes(type, number, year), error=function(e) e$message) | |
if ( class(votes) == "character") { | |
print(votes) | |
if ( str_detect(votes, "This is not a main bill. Download is not possible")) { | |
return(NULL) | |
} | |
else { | |
stop("nao funcionou") | |
} | |
} | |
if ( is.null(votes) ) return(NULL) | |
# Removing other types of orientation | |
orientation_removed <- colnames(votes)[grep("orient_", colnames(votes))] | |
orientation_removed <- orientation_removed[(orientation_removed!="orient_GOV")] | |
votes <- votes %>% dplyr::select(-one_of(orientation_removed)) | |
return(votes) | |
} | |
map(2003:2011, download_year) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment