import requests
import json
API_KEY = "sua_chave_api_legalmail"
BASE_URL = "https://app.legalmail.com.br/api"
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
def listar_processos():
url = f"{BASE_URL}/v1/lawsuit/all"
response = requests.get(url, headers=headers)
return response.json()
def obter_detalhes(numero_processo):
url = f"{BASE_URL}/v1/lawsuit/detail"
params = {"lawsuit_number": numero_processo}
response = requests.get(url, headers=headers, params=params)
return response.json()
# Uso
processos = listar_processos()
print(json.dumps(processos, indent=2))def analise_integrada(numero_processo, tribunal):
# Consultar LegalMail
dados_legalmail = obter_detalhes_legalmail(numero_processo)
# Consultar DataJud (fallback)
dados_datajud = obter_detalhes_datajud(numero_processo, tribunal)
# Consultar ERP
dados_erp = obter_status_erp(numero_processo)
# Comparar e detectar divergências
divergencias = detectar_divergencias(
dados_legalmail,
dados_datajud,
dados_erp
)
# Gerar relatório
relatorio = {
"processo": numero_processo,
"tribunal": tribunal,
"status_tribunal": dados_legalmail.get("status"),
"status_erp": dados_erp.get("status"),
"divergencias": divergencias,
"timestamp": datetime.now().isoformat()
}
return relatorio
# Uso
resultado = analise_integrada("0000832-35.2018.4.01.3202", "trf1")
print(json.dumps(resultado, indent=2))def detectar_divergencias(tribunal, erp, datajud):
divergencias = []
# Verificar status
if tribunal.get("status") != erp.get("status"):
divergencias.append({
"tipo": "Status",
"severidade": "CRÍTICA",
"tribunal": tribunal.get("status"),
"erp": erp.get("status")
})
# Verificar resultado
if tribunal.get("resultado") and not erp.get("resultado"):
divergencias.append({
"tipo": "Resultado",
"severidade": "CRÍTICA",
"tribunal": tribunal.get("resultado"),
"erp": "Não registrado"
})
# Verificar valores
if tribunal.get("valor") != erp.get("valor"):
divergencias.append({
"tipo": "Valor",
"severidade": "MÉDIA",
"tribunal": tribunal.get("valor"),
"erp": erp.get("valor")
})
return divergenciasdef gerar_tarefas(numero_processo, divergencias):
tarefas = []
for div in divergencias:
if div["severidade"] == "CRÍTICA":
tarefa = {
"id": f"TAREFA_{numero_processo}_{datetime.now().timestamp()}",
"tipo": "Auditoria Processual",
"prioridade": "CRÍTICA",
"titulo": f"Regularizar {div['tipo']}: {numero_processo}",
"descricao": f"Divergência encontrada. Tribunal: {div['tribunal']}. ERP: {div['erp']}",
"responsavel": "Gestor de Processos",
"data_vencimento": (datetime.now() + timedelta(days=2)).isoformat(),
"status": "Aberta"
}
tarefas.append(tarefa)
# Criar tarefa no ERP
criar_tarefa_erp(tarefa)
return tarefasdef auditoria_completa(lista_processos):
resultados = []
for numero_processo in lista_processos:
try:
# Análise integrada
analise = analise_integrada(numero_processo, "trf1")
# Detectar divergências
divergencias = analise.get("divergencias", [])
# Gerar tarefas se houver divergências
if divergencias:
tarefas = gerar_tarefas(numero_processo, divergencias)
analise["tarefas_geradas"] = len(tarefas)
resultados.append(analise)
except Exception as e:
print(f"Erro ao analisar {numero_processo}: {str(e)}")
# Gerar relatório consolidado
relatorio = {
"total_processos": len(lista_processos),
"processos_analisados": len(resultados),
"divergencias_encontradas": sum(len(r.get("divergencias", [])) for r in resultados),
"tarefas_geradas": sum(r.get("tarefas_geradas", 0) for r in resultados),
"timestamp": datetime.now().isoformat()
}
return relatorio, resultadosAdapte estes exemplos para seu caso de uso específico e integre com seus sistemas.
Guia de Implementação