Last active
May 3, 2017 18:45
-
-
Save leobarone/cab91b8c3a1ef439513c6e0962d9a412 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(dplyr, quietly = T) | |
library(data.table, quietly = T) | |
library(purrr, quietly = T) | |
library(rvest, quietly = T) | |
get_tse <- function(url_repo){ | |
temp_dir <- tempdir() | |
temp_file <- tempfile() | |
message("Downloading data from TSE repository") | |
download.file(url_repo, temp_file, quiet = T) | |
message("Data has been downloaded from TSE repository") | |
unzip(temp_file, exdir = temp_dir) | |
file_names_vec <- file.path(temp_dir, grep(".txt", list.files(temp_dir), value = T)) | |
safe_fread <- safely(fread) | |
file_names_vec %>% | |
map(function(x) safe_fread(x, encoding = "Latin-1", header = F)) %>% | |
map("result") %>% | |
reduce(bind_rows) | |
} | |
filiaweb_consulta <- function(identificador){ | |
filiaweb_url <- "http://filiaweb.tse.jus.br/filiaweb/filiacao/certidao/consulta.seam" | |
filiaweb_session <- html_session(filiaweb_url) | |
filiaweb_session$url <- filiaweb_url | |
filiaweb_form <- html_form(read_html(filiaweb_url))[[1]] | |
filiaweb_form <- set_values(filiaweb_form, | |
'j_id206:inscricao:inscricaoInput' = identificador) | |
filiaweb_submission <- submit_form(filiaweb_session, filiaweb_form, "j_id206:gerarCertidao") | |
inscricao <- filiaweb_submission %>% | |
html_nodes(xpath = '//*[@id="j_id205"]/ul/li[2]/p') %>% | |
html_text() %>% | |
trimws() | |
status <- filiaweb_submission %>% | |
html_nodes(xpath = '//*[@id="j_id205"]/p[1]/strong') %>% | |
html_text() %>% | |
trimws() | |
if(length(status) == 0) { | |
message("Titulo de Eleitor inexistente") | |
return(NULL) | |
} | |
nome <- filiaweb_submission %>% | |
html_nodes(xpath = '//*[@id="j_id205"]/ul/li[1]/p/text()') %>% | |
html_text() %>% | |
trimws() | |
filiacoes <- filiaweb_submission %>% | |
html_node(xpath = "//table[@id = 'j_id205:dataTable']") | |
if(length(filiacoes) == 0){ | |
return(data.frame(inscricao = inscricao, | |
nome = nome[2], | |
status = status, | |
sequencia = NA, | |
partido = NA, | |
uf = NA, | |
municipio = NA, | |
data_filiacao = NA)) | |
} | |
filiacoes <- html_table(filiacoes) | |
names(filiacoes) <- c("sequencia", "partido", "uf", "municipio", "data_filiacao") | |
data.frame(inscricao = inscricao, | |
nome = nome[2], | |
status = status, | |
filiacoes, | |
stringsAsFactors = F) | |
} | |
# Exemplo com meu titulo de eleitor | |
filiaweb_consulta("282745130116") | |
filiaweb_consulta("input com erro") | |
filiaweb_consulta("005778612410") | |
# Exemplo com candidatos em 2014 | |
url_cand14 <- "http://agencia.tse.jus.br/estatistica/sead/odsele/consulta_cand/consulta_cand_2014.zip" | |
cand14 <- get_tse(url_cand14) | |
titulos <- cand14 %>% | |
filter(V10 == "DEPUTADO FEDERAL", | |
V45 == "ELEITO" | V45 == "ELEITO POR MÉDIA" | V45 == "ELEITO POR QP") %>% | |
select(V28) %>% | |
as_vector() | |
resultado <- titulos[1:20] %>% | |
map_df(filiaweb_consulta) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment