R/crawler_cjsg.R

crawler_cjsg <- function(pag=0, livre='', ementa='', n_recurso='', n_registro='', relator='', 
                         classe='', assunto='', comarca='', orgao_julgador='', dt_julgamento='', 
                         dt_registro='', tipo_decisao='', ordenar='') { 
  form_data <- list('dados.buscaInteiroTeor'='',
                    'dados.sinonimos'='on',
                    'dados.buscaEmenta'='',
                    'dados.nuProcOrigem'='',
                    'dados.nuRegistro'='',
                    'agenteSelectedEntitiesList'='',
                    'contadoragente'='0',
                    'contadorMaioragente'='0',
                    'agentePK.cdAgente'='',
                    'nmAgente'='',
                    'classesTreeSelection.values'='',
                    'classesTreeSelection.text'='',
                    'assuntosTreeSelection.values'='',
                    'assuntosTreeSelection.text'='',
                    'comarcaSelectedEntitiesList'='',
                    'contadorcomarca'='0',
                    'contadorMaiorcomarca'='0',
                    'comarcaPK.cdComarca'=comarca,
                    'nmComarca'='',
                    'secoesTreeSelection.values'=orgao_julgador,
                    'secoesTreeSelection.text'='5 Registros selecionados',
                    'dados.dtJulgamentoInicio'='',
                    'dados.dtJulgamentoFim'='',
                    'dados.dtRegistroInicio'='',
                    'dados.dtRegistroFim'='',
                    'tipoDecisaoSelecionados'='A',
                    'dados.ordenacao'='data')
  url <- 'https://esaj.tjsp.jus.br/cjsg/resultadoCompleta.do'
  r <- POST(url, body=form_data, config=list(ssl.verifypeer=F))
  cat(content(r, 'text'), file='/home/julio/mestrado/projeto_mestrado/testes/teste.html')
  
  url_pag <- sprintf('https://esaj.tjsp.jus.br/cjsg/trocaDePagina.do?tipoDeDecisao=A&pagina=%d', pag)  
  cat(sprintf('pagina: %d...', pag))
  
  cat('fazendo download...')
  r_pag <- GET(url_pag, config=c(ssl.verifypeer=F, set_cookies(unlist(r$cookies))))
  cat('download realizado! ')
  cat('inicializando parser...')
  
  try ({
    html <- htmlParse(content(r_pag, 'text'), encoding='UTF-8')
    nodes <- getNodeSet(html, "//tr[@class='fundocinza1']//table")  
    cat('rodando parser...')
    df <- ldply(nodes, parse_node)
    df$pag <- pag
    cat('OK!\n')
    Sys.sleep(2)
    return(df)  
  },TRUE)
  cat('BUGOU!!!!!\n')
  return(data.frame())
}

# other functions
parse_node_meta <- function(node) {
  val <- str_trim(str_split_fixed(gsub('[\n\r\t]','', xmlValue(node)), ':', 2))
  df <- data.frame(val[1,2], stringsAsFactors=F)
  names(df) <- val[1,1]
  df
}

parse_node <- function(node) {
  children <- xmlChildren(node)
  df <- do.call(cbind, lapply(children[2:(length(children)-1)], parse_node_meta))
  df$n_processo <- gsub('[\n\r\t ]', '', xmlValue(xmlChildren(xmlChildren(children[[1]])$td)$a))
  df$tipo <- str_trim(gsub(' ', ' ', gsub('[\n\r\t]', '', xmlValue(xmlChildren(xmlChildren(children[[1]])$td)$span))))
  df$cod_sentenca <- xmlGetAttr(xmlChildren(xmlChildren(children[[1]])$td)$a,'name')
  df$txt <-  gsub('[\r\t]', '', xmlValue(children[[length(children)]]))
  df
}
jtrecenti/jurimetria documentation built on May 20, 2019, 3:17 a.m.