Skip to content

Instantly share code, notes, and snippets.

@yuiseki
Created April 18, 2021 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuiseki/3ec513ad1df755c67630376ddc31500e to your computer and use it in GitHub Desktop.
Save yuiseki/3ec513ad1df755c67630376ddc31500e to your computer and use it in GitHub Desktop.
const fs = require('fs').promises;
const fetch = require('node-fetch');
const { transform } = require('@moneyforward/stream-util');
const WBK = require('wikibase-sdk');
const wdk = WBK({
instance: 'https://www.wikidata.org',
sparqlEndpoint: 'https://query.wikidata.org/sparql'
});
let entities = require('./entities.json');
const getEntities = async (id) => {
const url = wdk.getEntities({
ids: [id],
languages: ['ja'],
props: ['labels', 'descriptions']
});
const res = await fetch(url);
const json = await res.json();
const entities = await wdk.parse.wb.entities(json);
return entities
}
const getLabel = async (id) => {
let entity;
if (entities[id]) {
entity = entities[id]
} else {
result = await getEntities(id);
entity = result[id];
entities = Object.assign(entities, result);
await fs.writeFile('./entities.json', JSON.stringify(entities));
}
let label;
if (entity.labels['ja']) {
label = entity.labels.ja;
} else {
label = entity.descriptions.ja;
}
return label;
}
const snakToLanguage = async (snak) => {
const propertyLabel = await getLabel(snak.property);
snak.propertyLabel = propertyLabel;
switch (snak.datatype) {
case 'wikibase-item':
const qualifierLabel = await getLabel(snak.datavalue.value.id);
snak.datavalue.value.label = qualifierLabel;
break;
case 'time':
const time = new Date(snak.datavalue.value.time);
snak.datavalue.value.unixtime = time.getTime();
break;
default:
break;
}
return snak;
}
/*
const mapSnaks = async (snaks) => {
let ret = {};
for (const snak of snaks) {
for (const [key, value] of Object.entries(snak.snaks)) {
for (const item of value) {
let data = await snakToLanguage(item);
ret = Object.assign(ret, data);
}
}
}
return ret;
}
*/
const mapClaims = async (claims) => {
let ret = {};
for (const [key, value] of Object.entries(claims)) {
ret[key] = [];
for (const claim of value) {
let data = await snakToLanguage(claim.mainsnak);
/*
if (claim.qualifiers) {
await mapSnaks(claim.qualifiers);
}
if (claim.references) {
await mapSnaks(claim.references);
}
*/
ret[key].push({'mainsnack': data});
}
}
return ret;
}
(async () => {
for await (const line of process.stdin.pipe(new transform.Lines())) {
let data = JSON.parse(line);
const claims = await mapClaims(data.claims);
data.claims = Object.assign(data.claims, claims)
console.log(JSON.stringify(data, null, 2))
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment