2019-07-28 01:52:06 +09:00
|
|
|
const fetch = require("node-fetch");
|
|
|
|
|
2020-04-09 00:16:46 +09:00
|
|
|
module.exports = async (titles) => {
|
2019-07-28 01:52:06 +09:00
|
|
|
const url = new URL("https://ja.wikipedia.org/w/api.php");
|
|
|
|
const params = new URLSearchParams({
|
|
|
|
action: "query",
|
|
|
|
format: "json",
|
|
|
|
prop: "extracts",
|
|
|
|
titles,
|
|
|
|
redirects: "",
|
|
|
|
exchars: 120,
|
2020-04-09 00:16:46 +09:00
|
|
|
explaintext: "",
|
2019-07-28 01:52:06 +09:00
|
|
|
});
|
|
|
|
try {
|
|
|
|
const response = await fetch([url, params].join("?"));
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error([response.status, response.statusText].join(":"));
|
|
|
|
}
|
|
|
|
const json = await response.json();
|
|
|
|
if (json == null || json.query == null || json.query.pages == null) {
|
|
|
|
throw new Error(["response is invalid", JSON.stringify(json)].join(":"));
|
|
|
|
}
|
|
|
|
const pages = new Map(Object.entries(json.query.pages));
|
|
|
|
return pages.size > 0
|
|
|
|
? [...pages.values()].map(({ extract }) => extract).join("\n")
|
|
|
|
: "しらないにゃーん";
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|