Skip to content

Instantly share code, notes, and snippets.

View jjesusfilho's full-sized avatar
🏠
Working from home

José de Jesus Filho jjesusfilho

🏠
Working from home
View GitHub Profile
@jjesusfilho
jjesusfilho / acidentes.R
Created May 7, 2017 22:56
Dataprev - Acidentes de Trabalho
## Acidentes de trabalho
library(tidyverse)
library(jsonlite)
url<-"http://dadosabertos.dataprev.gov.br/opendata/act10/formato=json"
a<-readLines(file(url, encoding="ISO-8859-1"), warn=FALSE)
b<-fromJSON(a)
c<-b$nodes$node
c<-c[str_which(c$CNAE,"^(20|21)"),]
c[]<-lapply(c,function(x) iconv(x,from="latin2",to="UTF-8"))
@jjesusfilho
jjesusfilho / via_cep.R
Last active October 24, 2017 07:38
Retorna o logradouro e o latlong com base no cep
library(httr)
library(stringr)
library(purrr)
library(magrittr)
library(ggmap)
via_cep<-function(cep,latlong=TRUE){
cep %<>%stringr::str_replace("\\D","") %>%
stringr::str_pad(8,side="left",pad="0") %>%
as.character()
@jjesusfilho
jjesusfilho / cnpj.R
Last active March 23, 2019 02:16
Extrai dados com base no CNPJ
cnpj<-function(cnpj){
cnpj<-str_replace_all(cnpj,"\\D+","")
cnpj<-str_trim(cnpj)
cnpj<-str_pad(cnpj,8,"left",0)
df <- as.data.frame(setNames(replicate(30,numeric(0), simplify = F),
c("atividade_principal.text","atividade_principal.code","data_situacao","nome","uf","telefone","email","atividades_secundarias.text1","atividades_secundarias.text2","atividades_secundarias.code1","atividades_secundarias.code2","situacao","bairro","logradouro","numero","cep","municipio","abertura","natureza_juridica","fantasia","cnpj","ultima_atualizacao","status","tipo","complemento","efr","motivo_situacao","situacao_especial","data_situacao_especial","capital_social")))
for (i in seq(cnpj)){
tryCatch({
d<-paste0("http://receitaws.com.br/v1/cnpj/",cnpj[i])
@jjesusfilho
jjesusfilho / es_bos.R
Last active March 23, 2019 02:21
Baixar BOs do Espírito Santo
## Acesso a todos os BOs:
# https://boletins.sesp.es.gov.br/
## Exemplo
library(httr)
library(textreadr)
library(stringi)
@jjesusfilho
jjesusfilho / trt4.R
Last active June 25, 2017 03:25
scraper do Tribunal Regional do Trabalho da 4ª Região
library(httr)
library(xml2)
library(stringr)
TRT4_meta<-function(livre="",quote=T,inicio="",fim=""){
if(quote==TRUE){livre<-deparse(livre)}
url1<-"http://www.trt4.jus.br/portal/portal/trt4/consultas/jurisprudencia/acordaos"
handle(url1)
url2<-"http://gsa5.trt4.jus.br/search?"
l<-list(client = "jurisp",
@jjesusfilho
jjesusfilho / trt4_inteiro_teor.R
Last active June 25, 2017 03:21
Inteiro teor do acórdão do TRT4
## Essas bibliotecas não precisam ser carregadas, mas devem estar instaladas.
library(purrr)
library(httr)
library(boilerpipeR)
trt4_inteiro_teor<-function(url){
url %>% purrr::map_chr(function(x){
x %>%
httr::GET() %>%
httr::content("text") %>%
@jjesusfilho
jjesusfilho / query_extract.R
Created May 25, 2017 01:03
Extract query parameters from url
library(stringr)
library(magritr)
query_extract<-function(string){
s<-str_split(string,"&") %>%
unlist()
nomes<-s %>%
str_extract(".*?(?=\\=)")
@jjesusfilho
jjesusfilho / tjscSG.R
Last active June 30, 2017 15:58
Função para extrair metadados e inteiro teor do Tribunal de Justiça de Santa Catarina
library(httr)
library(xml2)
library(stringr)
library(purrr)
library(tibble)
## Há duas opções: usar uma palavra ou uma frase.
tjscSG<- function(palavra="",frase=""){
@jjesusfilho
jjesusfilho / tjrsSG_meta.R
Last active June 23, 2017 23:06
scraper do Tribunal de Justiça do Rio Grande do Sul
library(httr)
library(xml2)
library(stringr)
library(boilerpipeR)
tjrsSG_meta<-function(BuscaLivre="",quote=TRUE){
if(quote==TRUE) BuscaLivre<-deparse(BuscaLivre)
url<-"http://www.tjrs.jus.br/busca/search?"
@jjesusfilho
jjesusfilho / ifs.R
Last active June 3, 2017 09:54
Wraper da função ifelse do R.
ifs<-function(x,y,z=NULL){
stopifnot(is.list(y))
#if(length(z)==0) {z<-x}
for(i in 1:length(y)){
x<-ifelse(stringr::str_detect(x,y[[i]][1]),y[[i]][2],x)
}
return(x)
}