Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
});
} catch (err) { /* ignore */ }
// what to fetch
let _brands = fileTree.read('brands');
let _wikidata = gatherQIDs(_brands);
let _qids = Object.keys(_wikidata);
let _total = _qids.length;
if (!_total) {
console.log('Nothing to fetch');
process.exit();
}
// split into several wikidata requests
let _urls = wdk.getManyEntities({
ids: _qids,
languages: ['en'],
props: ['info', 'labels', 'descriptions', 'claims'],
format: 'json'
});
let _errors = [];
doFetch().then(finish);
function gatherQIDs(brands) {
let wikidata = {};
Object.keys(brands).forEach(kvnd => {
['brand:wikidata', 'operator:wikidata'].forEach(t => {
let qid = brands[kvnd].tags[t];
if (qid && /^Q\d+$/.test(qid)) {
, QProgress
if ( progressBar ) { QProgress = new progress(
'[:bar] Processing entity :current/:total: :q (eta :etas)',
{
complete: '='.green,
width: 20,
total: entKeys.length
}
) } else console.log( 'Processing recieved entity data...' )
for ( var entIndex = 0; entIndex < entKeys.length; entIndex++ ) {
var entKey = entKeys [ entIndex ]
, entity = entities[ entKey ]
entities[ entKey ] = wdk.simplifyClaims( entity.claims )
entity = entities[ entKey ]
var json = { wikiID: entKey, id: entKey }
, props = Object.keys( entity )
for ( var propIndex = 0; propIndex < props.length; propIndex++ ) {
var prop = props[ propIndex ]
, value = entity[ prop ]
var resp = parseProp( prop, value, lang )
json[ resp[ 0 ] ] = resp[ 1 ]
// Removing empty responses
delete json[ '' ]
function getEntitySourceURI(queryString) {
// the wdk used below, actually uses the wikidata php api
return wdk.searchEntities({
search: queryString,
format: 'json',
language: 'en',
limit: 5
})
}
const filterLanguages = languages != null
// keep only the desired attributes
if (filterAttributes) entity = pick(entity, keep)
// with the desired languages
if (filterLanguages) {
attributesWithLanguages.forEach(attr => {
if (entity[attr]) entity[attr] = pick(entity[attr], languages)
})
entity.sitelinks = keepMatchingSitelinks(entity.sitelinks, languages)
}
// with simplify claims and text attributes if requested
if (simplify) {
entity = wdk.simplify.entity(entity, simplifyOptions)
}
return entity
}
}
}
// P3836 - Pinterest ID
const pinterestUser = getClaimValue(entity, 'P3836');
if (pinterestUser) {
target.identities.pinterest = pinterestUser;
}
// P4264 - LinkedIn Company ID
const linkedinUser = getClaimValue(entity, 'P4264');
if (linkedinUser) {
target.identities.linkedin = linkedinUser;
}
// P576 - Dissolution date
wdk.simplify.propertyClaims(entity.claims.P576, { keepQualifiers: true }).forEach(item => {
let dissolution = { date: item.value };
if (item.qualifiers) {
// P17 - Countries where the brand is dissoluted
let countries = item.qualifiers.P17;
if (countries) {
dissolution.countries = countries;
countryCodesQueue.push({ qid: qid, index: target.dissolutions.length, countries: countries });
}
// P156 - followed by or P1366 - replaced by (successor)
let successor = item.qualifiers.P156 || item.qualifiers.P1366;
if (successor) {
dissolution.upgrade = successor;
}
}
Object.keys(result.entities).forEach(qid => {
let entity = result.entities[qid];
let target = _data.wikidata[qid];
let entry = _brands[target];
let sitelinks = getSitelinks(entity);
let claims = wdk.simplify.claims(entity.claims, {
keepReferences: true
});
let instance = entity.claims && entity.claims.P31;
let tag = entry.tags['brand:wikidata'] === qid ? 'brand' : 'operator';
let wikipedia = entry.tags[`${tag}:wikipedia`];
// Wikidata entity was either deleted or is a redirect
if (entity.missing === '') {
return _deletedWikidata.push([target, qid, `${tag}:wikidata`]);
}
// If there is a Wikidata entity specified but no Wikipedia article,
// try to find a matching article from all possible sitelinks
if (!wikipedia && sitelinks.length) {
_foundSitelink.push([target, qid, `${tag}:wikidata`, sitelinks.join(', ')]);
if (!entity.claims) return;
if (!entity.claims[prop]) return;
let value;
for (let i = 0; i < entity.claims[prop].length; i++) {
let c = entity.claims[prop][i];
if (c.rank === 'deprecated') continue;
if (c.mainsnak.snaktype !== 'value') continue;
// skip if we find an end time qualifier - P582
let ended = false;
let qualifiers = (c.qualifiers && c.qualifiers.P582) || [];
for (let j = 0; j < qualifiers.length; j++) {
let q = qualifiers[j];
if (q.snaktype !== 'value') continue;
let enddate = wdk.wikidataTimeToDateObject(q.datavalue.value.time);
if (new Date() > enddate) {
ended = true;
break;
}
}
if (ended) continue;
value = c.mainsnak.datavalue.value;
if (c.rank === 'preferred') return value; // return immediately
}
return value;
}
function getWikidataPlaceInfo(action, next) {
const id = action.feature.properties.wikidata;
const url = wdk.getEntities({
ids: id,
languages: ["en"]
});
fetch(url, { method: "get" })
.then(res => {
if (res.ok) {
return res.json();
} else {
// 4xx or 5xx response
var err = new Error(res.statusText);
return Promise.reject(err);
}
})
.then(data => {
// Success
module.exports = (pattern, filter) => data => {
// Special case: when the pattern is a property id, just log the property
if (wdk.isPropertyId(pattern) && data[pattern] != null) {
const obj = {}
obj[pattern] = data[pattern]
return JSON.stringify(obj, null, 2)
}
var text = '{'
for (let key in data) {
let value = data[key]
if (!filter || filter(value)) {
let bla = `\n "${key}": ${spacedInlinedJson(value)},`
text += bla
}
}
// Remove the last comma
text = text.replace(/,$/, '')
text += '\n}'
const { P, QHash, negatedProp, filterByClaimValue } = claimsFilterData
// filter-out this entity unless it has claims of the desired property
var propClaims = claims[P]
if (propClaims && propClaims.length > 0) {
if (negatedProp && !filterByClaimValue) return false
} else {
if (!negatedProp) return false
}
// let the possibility to let the claim value unspecified
// ex: wikidata-filter --claim P184
if (filterByClaimValue) {
// filter-out this entity unless it has a claim
// of the desired property with the desired value
propClaims = wdk.simplifyPropertyClaims(propClaims)
if (haveAMatch(QHash, propClaims)) {
if (negatedProp) return false
} else {
if (!negatedProp) return false
}
}
return true
}