wiki-entity 0.5.2 → 0.5.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -59,6 +59,7 @@ type WikiEntity = {
59
59
  types?: string[];
60
60
  redirects?: string[];
61
61
  categories?: string[];
62
+ redirectsToId?: string;
62
63
  }
63
64
 
64
65
  type WikidataProperty = {
@@ -113,6 +114,7 @@ export type SimpleEntity = {
113
114
  countryCodes?: string[]
114
115
  data?: SimpleEntityData
115
116
  categories?: string[]
117
+ redirectsToId?: string;
116
118
  }
117
119
 
118
120
  ```
package/lib/index.js CHANGED
@@ -1,4 +1,12 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
2
10
  function __export(m) {
3
11
  for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
4
12
  }
@@ -13,30 +21,21 @@ exports.simplifyEntity = simplify_entity_1.simplifyEntity;
13
21
  __export(require("./simpleEntity"));
14
22
  __export(require("./types"));
15
23
  function getEntities(params) {
16
- const lang = params.language || "en";
17
- return wikidata_1.getEntities(params)
18
- .then(function (wikiDataEntities) {
19
- const entities = wikiDataEntities;
24
+ return __awaiter(this, void 0, void 0, function* () {
25
+ const lang = params.language || "en";
26
+ const entities = yield wikidata_1.getEntities(params);
20
27
  const ids = Object.keys(entities).filter((id) => utils_1.isValidWikiId(id));
21
- if (ids.length === 0) {
22
- return entities;
23
- }
24
- const tasks = [];
28
+ if (ids.length === 0)
29
+ return [];
25
30
  const wikiApi = new api_1.Api({}, params.httpTimeout);
26
- let callWikiApi = false;
27
- if (params.extract) {
28
- callWikiApi = true;
31
+ if (params.extract)
29
32
  wikiApi.extract(params.extract);
30
- }
31
- if (params.redirects) {
32
- callWikiApi = true;
33
+ if (params.redirects)
33
34
  wikiApi.redirects();
34
- }
35
- if (params.categories) {
36
- callWikiApi = true;
35
+ if (params.categories)
37
36
  wikiApi.categories();
38
- }
39
- if (callWikiApi && entities[ids[0]] && entities[ids[0]].sitelinks) {
37
+ const tasks = [];
38
+ if (entities[ids[0]] && entities[ids[0]].sitelinks) {
40
39
  const titleIds = ids.reduce((prev, id) => {
41
40
  if (entities[id] &&
42
41
  entities[id].sitelinks &&
@@ -57,15 +56,13 @@ function getEntities(params) {
57
56
  })
58
57
  .then((apiResults) => apiResults.forEach((result) => {
59
58
  const entity = entities[titleIds[result.title]];
60
- if (params.extract) {
59
+ entity.pageid = result.pageid;
60
+ if (params.extract)
61
61
  entity.extract = result.extract;
62
- }
63
- if (params.redirects) {
62
+ if (params.redirects)
64
63
  entity.redirects = result.redirects;
65
- }
66
- if (params.categories) {
64
+ if (params.categories)
67
65
  entity.categories = result.categories;
68
- }
69
66
  })));
70
67
  }
71
68
  if (params.types === true || Array.isArray(params.types)) {
@@ -83,11 +80,9 @@ function getEntities(params) {
83
80
  }
84
81
  });
85
82
  }
86
- return Promise.all(tasks).then(() => entities);
87
- })
88
- .then(function (resultEntities) {
89
- return Object.keys(resultEntities)
90
- .map((id) => resultEntities[id])
83
+ yield Promise.all(tasks);
84
+ return Object.keys(entities)
85
+ .map((id) => entities[id])
91
86
  .filter((it) => !!it);
92
87
  });
93
88
  }
@@ -15,6 +15,8 @@ function convertToSimpleEntity(wikiEntity, lang, options) {
15
15
  entity.description = wikiEntity.description;
16
16
  entity.wikiPageId = wikiEntity.pageid;
17
17
  entity.about = wikiEntity.extract;
18
+ if (wikiEntity.redirectsToId)
19
+ entity.redirectsToId = wikiEntity.redirectsToId;
18
20
  if (wikiEntity.types) {
19
21
  entity.types = utils_1.uniq(wikiEntity.types.filter((item) => !/:(Thing|Agent)$/.test(item)));
20
22
  }
@@ -15,5 +15,5 @@ exports.SIMPLE_ENTITY_TYPES = [
15
15
  SimpleEntityType.PLACE,
16
16
  SimpleEntityType.ORG,
17
17
  SimpleEntityType.PRODUCT,
18
- SimpleEntityType.WORK,
18
+ SimpleEntityType.WORK
19
19
  ];
@@ -46,6 +46,9 @@ function getEntities(params) {
46
46
  throw getError(data);
47
47
  const entities = (data && data.entities) || {};
48
48
  for (let id of Object.keys(entities)) {
49
+ const entity = entities[id];
50
+ if (entity.redirects && entity.redirects.to)
51
+ entity.redirectsToId = entity.redirects.to;
49
52
  if (!utils_1.isValidWikiId(id) || ~Object.keys(entities[id]).indexOf("missing")) {
50
53
  delete entities[id];
51
54
  }
@@ -33,15 +33,17 @@ function parseTypes(types) {
33
33
  return types.map((item) => item.type.value);
34
34
  }
35
35
  function dbpediaTypes(name) {
36
- const query = `
37
- SELECT ?type
38
- WHERE {
39
- <http://dbpedia.org/resource/${encodeURIComponent(name.replace(/\s+/g, "_"))}> rdf:type ?type
40
- }`;
36
+ const url = `http://dbpedia.org/resource/${name.replace(/\s+/g, "_")}`;
37
+ const query = `SELECT ?type WHERE { <${url}> rdf:type ?type }`;
41
38
  return request_1.default("http://dbpedia.org/sparql", {
42
39
  params: { query: query },
43
- timeout: 10 * 1000
44
- }).then((data) => data.results && data.results.bindings);
40
+ timeout: 30 * 1000
41
+ })
42
+ .then((data) => data.results && data.results.bindings)
43
+ .catch((error) => {
44
+ console.error(`${error.message}: ${url}`);
45
+ return [];
46
+ });
45
47
  }
