UNPKG

brazilian-courts-scrappers

Version:

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

129 lines 7.02 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 ScrappedAndamento_1 = __importDefault(require("../data-structures/ScrappedAndamento")); const utils_1 = require("../utils"); const AndamentosScrapper_1 = __importDefault(require("./AndamentosScrapper")); class ProjudiTjbaAndamentosScrapper extends AndamentosScrapper_1.default { constructor(doc) { super(doc); this.doc = doc; this.HTML_BODY_REGEX = /<body[^]*<\/body>/gi; } fetchAndamentosInfo() { const _super = Object.create(null, { fetchAndamentosInfo: { get: () => super.fetchAndamentosInfo } }); return __awaiter(this, void 0, void 0, function* () { return yield _super.fetchAndamentosInfo.call(this); }); } loadPageCheckpoints() { this.divAndamentosTbody = this.doc.querySelector("#Arquivos > table > tbody"); } getAndamentos() { var _a, _b, _c, _d, _e, _f, _g, _h, _j; return __awaiter(this, void 0, void 0, function* () { const andamentos = []; const andamentosTrs = this.divAndamentosTbody.querySelectorAll("tr:not(tr:first-of-type, tr *)"); for (const tr of Array.from(andamentosTrs)) { const id = tr .querySelector("td > table > tbody > tr > td:nth-child(1)") .textContent.trim(); const dataStr = tr .querySelector("td > table > tbody > tr > td:nth-child(3)") .textContent.trim(); const data = this.brStringToDate(dataStr); const agente = tr .querySelector("td > table > tbody > tr > td:nth-child(4)") .textContent.trim(); const nomeAndamentoTd = tr.querySelector("td > table > tbody > tr > td:nth-child(2)"); const cancelado = this.isCancelado(nomeAndamentoTd); const nomeOriginalSistemaJustica = nomeAndamentoTd .querySelector("b > font") .innerText.trim(); const obsUnderNomeAndamento = cancelado ? (_c = (_b = (_a = nomeAndamentoTd.querySelector("strike")) === null || _a === void 0 ? void 0 : _a.textContent) === null || _b === void 0 ? void 0 : _b.trim()) !== null && _c !== void 0 ? _c : "" : (_e = (_d = nomeAndamentoTd.childNodes[nomeAndamentoTd.TEXT_NODE].textContent) === null || _d === void 0 ? void 0 : _d.trim()) !== null && _e !== void 0 ? _e : ""; const contentInButton = (_h = (_g = (_f = tr .querySelector("td > table + span:first-of-type")) === null || _f === void 0 ? void 0 : _f.textContent) === null || _g === void 0 ? void 0 : _g.trim()) !== null && _h !== void 0 ? _h : ""; const contentInDocument = (_j = (yield this.getDocumentTextContentIfExists(tr))) !== null && _j !== void 0 ? _j : ""; const fullObservacao = `${obsUnderNomeAndamento}\n${contentInButton}\n${contentInDocument}`; const observacao = (0, utils_1.stripBlankLines)(fullObservacao); const andamento = new ScrappedAndamento_1.default(nomeOriginalSistemaJustica, data, id, observacao, agente, cancelado); andamentos.unshift(andamento); } return andamentos; }); } brStringToDate(str) { const items = str.split("/"); if (items[2].length === 2) items[2] = `20` + items[2]; return new Date(parseInt(items[2]), parseInt(items[1]) - 1, parseInt(items[0])); } isCancelado(andamentoTd) { //TODO: provavelmente é na classlist return andamentoTd.innerHTML.includes("strike"); } getDocumentTextContentIfExists(tr) { return __awaiter(this, void 0, void 0, function* () { const documentsRows = tr.querySelectorAll('td > table ~ span[id^="sub"] > div > div > table > tbody > tr'); if (!documentsRows) return new Promise(resolve => resolve("")); let docUri = ""; documentsRows.forEach(tr => { let a = tr.querySelector("td:nth-child(4) > a"); if (a && a.innerText === "online.html") docUri = a.href; }); if (!docUri) return new Promise(resolve => resolve("")); const docTextContent = yield this.getDocumentTextContent(docUri); if (this.documentIsNotRelevant(docTextContent)) return new Promise(resolve => resolve("")); return docTextContent; }); } getDocumentTextContent(uri) { return __awaiter(this, void 0, void 0, function* () { const response = yield fetch(uri, { method: "GET" }); if (!response.ok) { throw new Error(`Não foi possível coletar de forma automática o conteúdo do andamento. Erro: ${response.statusText}`); } const arrBuffer = yield response.arrayBuffer(); const charCodesArray = new Uint8Array(arrBuffer); const dec = new TextDecoder("windows-1252"); const htmlStr = dec.decode(charCodesArray); const bodyStr = this.getBodyInnerHtmlFromHtmlString(htmlStr); const noScriptBodyStr = (0, utils_1.stripScriptTagsFromHtmlString)(bodyStr); return (0, utils_1.getTextContent)(noScriptBodyStr, this.doc); }); } getBodyInnerHtmlFromHtmlString(htmlStr) { const bodyMatches = htmlStr.match(this.HTML_BODY_REGEX); if (bodyMatches === null) return `<body>${htmlStr}</body>`; return bodyMatches[0]; } documentIsNotRelevant(textContent) { const lowCaseDocText = textContent.toLowerCase(); const notRelevant = lowCaseDocText.includes("endereço para devolução do ar") && lowCaseDocText.includes("assinatura do recebedor") && lowCaseDocText.includes("rubrica e matrícula do carteiro"); return !!notRelevant; } } exports.default = ProjudiTjbaAndamentosScrapper; //# sourceMappingURL=ProjudiTjbaAndamentosScrapper.js.map