Created
August 6, 2021 14:58
-
-
Save leoniedu/161fafd4b3ef24e705e776f00cb1335b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
url_sei <- "https://sei.economia.gov.br" | |
user_sei <- "usuario aqui" | |
pwd_sei <- "senha aqui" | |
org_sei <- "0" ## se tem mais de uma organizacao no login pode ter que mudar aqui. | |
library(RSelenium) | |
library(dplyr) | |
library(rvest) | |
library(purrr) | |
library(tictoc) | |
get_users_sei <- function(x) { | |
require(rvest) | |
x <- read_html(x) | |
unidades <- x%>%html_node("#selInfraUnidades")%>%html_nodes("option") | |
unidade <- unidades[unidades%>%as.character%>%grep("selected", .)]%>%html_text | |
usuarios <- x%>%html_node("#selAtribuicao")%>%html_nodes("option") | |
values <- usuarios%>%html_attr("value")#%>%unlist | |
values <- values[!values%in%"null"] | |
names <- usuarios%>% | |
html_text%>% | |
strsplit(" - |class")%>% | |
purrr::map_dfr(function(x) tibble(user_sei=x[1], nome_sei=x[2])%>% | |
filter(!is.na(nome_sei)))%>% | |
arrange(nome_sei)%>%mutate(unidade=unidade, id_sei=values, user_sei) | |
names | |
} | |
# https://stackoverflow.com/a/56173984/143377 | |
rD <- RSelenium::rsDriver(browser = "chrome", | |
chromever = | |
system2(command = "wmic", | |
args = 'datafile where name="C:\\\\Program Files (x86)\\\\Google\\\\Chrome\\\\Application\\\\chrome.exe" get Version /value', | |
stdout = TRUE, | |
stderr = TRUE) %>% | |
stringr::str_extract(pattern = "(?<=Version=)\\d+\\.\\d+\\.\\d+\\.") %>% | |
magrittr::extract(!is.na(.)) %>% | |
stringr::str_replace_all(pattern = "\\.", | |
replacement = "\\\\.") %>% | |
paste0("^", .) %>% | |
stringr::str_subset(string = | |
binman::list_versions(appname = "chromedriver") %>% | |
dplyr::last())%>% | |
as.numeric_version() %>% | |
max() %>% | |
as.character(), port=4565L) | |
remDr <- rD[["client"]] | |
#remDr$screenshot(display=TRUE) | |
##login | |
remDr$navigate(url_sei) | |
user <- remDr$findElement(value = "txtUsuario", using = "id") | |
user$clearElement() | |
user$sendKeysToElement(list(user_sei)) | |
pwd <- remDr$findElement(value = "pwdSenha", using = "id") | |
pwd$clearElement() | |
pwd$sendKeysToElement(list(pwd_sei)) | |
org <- remDr$findElement(value = "selOrgao", using = "id") | |
org$clickElement() | |
m <- org$findChildElement(value=paste0("option[value=\"", org_sei, "\""), using='css') | |
m$clickElement() | |
login <- remDr$findElement(value="sbmLogin", using='id') | |
login$clickElement() | |
unidades <- remDr$findElement(value="selInfraUnidades", using='id') | |
unidades_sel <- unidades$selectTag() | |
unidades_disp <- unidades_sel$text | |
get_user_sei_unidade <- function(i) { | |
cat(i%>%paste0("\n")) | |
unidades <- remDr$findElement(value="selInfraUnidades", using='id') | |
unidades$clickElement() | |
m <- unidades$findChildElement(value=paste0("option[value=\"", i, "\""), using='css') | |
m$clickElement() | |
processo <- try(remDr$findElement(value="chkRecebidosItem0", using='id')) | |
if ("try-error"%in%class(processo)) processo <- try(remDr$findElement(value="chkGeradosItem0", using='id')) | |
processo$clickElement() | |
atribuir <- remDr$findElement(value="#divComandos :nth-child(3) .infraCorBarraSistema", using='css') | |
atribuir$clickElement() | |
users <- remDr$getPageSource()%>%as.character()%>%get_users_sei() | |
users | |
} | |
tic() | |
usuarios <- map_dfr(unidades_sel$value, possibly(get_user_sei_unidade, otherwise = tibble())) | |
toc() | |
unidades_missing <-(tibble(unidade=unidades_disp, unidades_value=unidades_sel$value)%>%anti_join(usuarios)) | |
stopifnot(nrow(unidades_missing)==0) | |
while (nrow(unidades_missing)!=0) { | |
usuarios_retry <- map_dfr(unidades_missing$unidades_value, possibly(get_user_sei_unidade, otherwise = tibble())) | |
usuarios <-bind_rows(usuarios, usuarios_retry) | |
unidades_missing <-(tibble(unidade=unidades_disp, unidades_value=unidades_sel$value)%>%anti_join(usuarios)) | |
} | |
usuarios%>% | |
mutate(nome_sei=trimws(nome_sei)) -> usuarios | |
remDr$close() | |
remDr$closeServer() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment