/
deputados.R
199 lines (170 loc) · 6.81 KB
/
deputados.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#' @title Retrieves details about a deputy
#' @description ID, name, birth date, birth city among other informations are returned.
#' @param id Deputy's ID
#' @param nome Deputy's name (partially or complete)
#' @param idLegislatura Id of one or more mandates of the deputy
#' @param siglaUf State's abbreviation of the deputy
#' @param siglaPartido Party's abbreviation of the deputy
#' @param siglaSexo Gender's abbreviation
#' @param itens Items quantity. '-1' returns all current deputies at the request moment
#' @param ordenarPor Element that will be used to sort the returned list
#' @return Dataframe containing details about the deputy.
#' @examples
#' abel_mesquita_info <- fetch_deputado(id = 178957)
#' @rdname fetch_deputado
#' @export
fetch_deputado <- function(id = NULL, nome = NULL, idLegislatura = NULL, siglaUf = NULL, siglaPartido = NULL,
siglaSexo = NULL, itens = NULL, ordenarPor = NULL){
parametros <- as.list(environment(), all = TRUE)
if ( !length(.verifica_parametros_entrada(parametros))) {
.camara_api(.DEPUTADOS_PATH) %>%
.assert_dataframe_completo(.COLNAMES_DEP_INFO) %>%
.coerce_types(.COLNAMES_DEP_INFO, order_cols = FALSE)
} else if ( is.null(id)) {
.fetch_using_queries(parametros, .DEPUTADOS_PATH) %>%
.assert_dataframe_completo(.COLNAMES_DEP_INFO) %>%
.coerce_types(.COLNAMES_DEP_INFO, order_cols = FALSE)
} else {
.fetch_using_id(id, .DEPUTADOS_PATH) %>%
.assert_dataframe_completo(.COLNAMES_DEP_INFO_ID) %>%
.coerce_types(.COLNAMES_DEP_INFO_ID, order_cols = FALSE)
}
}
#' @title Fetches expenditures from deputy
#' @description Fetches expenditures from deputy with his/her parlamentary quota. If no time
#' parameters are passed, the fucntion returns the expenditures in the last six months.
#' @param id Deputy's ID. Mandatory field
#' @param idLegislatura Deputy's ID
#' @param ano Expenditure's year
#' @param mes Expenditure's month
#' @param cnpjCpfFornecedor Provider's CPF or CNPJ
#' @param itens Items quantity. '-1' returns all the expenditures
#' @return Dataframe containing details about the deputy's expenditures
#' @examples
#' gastos_abel_mesquita <- fetch_despesas_deputado(id = 178957)
#' gastos_abel_junho2017 <- fetch_despesas_deputado(id = 178957, ano = 2017, mes = 06, itens = -1)
#' @rdname fetch_despesas_deputado
#' @export
fetch_despesas_deputado <- function(id = NULL, idLegislatura = NULL, ano = NULL, mes = NULL,
cnpjCpfFornecedor = NULL, itens = NULL) {
parametros <- as.list(environment(), all = TRUE)
path <- paste0(.DEPUTADOS_PATH, "/", id, "/despesas")
.camara_api(path) %>%
.assert_dataframe_completo(.COLNAMES_DEP_GASTOS) %>%
.coerce_types(.COLNAMES_DEP_GASTOS)
}
#' @title Get the state and party of an author
#' @description Return state and party
#' @param uri uri that contains data about the author
#' @return State and party
#' @export
extract_partido_estado_autor <- function(uri) {
if (!is.na(uri)) {
resp <- .get_from_api_with_exponential_backoff_cached(uri, NULL, NULL)
autor <- .get_json(resp)$dados
autor_uf <-
autor %>%
magrittr::extract2('ufNascimento')
autor_partido <-
autor %>%
magrittr::extract2('ultimoStatus') %>%
magrittr::extract2('siglaPartido')
paste0(autor_partido, '/', autor_uf)
} else {
''
}
}
#' @title Fetches details abaout all deputys
#' @description Fetches details about deputys from the 40º legislature to the current
#' @param ids_dep Dataframe containing all deputys IDs
#' @return Dataframe containing details about the deputy's
#' @rdname fetch_all_deputados
#' @export
fetch_all_deputados <- function(ids_dep) {
deputados <- tibble::tibble()
if (is.null(dim(ids_dep)) | !is.data.frame(ids_dep) ) {
warning("Objeto deve ser um dataframe nao-nulo")
} else if (nrow(ids_dep) == 0) {
warning("Dataframe vazio")
} else {
deputados <- purrr::map_df(ids_dep$id, ~(fetch_deputado(.x) %>%
dplyr::mutate_all(~ as.character(.))))
deputados <- deputados %>%
.assert_dataframe_completo(.COLNAMES_DEP_INFO_ID) %>%
.coerce_types(.COLNAMES_DEP_INFO_ID, order_cols = FALSE) %>%
.rename_df_columns()
}
deputados
}
#' @title Fetches all deputys IDs
#' @description Fetches all deputys IDs from the given legislature to the current
#' @param legislatura_base Legislatura inicial para retornar os deputados
#' @return Dataframe containing all deputys IDs
#' @rdname fetch_all_deputados
#' @export
fetch_ids_deputados <- function(legislatura_base = .LEGISLATURA_INICIAL) {
url <- paste0(.CAMARA_API_LINK, .URL_TABELA_DEP)
tabela_deputados <- readr::read_delim(
url,
delim = ";",
col_types = list(
.default = readr::col_character(),
idLegislaturaInicial = readr::col_double(),
idLegislaturaFinal = readr::col_double(),
dataNascimento = readr::col_date(format = ""),
dataFalecimento = readr::col_date(format = "")
)
) %>%
dplyr::filter(idLegislaturaInicial >= legislatura_base)
ids_dep <-
tabela_deputados %>%
dplyr::rowwise() %>%
dplyr::mutate(id = as.character(.get_id(as.character(uri)))) %>%
dplyr::select(id)
ids_dep
}
#' @title Fetches all deputys IDsfrom a given legislature
#' @description Fetches all deputys IDs from a given legislature
#' @param legislatura_id Legislature's id
#' @return Dataframe containing all deputys IDs
#' @rdname fetch_ids_deputados_by_leg
#' @export
fetch_ids_deputados_by_leg <- function(legislatura = .LEGISLATURA_ATUAL) {
path <- paste0(.DEPUTADOS_PATH, "?idLegislatura=", legislatura)
ids_dep <-
.camara_api(path)
if(nrow(ids_dep) == 0) {
warning("Não há dados para esta legislatura")
} else {
ids_dep <- ids_dep %>%
dplyr::distinct(id)
}
ids_dep
}
#' @title Fetches all fronts from deputy
#' @description Fetches all fronts from a deputy from the given legislature
#' @param id_dep Deputy's ID
#' @param legislatura_base Fronts legislature
#' @return Dataframe containing all fronts from a deputy
#' @rdname fetch_frentes_deputado
#' @export
fetch_frentes_deputado <- function(id_dep, legislatura_base = .LEGISLATURA_ATUAL) {
frentes <- tibble::tibble()
frentes <- .camara_api(paste0(.DEPUTADOS_PATH, "/", id_dep, "/frentes"))
if (nrow(frentes) == 0) {
warning("ID invalido ou deputado nao faz parte de nenhuma frente.")
} else {
frentes <- frentes %>%
dplyr::mutate(id_deputado = id_dep) %>%
dplyr::mutate(id_deputado = as.integer(id_deputado)) %>%
dplyr::rename(id_frente = id) %>%
.rename_df_columns() %>%
.assert_dataframe_completo(.COLNAMES_DEPUTADO_FRENTES) %>%
.coerce_types(.COLNAMES_DEPUTADO_FRENTES) %>%
dplyr::filter(id_legislatura == legislatura_base)
}
frentes
}
.get_id <- function(uri) {
return(tail(unlist(strsplit(uri, "/")), 1))
}