UNPKG

brazilian-courts-scrappers

Version:

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

169 lines 8.6 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")); const Pje1gTjbaProcessoScrapper_1 = __importDefault(require("./Pje1gTjbaProcessoScrapper")); class Pje1gTjbaAndamentosScrapper extends AndamentosScrapper_1.default { constructor(doc) { super(doc); this.doc = doc; } 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.divTimeline = this.doc.getElementById("divTimeLine:eventosTimeLineElement"); } getAndamentos() { var _a, _b, _c; return __awaiter(this, void 0, void 0, function* () { if (this.divTimeline == null) return []; const andamentosMediaDivs = this.divTimeline.querySelectorAll(":scope > .media:not(.data)"); const andamentos = []; for (const mediaDiv of Array.from(andamentosMediaDivs)) { const andamentoPjeType = Array.from(mediaDiv.classList).includes("tipo-D") ? "documento" : "movimentação"; const bodyBoxDiv = mediaDiv.querySelector(".media-body"); if (!bodyBoxDiv) continue; const horaAndamento = (_c = (_b = (_a = bodyBoxDiv.querySelector("small.text-muted")) === null || _a === void 0 ? void 0 : _a.textContent) === null || _b === void 0 ? void 0 : _b.trim()) !== null && _c !== void 0 ? _c : ""; const [id, cancelado, docName, docContent] = yield this.getDocumentInfo(bodyBoxDiv, andamentoPjeType); const data = this.getDate(mediaDiv, horaAndamento); const nomeOriginalSistemaJustica = this.getNome(bodyBoxDiv, docName); const fullObservacao = `${docName !== null && docName !== void 0 ? docName : ""}\n${docContent !== null && docContent !== void 0 ? docContent : ""}`; const observacao = (0, utils_1.stripBlankLines)(fullObservacao); const andamento = new ScrappedAndamento_1.default(nomeOriginalSistemaJustica, data, id, observacao, undefined, cancelado); andamentos.unshift(andamento); } return andamentos; }); } getDocumentInfo(bodyBoxDiv, andamentoType) { return __awaiter(this, void 0, void 0, function* () { if (andamentoType !== "documento") { return new Promise(resolve => resolve([])); } let mainDocumentNameString; let docContent = ""; const isCancelado = this.isCancelado(bodyBoxDiv); if (isCancelado) { mainDocumentNameString = bodyBoxDiv .querySelector(".anexos > .anexos-inativos > span") .textContent.trim(); } else { const mainDocumentA = bodyBoxDiv.querySelector(".anexos > a:first-child"); mainDocumentNameString = mainDocumentA .querySelector(":scope > span") .textContent.trim(); docContent = yield this.getDocumentTextContentIfExists(mainDocumentA); } const id = this.getIdFromDocString(mainDocumentNameString); const docName = this.getNameFromDocString(mainDocumentNameString); return [id, isCancelado, docName, docContent]; }); } getIdFromDocString(docString) { const idRegex = /^\d+(?= -)/; const match = docString.match(idRegex); return match ? match[0] : ""; } getNameFromDocString(docString) { const idRegex = /(?<=^\d+ - ).+/; const match = docString.match(idRegex); return match ? match[0] : ""; } getDate(mediaDiv, horaAndamento) { const dateString = this.findDateSibling(mediaDiv).textContent.trim(); return Pje1gTjbaProcessoScrapper_1.default.getDateFromPjeTjbaDateString(dateString, horaAndamento); } findDateSibling(mediaDiv) { const prevSibling = mediaDiv.previousElementSibling; if (Array.from(prevSibling.classList).includes("data")) return prevSibling; return this.findDateSibling(prevSibling); } getNome(bodyBoxDiv, filename = "") { if (filename !== "") { const matchParenthesisResults = filename.match(/.+(?= (\(.+\))$)/); return matchParenthesisResults ? matchParenthesisResults[0].trim() : filename; } const titleDivs = bodyBoxDiv.querySelectorAll(":scope > :not(.anexos, .col-sm-12)"); const titles = Array.from(titleDivs).map(titleDiv => titleDiv.querySelector("span").textContent.trim()); return titles[0]; } isCancelado(bodyBoxDiv) { const inativosDiv = bodyBoxDiv.querySelector(".anexos > .anexos-inativos"); return !!inativosDiv; // TODO: buscar exemplo de andamento cancelado SEM DOCUMENTO no PJe } getDocumentTextContentIfExists(documentAnchor) { return __awaiter(this, void 0, void 0, function* () { if (this.isPdf(documentAnchor)) return new Promise(resolve => resolve("")); documentAnchor.click(); const contentDoc = yield this.waitPageLoad("#frameHtml"); yield new Promise(resolve => setTimeout(resolve, 250)); if (this.isPjeDocumentLandingPageBug(contentDoc)) return new Promise(resolve => resolve("")); else return this.getDocumentTextContent(contentDoc); }); } isPdf(documentAnchor) { const icon = documentAnchor.querySelector(":scope > i:first-child"); return Array.from(icon.classList).includes("fa-file-pdf-o"); } waitPageLoad(iframeDocSelector) { var _a, _b, _c; return __awaiter(this, void 0, void 0, function* () { let readystateComplete = false; let bodyLengthChanged = false; let bodyLength = undefined; let iframe; while (!readystateComplete || bodyLengthChanged) { yield new Promise(resolve => setTimeout(resolve, 350)); iframe = this.doc.querySelector(iframeDocSelector); readystateComplete = ((_a = iframe === null || iframe === void 0 ? void 0 : iframe.contentDocument) === null || _a === void 0 ? void 0 : _a.readyState) === "complete"; bodyLengthChanged = bodyLength !== ((_b = iframe === null || iframe === void 0 ? void 0 : iframe.contentDocument) === null || _b === void 0 ? void 0 : _b.body.children.length); bodyLength = (_c = iframe === null || iframe === void 0 ? void 0 : iframe.contentDocument) === null || _c === void 0 ? void 0 : _c.body.children.length; } return iframe.contentDocument; }); } isPjeDocumentLandingPageBug(htmlDoc) { if (htmlDoc.head.children.length > 3) return true; else return false; } getDocumentTextContent(contentDoc) { const noScriptInnerHtml = (0, utils_1.stripScriptTagsFromHtmlString)(contentDoc.body.innerHTML); return (0, utils_1.getTextContent)(noScriptInnerHtml, contentDoc); } } exports.default = Pje1gTjbaAndamentosScrapper; //# sourceMappingURL=Pje1gTjbaAndamentosScrapper.js.map