UNPKG

brazilian-courts-scrappers

Version:

Data scrapper para fazer raspagem de dados de processos judiciais dos portais de tribunais do Brasil.

276 lines 13.8 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const ScrappedProcesso_1 = __importDefault(require("../data-structures/ScrappedProcesso")); const ScrappedUnidadeJurisdicional_1 = __importDefault(require("../data-structures/ScrappedUnidadeJurisdicional")); const ProcessoScrapper_1 = __importDefault(require("./ProcessoScrapper")); const ProjudiTjbaAndamentosScrapper_1 = __importDefault(require("./ProjudiTjbaAndamentosScrapper")); const ProjudiTjbaPartesScrapper_1 = __importDefault(require("./ProjudiTjbaPartesScrapper")); const utils_1 = require("../utils"); class ProjudiTjbaProcessoScrapper extends ProcessoScrapper_1.default { constructor(doc) { super(doc); this.doc = doc; } fetchProcessoInfo() { const _super = Object.create(null, { fetchProcessoInfo: { get: () => super.fetchProcessoInfo } }); return __awaiter(this, void 0, void 0, function* () { return _super.fetchProcessoInfo.call(this); }); } checkProcessoHomepage() { const result = super.checkProcessoHomepage(this.doc.URL, ProjudiTjbaProcessoScrapper.IGNORE_URLS_CONTAINING, ProjudiTjbaProcessoScrapper.PROCESSO_HOME_PATH_PART); return result; } loadPageCheckpoints() { this.divPartes = this.doc.querySelector("#Partes"); this.divPartesTbody = this.divPartes.querySelector("table > tbody"); } scrappeProcessoInfo() { var _a; return __awaiter(this, void 0, void 0, function* () { const andamentos = (_a = (yield this.getAndamentos())) !== null && _a !== void 0 ? _a : []; const { poloAtivo, poloPassivo, outros } = this.getPartes(); const { juizo, nomeJuiz } = this.getJuizoInfo(); const processoInfo = new ScrappedProcesso_1.default(this.getNumero(), "projudiTjba", poloAtivo !== null && poloAtivo !== void 0 ? poloAtivo : [], poloPassivo !== null && poloPassivo !== void 0 ? poloPassivo : [], outros !== null && outros !== void 0 ? outros : [], andamentos, this.getPedidos(), this.getUrl(), this.getDataDistribuicao(), this.getValorDaCausa(), this.getCausaDePedir(), juizo, nomeJuiz, this.getAudienciaFutura(andamentos), this.getTipoDeAcao(), this.getSegredoJustica(), this.getNumeroProcessoPrincipal(), this.getNumeroRegional(), this.getNumerosIncidentes(), this.getNumerosProcessosRelacionados()); return processoInfo; }); } getNumero() { const linkNumeroProcesso = this.divPartes.querySelector(`a[href*="${ProjudiTjbaProcessoScrapper.PROCESSO_HOME_PATH_PART}"]`); return linkNumeroProcesso === null || linkNumeroProcesso === void 0 ? void 0 : linkNumeroProcesso.textContent; // TODO: Validar número CNJ com RegEx ou lançar exceção } getNumeroRegional() { return null; } getUrl() { const linkNumeroProcesso = this.divPartes.querySelector(`a[href*="${ProjudiTjbaProcessoScrapper.PROCESSO_HOME_PATH_PART}"]`); return new URL(linkNumeroProcesso.href); } getDataDistribuicao() { const params = { parentElement: this.divPartesTbody, firstGuessQuerySelector: "tr:nth-child(14) > td:nth-child(3)", IterableElementsQuerySelector: "tr td", partialTextToSearch: "data de distribuição", }; const projudiDateString = (0, utils_1.getValueFollowingCellSearchedByTextContent)(params); return projudiDateString ? this.getDateFromProjudiTjbaDateString(projudiDateString) : null; } getDateFromProjudiTjbaDateString(dateStr) { var _a; const timeDiffFromGmt = "-03:00"; const dateStrWithoutPrepositions = dateStr .trim() .replaceAll(/(de )|(às )|( h)/g, ""); const dateStrDividedBySpaces = dateStrWithoutPrepositions.replaceAll(":", " "); const dateArray = dateStrDividedBySpaces.split(" "); const meses = { Janeiro: "01", Fevereiro: "02", Março: "03", Abril: "04", Maio: "05", Junho: "06", Julho: "07", Agosto: "08", Setembro: "09", Outubro: "10", Novembro: "11", Dezembro: "12", }; dateArray[1] = meses[dateArray[1]]; const year = String(dateArray[2]).padStart(4, "0"); const month = String(dateArray[1]); const day = String(dateArray[0]).padStart(2, "0"); const hour = String(dateArray[3]).padStart(2, "0"); const minute = String(dateArray[4]).padStart(2, "0"); const second = String((_a = dateArray[5]) !== null && _a !== void 0 ? _a : "00").padStart(2, "0"); return new Date(`${year}-${month}-${day}T${hour}:${minute}:${second}${timeDiffFromGmt}`); } getValorDaCausa() { const params = { parentElement: this.divPartesTbody, firstGuessQuerySelector: "tr:nth-child(15) > td:first-child", IterableElementsQuerySelector: "tr td", partialTextToSearch: "valor da causa", }; const projudiValorDaCausaString = (0, utils_1.getValueFollowingCellSearchedByTextContent)(params); let valorDaCausa = projudiValorDaCausaString !== null && projudiValorDaCausaString !== void 0 ? projudiValorDaCausaString : "".trim().replace(/(R\$ )|(\.)/g, ""); valorDaCausa = valorDaCausa.replace(",", "."); return Number(valorDaCausa); } getTipoDeAcao() { const params = { parentElement: this.divPartesTbody, firstGuessQuerySelector: "tr:nth-child(10) > td:first-child", IterableElementsQuerySelector: "tr td", partialTextToSearch: "classe", }; const projudiTipoDeAcaoString = (0, utils_1.getValueFollowingCellSearchedByTextContent)(params); if (!projudiTipoDeAcaoString) return []; const tipoDeAcaoStringArray = projudiTipoDeAcaoString .split(" « ") .reverse(); return tipoDeAcaoStringArray.map(tipoAcao => { return { id: undefined, valor: tipoAcao.trim() }; }); } getCausaDePedir() { const params = { parentElement: this.divPartesTbody, firstGuessQuerySelector: "tr:nth-child(8) > td:first-child", IterableElementsQuerySelector: "tr td", partialTextToSearch: "assunto", }; const projudiCausaDePedirString = (0, utils_1.getValueFollowingCellSearchedByTextContent)(params); const causaDePedirStringArray = !projudiCausaDePedirString ? [] : projudiCausaDePedirString.split(" « ").reverse(); return causaDePedirStringArray.map(causaPedir => { return { id: undefined, valor: causaPedir.trim() }; }); } getSegredoJustica() { const params = { parentElement: this.divPartesTbody, firstGuessQuerySelector: "tr:nth-child(11) > td:first-child", IterableElementsQuerySelector: "tr td", partialTextToSearch: "segredo de justiça", }; const projudiSegredoJusticaString = (0, utils_1.getValueFollowingCellSearchedByTextContent)(params); return projudiSegredoJusticaString === "SIM"; } getJuizoInfo() { var _a; const JuizoJuiz = (_a = this.getJuizoJuizString()) === null || _a === void 0 ? void 0 : _a.split(" Juiz: "); if (!JuizoJuiz) return { juizo: undefined, nomeJuiz: undefined }; const juizo = new ScrappedUnidadeJurisdicional_1.default(JuizoJuiz[0]); const nomeJuiz = JuizoJuiz[1].replace(" Histórico de Juízes", "").trim(); return { juizo, nomeJuiz }; } getJuizoJuizString() { const params = { parentElement: this.divPartesTbody, firstGuessQuerySelector: "tr:nth-child(7) > td:first-child", IterableElementsQuerySelector: "tr td", partialTextToSearch: "juízo", }; return (0, utils_1.getValueFollowingCellSearchedByTextContent)(params); } getNumeroProcessoPrincipal() { const params = { parentElement: this.divPartesTbody, firstGuessQuerySelector: "tr:nth-child(6) > td:first-child", IterableElementsQuerySelector: "tr td", partialTextToSearch: "proc. principal", }; const processoPrincipal = (0, utils_1.getValueFollowingCellSearchedByTextContent)(params); return processoPrincipal && !processoPrincipal.toLowerCase().includes("o próprio") ? processoPrincipal : null; } getNumerosIncidentes() { return []; } getNumerosProcessosRelacionados() { var _a; const params = { parentElement: this.divPartesTbody, firstGuessQuerySelector: "tr:nth-child(6) > td:nth-child(3)", IterableElementsQuerySelector: "tr td", partialTextToSearch: "proc. dependentes", }; // TODO: testar processo com mais de um processo dependente. São muitos TD? São múltiplos elementos A // na mesma TD? const processosRelacionados = (_a = (0, utils_1.getValueFollowingCellSearchedByTextContent)(params)) === null || _a === void 0 ? void 0 : _a.trim(); return processosRelacionados ? [processosRelacionados] : []; } getPartes() { const partesScrapper = new ProjudiTjbaPartesScrapper_1.default(this.doc); const partes = partesScrapper.fetchParticipantesInfo(); if (!partes) { return { poloAtivo: null, poloPassivo: null, outros: null, }; } return partes; } getAndamentos() { return __awaiter(this, void 0, void 0, function* () { const scrapper = new ProjudiTjbaAndamentosScrapper_1.default(this.doc); return yield scrapper.fetchAndamentosInfo(); }); } getPedidos() { const params = { parentElement: this.divPartesTbody, firstGuessQuerySelector: "tr:nth-child(9) > td:first-child", IterableElementsQuerySelector: "tr td", partialTextToSearch: "complementares", }; const pedidosTd = (0, utils_1.getElementFollowingCellSearchedByTextContent)(params); if (!pedidosTd) return []; const pedidos = []; pedidosTd .querySelectorAll("table > tbody > tr") .forEach(tr => { pedidos.push(tr.innerText); }); return pedidos; } getAudienciaFutura(andamentos) { const audienciaRelatedAndamentos = this.filterAudienciaConcerningAndamentos(andamentos); const lastRelevantAudiencia = this.getLastAudienciaOrNullIfCancelled(audienciaRelatedAndamentos); const audienciaProjudiDateRegexMatch = lastRelevantAudiencia === null || lastRelevantAudiencia === void 0 ? void 0 : lastRelevantAudiencia.observacao.match(ProjudiTjbaProcessoScrapper.PROJUDI_EXTENDED_DATE_REGEX); if (!audienciaProjudiDateRegexMatch || !lastRelevantAudiencia) return null; const audienciaProjudiDateString = audienciaProjudiDateRegexMatch[0]; const audienciaFutura = lastRelevantAudiencia; audienciaFutura.data = this.getDateFromProjudiTjbaDateString(audienciaProjudiDateString); return audienciaFutura; } filterAudienciaConcerningAndamentos(andamentosArray) { const audienciaRelatedAndamentos = andamentosArray.filter(andamento => (andamento === null || andamento === void 0 ? void 0 : andamento.nomeOriginalSistemaJustica.toLowerCase().includes("audiência")) && !andamento.cancelado); return audienciaRelatedAndamentos; } getLastAudienciaOrNullIfCancelled(audienciasUniverse) { const lastRelevantAudiencia = audienciasUniverse.at(-1); if (!(lastRelevantAudiencia === null || lastRelevantAudiencia === void 0 ? void 0 : lastRelevantAudiencia.observacao)) return null; return (lastRelevantAudiencia === null || lastRelevantAudiencia === void 0 ? void 0 : lastRelevantAudiencia.observacao.search(ProjudiTjbaProcessoScrapper.PROJUDI_EXTENDED_DATE_REGEX)) === -1 ? null : lastRelevantAudiencia; } } ProjudiTjbaProcessoScrapper.PROCESSO_HOME_PATH_PART = "/projudi/listagens/DadosProcesso?numeroProcesso="; ProjudiTjbaProcessoScrapper.IGNORE_URLS_CONTAINING = [ "https://projudi.tjba.jus.br/projudi/scripts/subModal/carregando.html", "https://projudi.tjba.jus.br/projudi/Cabecalho.jsp", "https://projudi.tjba.jus.br/projudi/advogado/CentroAdvogado", ]; ProjudiTjbaProcessoScrapper.PROJUDI_EXTENDED_DATE_REGEX = /\d{1,2} de [a-zç]+ de (\d{2}|\d{4}) às \d{1,2}:\d{1,2}/gi; exports.default = ProjudiTjbaProcessoScrapper; //# sourceMappingURL=ProjudiTjbaProcessoScrapper.js.map