Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

wikidata-search

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

wikidata-search - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

174

lib/wikidata-search.js

@@ -64,2 +64,80 @@ var request = require('request');

/**
* Resolve properties using a batch system. Just give a list of property/value IDs, and we
* go through 50 at a time (the API max). Once all are resolved, we activate the callback.
*
* @param selectedLanguage The language used to request the results.
* @param wikidataSearch The main WikidataSearch object containing options.
* @param propertyList List of property/value IDs to lookup.
* @param finishedCb Activated when all finished. A function of the form: function(error);
*/
_resolveProperties: function(selectedLanguage, wikidataSearch, propertyList, finishedCb) {
//We can only request 50 IDs at a time.
var trimmedList = [ ];
//If we have more than 50 properties to lookup, take the first 50 and trim the main list.
if (propertyList.length > 50) {
trimmedList = propertyList.slice(0, 50);
propertyList.splice(0, 50);
} else {
trimmedList = propertyList;
propertyList = [ ];
}
//Construct the request URL with our ids.
var combinedUri = 'https://' + wikidataSearch.options.apiHost + wikidataSearch.options.apiPath;
combinedUri = combinedUri + '?action=' + wikidataSearch.options.getAction;
combinedUri = combinedUri + '&languages=' + wikidataSearch.options.language;
combinedUri = combinedUri + '&redirects=yes';
combinedUri = combinedUri + '&props=labels&normalize=true';
combinedUri = combinedUri + '&ids=' + trimmedList.join('|');
combinedUri = combinedUri + '&format=json'; //we always want JSON back...
//Send out the request.
request({uri : combinedUri, method : 'GET' }, function (error, response, wikidataResponse) {
//Make sure we have no errors.
if (error)
return finishedCb(error);
if (response === undefined) {
return finishedCb('Undefined WikiData response');
} else if (response.statusCode === 200) {
var propertyResult = (typeof wikidataResponse === 'string') ? JSON.parse(wikidataResponse) : wikidataResponse;
for (var i in propertyResult.entities) {
var property = propertyResult.entities[i];
var propId = '';
var label = '';
//Get the property ID.
if (property.hasOwnProperty('id'))
propId = property.id;
//Make sure we have a label...
if (property.hasOwnProperty('labels') && property.labels.hasOwnProperty(selectedLanguage) && property.labels[selectedLanguage].hasOwnProperty('value'))
label = property.labels[selectedLanguage].value;
if (!propId || !label)
continue;
//Apply this property/text pair to our cache.
PROPERTY_CACHE[propId] = label;
}
if (propertyList.length > 0) //still need to resolve more properties? Keep going.
return wikidataSearch._resolveProperties(selectedLanguage, wikidataSearch, propertyList, finishedCb);
return finishedCb(); //otherwise we're good. Activate the final callback.
} else
return finishedCb('Request error: ' + (typeof response === 'string' ? response : JSON.stringify(response)));
});
},
set: function(key, value) {

@@ -372,83 +450,49 @@ this.options[key] = value;

//Reconstruct the request URL with our new ids.
combinedUri = 'https://' + wikidataSearch.options.apiHost + wikidataSearch.options.apiPath;
combinedUri = combinedUri + '?action=' + wikidataSearch.options.getAction;
combinedUri = combinedUri + '&languages=' + wikidataSearch.options.language;
combinedUri = combinedUri + '&redirects=yes';
combinedUri = combinedUri + '&props=labels&normalize=true';
combinedUri = combinedUri + '&ids=' + propertyArray.join('|');
combinedUri = combinedUri + '&format=json'; //we always want JSON back...
//This will recurse through our property list in batches of 50 (the API max) until
//all are accounted for. The PROPERTY_CACHE object is directly updated, so no
//return values are given in the callback function.
wikidataSearch._resolveProperties(selectedLanguage, wikidataSearch, propertyArray, function(error) {
//Send out the request.
request({uri : combinedUri, method : 'GET' }, function (error, response, wikidataResponse) {
//Make sure we have no errors.
if (error)
return cb({}, error);
return cb({'error' : true, 'details' : error});
if (response === undefined) {
return cb({}, 'Undefined WikiData response');
} else if (response.statusCode === 200) {
var propertyResult = (typeof wikidataResponse === 'string') ? JSON.parse(wikidataResponse) : wikidataResponse;
//Now just go through and update our entities from the global property cache.
for (var i = 0, len = entities.length; i < len; i++) {
for (var j = 0, claimLen = entities[i].claims.length; j < claimLen; j++) {
for (var i in propertyResult.entities) {
var property = propertyResult.entities[i];
var propCached = entities[i].claims[j].propertyCached;
var valCached = entities[i].claims[j].valueCached;
var propId = '';
var label = '';
//Remove the cached properties for final output.
delete entities[i].claims[j].propertyCached;
delete entities[i].claims[j].valueCached;
//Get the property ID.
if (property.hasOwnProperty('id'))
propId = property.id;
//Make sure we have a label...
if (property.hasOwnProperty('labels') && property.labels.hasOwnProperty(selectedLanguage) && property.labels[selectedLanguage].hasOwnProperty('value'))
label = property.labels[selectedLanguage].value;
if (!propId || !label)
//If we already replaced the property of this claim with text, then skip it.
if (propCached && valCached)
continue;
//Apply this property/text pair to our cache.
PROPERTY_CACHE[propId] = label;
}
//Now swap the IDs in the claim with the text version, if not cached
if (!propCached) {
var propertyId = entities[i].claims[j].property;
if (propertyId && (propertyId in PROPERTY_CACHE))
entities[i].claims[j].property = PROPERTY_CACHE[propertyId];
}
for (var i = 0, len = entities.length; i < len; i++) {
for (var j = 0, claimLen = entities[i].claims.length; j < claimLen; j++) {
var propCached = entities[i].claims[j].propertyCached;
var valCached = entities[i].claims[j].valueCached;
//Remove the cached properties for final output.
delete entities[i].claims[j].propertyCached;
delete entities[i].claims[j].valueCached;
//If we already replaced the property of this claim with text, then skip it.
if (propCached && valCached)
continue;
//Now swap the IDs in the claim with the text version, if not cached
if (!propCached) {
var propertyId = entities[i].claims[j].property;
if (propertyId && (propertyId in PROPERTY_CACHE))
entities[i].claims[j].property = PROPERTY_CACHE[propertyId];
if (!valCached) {
var valueId = entities[i].claims[j].value;
if (valueId && (valueId in PROPERTY_CACHE)) {
entities[i].claims[j].value = PROPERTY_CACHE[valueId];
//we replaced the given structure with a string, so update
//the type field to reflect that.
entities[i].claims[j].type = 'string';
}
if (!valCached) {
var valueId = entities[i].claims[j].value;
if (valueId && (valueId in PROPERTY_CACHE)) {
entities[i].claims[j].value = PROPERTY_CACHE[valueId];
//we replaced the given structure with a string, so update
//the type field to reflect that.
entities[i].claims[j].type = 'string';
}
}
}
}
}
return cb(entities, wikidataResponse.errors);
} else
return cb({}, 'Request error: ' + (typeof response === 'string' ? response : JSON.stringify(response)));
return cb(entities, wikidataResponse.errors);
});
} else

@@ -455,0 +499,0 @@ return cb({}, 'Request error: ' + (typeof response === 'string' ? response : JSON.stringify(response)));

{
"name": "wikidata-search",
"version": "1.0.0",
"version": "1.0.1",
"description": "Perform easy entity searches using Wikidata. Supply an entity name, and get back a bunch of structured data about that entity.",

@@ -5,0 +5,0 @@ "main": "./lib/wikidata-search.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc