summaryrefslogtreecommitdiff
path: root/ichimoe-get.js
blob: f929db54f26b06a366a17cc90bc92e4268440319 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env node
const https = require("https");
const util = require("util");
const cheerio = require("cheerio");

function atob(b64) {
	return Buffer.from(b64, "base64").toString();
}

function normaliseArg(arg) {
	if ((arg[0] == '"' || arg[0] == "'") && arg[arg.length - 1] == arg[0]) {
		return arg.slice(1, arg.length - 1);
	} else {
		return arg;
	}
}

function parseDefinitionLI($, li) {
	const items = $(li).children();
	let pos = null, desc = null, unknown = [];

	for (let i = 0; i < items.length; i++) {
		if (items[i].type != "tag") continue;
		const tagname = items[i].name;
		const cls = items[i].attribs.class;
		if (tagname == "span" && cls == "pos-desc") {
			pos = $(items[i]).text().trim();
		} else if (tagname == "span" && cls == "gloss-desc") {
			desc = $(items[i]).text().trim();
		} else if (tagname == "span" && cls == "sense-info-note has-tip tip-top") {
			// note, let's skip those
		} else {
			unknown.push("li?? " + tagname + " " + cls);
		}
	}

	if (unknown.length) return {pos, desc, unknown};
	else return {pos, desc};
}

function parseDefinitionsOL($, ol) {
	const definitions = [];
	const lis = $(ol).children();
	for (let j = 0; j < lis.length; j++) {
		definitions.push(parseDefinitionLI($, lis[j]));
	}
	return definitions;
}

function parseDL($, dl) {
	const alternatives = [];
	const items = $(dl).children();
	for (let i = 0; i < items.length/2; i++) {
		const dt = items[2*i];
		const dd = items[2*i+1];
		// This replace is not always necessary but it seems reasonably safe
		const text = $(dt).text().trim().replace(new RegExp(`^${i+1}\\. `), "");
		const result = parseDefinitionsDD($, dd);
		alternatives.push({text, ...result});
	}
	return alternatives;
}

function parseConjugationDiv($, div) {
	const items = $(div).children();
	let formdesc = null;
	let alternatives = [];

	for (let i = 0; i < items.length; i++) {
		if (items[i].type != "tag") continue;
		const tagname = items[i].name;
		const cls = items[i].attribs.class;
		if (tagname == "div" && cls == "conj-prop") {
			if (formdesc == null) formdesc = $(items[i]).text().trim().replace(/  +/g, " ");
		} else if (tagname == "div" && cls == "conj-gloss") {
			const dl = $(items[i]).children()[0];
			alternatives = alternatives.concat(parseDL($, dl));
		} else {
			unknown.push("conj?? " + tagname + " " + cls);
		}
	}

	return {
		form: formdesc,
		alternatives
	};
}

function parseConjugationsDiv($, div) {
	const conjugations = [];
	const divs = $(div).children();
	for (let j = 0; j < divs.length; j++) {
		conjugations.push(parseConjugationDiv($, divs[j]));
	}
	return conjugations;
}

function parseDefinitionsDD($, dd) {
	const items = $(dd).children();
	let definitions = [];
	let conjugations = [];
	let compounds = [];
	let suffix = null;
	const unknown = [];

	for (let i = 0; i < items.length; i++) {
		if (items[i].type != "tag") continue;
		const tagname = items[i].name;
		const cls = items[i].attribs.class;
		if (tagname == "ol" && cls == "gloss-definitions") {
			definitions = definitions.concat(parseDefinitionsOL($, items[i]));
		} else if (tagname == "div" && cls == "conjugations") {
			conjugations = conjugations.concat(parseConjugationsDiv($, items[i]));
		} else if (tagname == "span" && cls == "compound-desc") {
			compounds.push({
				desc: $("span.compound-desc-word", items[i]).text().trim(),
				parts: null,
			});
		} else if (tagname == "dl" && cls == "compounds") {
			if (compounds.length > 0 && compounds[compounds.length - 1].parts == null) {
				compounds[compounds.length - 1].parts = parseDL($, items[i]);
			}
		} else if (tagname == "span" && cls == "suffix-desc") {
			suffix = $(items[i]).text().trim();
		} else {
			unknown.push("dd?? " + tagname + " " + cls);
		}
	}

	let result = {};
	if (definitions.length) result.definitions = definitions;
	if (conjugations.length) result.conjugations = conjugations;
	if (compounds.length) result.compounds = compounds;
	if (suffix != null) result.suffix = suffix;
	if (unknown.length) result.unknown = unknown;
	return result;
}

function parseWordLI($, li) {
	const romaji = $($(".gloss-rtext", li)[0]).text().trim();
	const dl = $(".gloss-content > dl.alternatives", li);
	const alternatives = parseDL($, dl);
	return {romaji, alternatives};
}

function parseIchimoeHTML(html) {
	const $ = cheerio.load(html);
	const words = [];
	const rows = $(".gloss-row");
	for (let i = 0; i < rows.length; i++) {
		if ($(rows[i]).hasClass("hidden")) continue;
		const lis = $($("ul", rows[i])[0]).children();
		for (let j = 0; j < lis.length; j++) {
			words.push(parseWordLI($, lis[j]));
		}
	}
	return words;
}

// const result = parseIchimoeHTML(require("fs").readFileSync("kioku.ichimoe.html"));
// console.log(util.inspect(result, {depth: Infinity, colors: true}));
// process.exit();

if (process.argv.length != 3) {
	console.error(`Usage: ./ichimoe-get.js <文>`);
	process.exit(1);
}

const word = process.argv[2];
const url = `https://ichi.moe/cl/qr/?q=${encodeURIComponent(word)}&r=htr`;

https.get(url, res => {
	if (res.statusCode != 200) {
		console.error(`Ichi.moe returned status code: ${res.statusCode}`);
		console.error(res.headers);
		process.exit(1);
	}

	let body = "";
	res.on("data", data => body += data);
	res.on("end", () => {
		const result = parseIchimoeHTML(body);
		console.log(util.inspect(result, {depth: Infinity}));
	});
});