brazilian-courts-scrappers
Version:
Data scrapper para fazer raspagem de dados de processos judiciais dos portais de tribunais do Brasil.
195 lines • 9.98 kB
JavaScript
;
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 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() {
return __awaiter(this, void 0, void 0, function* () {
yield this.expandMovimentos();
this.lisAndamentos =
this.doc.querySelectorAll(".pje-timeline > li");
});
}
expandMovimentos() {
return __awaiter(this, void 0, void 0, function* () {
const expandButton = this.doc.querySelector("button[aria-label='Exibir movimentos.']");
expandButton.click();
yield (0, utils_1.waitForElement)(".pje-timeline > li > div > mat-card[id^='mov']", this.doc);
});
}
getAndamentos() {
return __awaiter(this, void 0, void 0, function* () {
const andamentos = [];
for (const li of Array.from(this.lisAndamentos)) {
const andamentoMatcardId = li
.querySelector(":scope > div > mat-card")
.getAttribute("id");
const andamentoType = andamentoMatcardId.startsWith("doc")
? "documento"
: "movimentação";
const elMatCard = li.querySelector("mat-card");
const [id, cancelado, docName, docContent] = yield this.getDocumentInfo(elMatCard, andamentoType);
const horaAndamento = elMatCard
.querySelector(":scope div.tl-item-hora")
.textContent.trim();
const data = this.getDate(li, horaAndamento);
const nomeOriginalSistemaJustica = this.getNome(elMatCard, 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 yield Promise.all(andamentos);
});
}
getDocumentInfo(elMatCard, andamentoType) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
if (andamentoType !== "documento") {
return new Promise(resolve => resolve([]));
}
const isCancelado = !!elMatCard.querySelector(":scope a.is-inativo");
const docName = this.getDocName(elMatCard);
const id = (_c = (_b = (_a = elMatCard
.querySelector(":scope > div > a > span.ng-star-inserted")) === null || _a === void 0 ? void 0 : _a.textContent) === null || _b === void 0 ? void 0 : _b.replace("- ", "").trim()) !== null && _c !== void 0 ? _c : "";
const docContent = yield this.getDocContent(elMatCard, isCancelado);
return [id, isCancelado, docName, docContent];
});
}
getDocName(elMatCard) {
const nameSpans = Array.from(elMatCard.querySelectorAll(":scope > div > a > span:not([class])"));
const [firstNameStr, secondNameStr] = nameSpans.map(span => { var _a; return (_a = span.textContent) === null || _a === void 0 ? void 0 : _a.trim(); });
if (firstNameStr === `(${secondNameStr})`)
return firstNameStr;
return `${firstNameStr} ${secondNameStr}`;
}
getDocContent(elMatCard, isCancelado) {
return __awaiter(this, void 0, void 0, function* () {
if (isCancelado)
return new Promise(resolve => resolve(""));
const mainDocumentA = elMatCard.querySelector("a:not(.ng-star-inserted)");
if (!mainDocumentA)
return new Promise(resolve => resolve(""));
const docContent = yield this.getDocumentTextContentIfExists(mainDocumentA);
return docContent !== null && docContent !== void 0 ? docContent : "";
});
}
getDocumentTextContentIfExists(documentAnchor) {
return __awaiter(this, void 0, void 0, function* () {
documentAnchor.click();
const docAreaContainer = yield (0, utils_1.waitForElement)("pje-historico-scroll-documento", documentAnchor.ownerDocument);
if (!docAreaContainer)
return new Promise(resolve => resolve(""));
const extractors = {
pdf: () => __awaiter(this, void 0, void 0, function* () {
console.warn(1);
const documentWrapper = (yield (0, utils_1.waitForElement)("pje-historico-scroll-documento object", documentAnchor.ownerDocument));
if (!documentWrapper)
return new Promise(resolve => resolve(""));
console.warn(2, documentWrapper);
const contentElement = yield (0, utils_1.waitForElement)("body div#viewer div.endOfContent", documentAnchor.ownerDocument, {
returnElementSelector: "body div#viewer",
documentParent: documentWrapper,
waitForTextContent: false,
});
console.warn(3, contentElement);
return contentElement;
}),
inline: () => __awaiter(this, void 0, void 0, function* () {
return yield (0, utils_1.waitForElement)("pje-historico-scroll-documento mat-card-content > span", documentAnchor.ownerDocument, { waitForTextContent: true });
}),
};
const parser = this.isPdf(docAreaContainer)
? extractors.pdf
: extractors.inline;
const contentElement = yield parser();
if (!contentElement)
return new Promise(resolve => resolve(""));
return this.getElementInnerText(contentElement);
});
}
isPdf(docAreaContainer) {
console.warn({
docAreaContainer,
ifQs: !!docAreaContainer.querySelector("div.container-pdf > object"),
});
return !!docAreaContainer.querySelector("div.container-pdf > object");
}
getElementInnerText(contentElement) {
const inHtml = contentElement.innerHTML;
const noScriptInnerHtml = (0, utils_1.stripScriptTagsFromHtmlString)(inHtml);
return (0, utils_1.getTextContent)(noScriptInnerHtml, contentElement.ownerDocument, [
{ expressionToSearch: /<br.*?>/gim, replacingText: "\n" },
]);
}
getDate(li, horaAndamento) {
const dateString = this.findDateString(li);
return this.getDateFromPje1gTrt5AndamentoDateString(dateString, horaAndamento);
}
findDateString(li) {
var _a, _b, _c;
if (li.querySelector(":scope > div[role=heading]")) {
return ((_c = (_b = (_a = li.querySelector(":scope > div[role=heading]")) === null || _a === void 0 ? void 0 : _a.textContent) === null || _b === void 0 ? void 0 : _b.trim()) !== null && _c !== void 0 ? _c : "");
}
return this.findDateString(li.previousElementSibling);
}
getDateFromPje1gTrt5AndamentoDateString(dateStr, timeStr = "00:00") {
const dateArray = dateStr.split(" ");
const meses = {
jan: "01",
fev: "02",
mar: "03",
abr: "04",
mai: "05",
jun: "06",
jul: "07",
ago: "08",
set: "09",
out: "10",
nov: "11",
dez: "12",
};
const mesStr = dateArray[1].replace(".", "").toLowerCase();
const monthNumberString = meses[mesStr];
const iso8601DateString = `${dateArray[2]}-${monthNumberString}-${dateArray[0]}T${timeStr}-03:00`;
return new Date(iso8601DateString);
}
getNome(elMatCard, docName = "") {
var _a, _b;
if (docName !== "") {
const withoutParenthesisResults = docName.match(/.+(?= ?(\(.+\))$)/);
return withoutParenthesisResults
? withoutParenthesisResults[0].trim()
: docName;
}
const divString = (_b = (_a = elMatCard.querySelector(":scope > div")) === null || _a === void 0 ? void 0 : _a.textContent) !== null && _b !== void 0 ? _b : "";
return divString.replace("Descrição do movimento:", "").trim();
}
}
exports.default = Pje1gTjbaAndamentosScrapper;
//# sourceMappingURL=Pje1gTrt5AndamentosScrapper.js.map