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