46
48
  function repairTypes(types) {
47
49
  const personIndex = types.findIndex((item) => /:Person$/.test(item));
@@ -100,7 +100,7 @@ function exploreEntityClaims(claims, params) {
100
100
  pa.forEach((pai) => {
101
101
  const val = claims[pai.pid].values[pai.index];
102
102
  for (var prop in item) {
103
- if (~["label", "pageid", "description"].indexOf(prop)) {
103
+ if (~["label", "description"].indexOf(prop)) {
104
104
  val[prop] = item[prop];
105
105
  }
106
106
  }
@@ -3,31 +3,28 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const simplify_claims_1 = require("./simplify_claims");
4
4
  function simplifyEntity(lang, data, options = {}) {
5
5
  const entity = { id: data.id };
6
- if (data.pageid) {
6
+ if (data.pageid)
7
7
  entity.pageid = data.pageid;
8
- }
8
+ if (data.redirectsToId)
9
+ entity.redirectsToId = data.redirectsToId;
9
10
  if (options.labels !== false && data.labels) {
10
11
  entity.labels = simplifyLabels(data.labels);
11
12
  entity.label = entity.labels[lang];
12
13
  }
13
14
  if (options.descriptions !== false && data.descriptions) {
14
15
  entity.description = simplifyDescriptions(data.descriptions)[lang];
15
- if (!entity.description) {
16
+ if (!entity.description)
16
17
  delete entity.description;
17
- }
18
18
  }
19
19
  if (options.aliases !== false && data.aliases) {
20
20
  entity.aliases = simplifyAliases(data.aliases)[lang];
21
- if (!entity.aliases) {
21
+ if (!entity.aliases)
22
22
  delete entity.aliases;
23
- }
24
23
  }
25
- if (options.sitelinks !== false && data.sitelinks) {
24
+ if (options.sitelinks !== false && data.sitelinks)
26
25
  entity.sitelinks = simplifySitelinks(data.sitelinks);
27
- }
28
- if (options.claims !== false && data.claims) {
26
+ if (options.claims !== false && data.claims)
29
27
  entity.claims = simplify_claims_1.simplifyClaims(data.claims);
30
- }
31
28
  return entity;
32
29
  }
33
30
  exports.simplifyEntity = simplifyEntity;
@@ -33,14 +33,14 @@ class Api {
33
33
  .map((id) => data.query.pages[id])
34
34
  .map((data) => {
35
35
  const item = {
36
- pageid: data["pageid"],
37
- title: data["title"]
36
+ pageid: data.pageid,
37
+ title: data.title
38
38
  };
39
39
  if (data["categories"]) {
40
- item.categories = data["categories"].map((item) => item.title);
40
+ item.categories = data["categories"].map((it) => it.title);
41
41
  }
42
42
  if (data["redirects"]) {
43
- item.redirects = data["redirects"].map((item) => item.title);
43
+ item.redirects = data["redirects"].map((it) => it.title);
44
44
  }
45
45
  if (data["extract"]) {
46
46
  item.extract = data["extract"];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wiki-entity",
3
- "version": "0.5.2",
3
+ "version": "0.5.6",
4
4
  "description": "Wiki entity fetcher",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./types/index.d.ts",
@@ -24,4 +24,5 @@ export declare type SimpleEntity = {
24
24
  countryCodes?: string[];
25
25
  data?: SimpleEntityData;
26
26
  categories?: string[];
27
+ redirectsToId?: string;
27
28
  };
package/types/types.d.ts CHANGED
@@ -35,6 +35,7 @@ export interface WikiEntity extends WikidataEntity {
35
35
  types?: string[];
36
36
  redirects?: string[];
37
37
  categories?: string[];
38
+ redirectsToId?: string;
38
39
  }
39
40
  export declare type WikiEntities = PlainObject<WikiEntity>;
40
41
  export declare type ParamClaimsType = "none" | "all" | "item" | "property";