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