Path: blob/master/node_modules/@bochilteam/scraper/lib/cjs/others/KBBI.js
1126 views
"use strict";1var __importDefault = (this && this.__importDefault) || function (mod) {2return (mod && mod.__esModule) ? mod : { "default": mod };3};4Object.defineProperty(exports, "__esModule", { value: true });5const got_1 = __importDefault(require("got"));6const cheerio_1 = __importDefault(require("cheerio"));7const utils_js_1 = require("../utils.js");8/**9* p = Partikel: kelas kata yang meliputi kata depan, kata sambung, kata seru, kata sandang, ucapan salam10*11* n = Nomina: kata benda12*/13async function kbbi(words) {14const html = await (0, got_1.default)(`https://kbbi.kemdikbud.go.id/entri/${encodeURIComponent(words)}`).text();15const $ = cheerio_1.default.load(html);16const isExist = !/tidak ditemukan/i.test($('body > div.container.body-content > h4[style="color:red"]').text());17if (!isExist)18throw new utils_js_1.ScraperError(`${words} does not exist!`);19const results = [];20let isContent = false;21let lastTitle;22$('body > div.container.body-content').children().each((_, el) => {23const tag = el.tagName;24const elem = $(el);25if (tag === 'hr')26isContent = !isContent && !Object.keys(results).length;27if (tag === 'h2' && isContent) {28const index = elem.find('sup').text().trim();29const title = elem.text().trim();30results.push({31index: parseInt(index),32title,33means: []34});35lastTitle = title;36}37if ((tag === 'ol' || tag === 'ul') && isContent && lastTitle) {38elem.find('li').each((_, el) => {39const li = $(el).text().trim();40const index = results.findIndex(({ title }) => title === lastTitle);41if (index !== -1)42results[index].means.push(li);43else44console.log(li, lastTitle);45});46lastTitle = '';47}48});49return results;50}51exports.default = kbbi;52//# sourceMappingURL=KBBI.js.map5354