UNPKG

brazilian-courts-scrappers

Version:

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

102 lines 6.05 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getElementFollowingCellSearchedByTextContent = exports.getValueFollowingCellSearchedByTextContent = exports.extendedTrim = exports.getTextContent = exports.stripScriptTagsFromHtmlString = exports.stripBlankLines = exports.waitForElement = exports.trtInterfacePolosNames = exports.EMAIL_REGEX = exports.REGEX_CNJ_NUMBER = void 0; exports.REGEX_CNJ_NUMBER = /(\d{7}-\d{2}.\d{4}.)(\d)(.\d{2}.\d{4})/; exports.EMAIL_REGEX = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/g; var trtInterfacePolosNames; (function (trtInterfacePolosNames) { trtInterfacePolosNames[trtInterfacePolosNames["ativo"] = 0] = "ativo"; trtInterfacePolosNames[trtInterfacePolosNames["passivo"] = 1] = "passivo"; trtInterfacePolosNames[trtInterfacePolosNames["outros"] = 2] = "outros"; })(trtInterfacePolosNames || (exports.trtInterfacePolosNames = trtInterfacePolosNames = {})); function waitForElement(selector, doc, { returnElementSelector = selector, documentParent, waitForTextContent = false, } = {}) { return __awaiter(this, void 0, void 0, function* () { function reevaluateCheckpoints() { var _a; const documentToSearch = documentParent ? (_a = documentParent.contentDocument) !== null && _a !== void 0 ? _a : doc : doc; const awaitedElement = documentToSearch.querySelector(selector); const searchedInfoWasFound = waitForTextContent ? !!(awaitedElement === null || awaitedElement === void 0 ? void 0 : awaitedElement.textContent) : !!awaitedElement; return { documentToSearch, searchedInfoWasFound }; } return yield new Promise(resolve => { const { documentToSearch, searchedInfoWasFound } = reevaluateCheckpoints(); if (searchedInfoWasFound) { return resolve(documentToSearch.querySelector(returnElementSelector)); } const timerId = setInterval(() => { const { documentToSearch, searchedInfoWasFound } = reevaluateCheckpoints(); if (searchedInfoWasFound) { clearInterval(timerId); resolve(documentToSearch.querySelector(returnElementSelector)); } }, 250); }); }); } exports.waitForElement = waitForElement; function stripBlankLines(str) { const lines = str.split("\n"); const nonBlankLines = lines.filter(line => line.trim() !== ""); return nonBlankLines.join("\n").trim(); } exports.stripBlankLines = stripBlankLines; function stripScriptTagsFromHtmlString(htmlString) { const scriptTagWithContentRegex = /<script[^]*?<\/script>/gi; const contentScriptTagsStrippedHtml = htmlString.replace(scriptTagWithContentRegex, ""); const selfEnclosingScriptTagRegex = /<script[^]*?>/gi; return contentScriptTagsStrippedHtml.replace(selfEnclosingScriptTagRegex, ""); } exports.stripScriptTagsFromHtmlString = stripScriptTagsFromHtmlString; function getTextContent(innerHtml, doc, replaces = []) { const div = doc.createElement("div"); replaces.forEach(({ expressionToSearch, replacingText }) => (innerHtml = innerHtml.replace(expressionToSearch, replacingText))); div.innerHTML = innerHtml; return stripBlankLines(div.textContent).trim(); } exports.getTextContent = getTextContent; function extendedTrim(str, trimmableCharacters) { const specialRegexChars = "^$.|?*+()[]{}\\"; const escapedTrimmableCharsArray = trimmableCharacters.map(str => { return specialRegexChars.includes(str) ? "\\" + str : str; }); const escapedTrimmableChars = escapedTrimmableCharsArray.join(""); const trimRegex = new RegExp(`(^[${escapedTrimmableChars}])|([${escapedTrimmableChars}]$)`, "gi"); const trimmedStr = str.trim().replaceAll(trimRegex, ""); return trimmedStr === str ? trimmedStr : extendedTrim(trimmedStr, trimmableCharacters); } exports.extendedTrim = extendedTrim; function getValueFollowingCellSearchedByTextContent(searcherParams) { var _a; return (_a = getElementFollowingCellSearchedByTextContent(searcherParams)) === null || _a === void 0 ? void 0 : _a.textContent; } exports.getValueFollowingCellSearchedByTextContent = getValueFollowingCellSearchedByTextContent; function getElementFollowingCellSearchedByTextContent({ parentElement, firstGuessQuerySelector, IterableElementsQuerySelector, partialTextToSearch, }) { var _a; const firstGuess = parentElement.querySelector(firstGuessQuerySelector); if (firstGuess === null || firstGuess === void 0 ? void 0 : firstGuess.textContent.toLowerCase().includes(partialTextToSearch.toLowerCase())) { return firstGuess.nextElementSibling; } const slowChoice = Array.from(parentElement.querySelectorAll(IterableElementsQuerySelector)).filter(iElement => { return iElement.textContent .toLowerCase() .includes(partialTextToSearch.toLowerCase()); }); return (_a = slowChoice[0]) === null || _a === void 0 ? void 0 : _a.nextElementSibling; } exports.getElementFollowingCellSearchedByTextContent = getElementFollowingCellSearchedByTextContent; //# sourceMappingURL=utils.js.map