wiki-entity 0.5.11 → 0.6.0

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/lib/index.js CHANGED
@@ -19,7 +19,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
19
19
  });
20
20
  };
21
21
  Object.defineProperty(exports, "__esModule", { value: true });
22
- exports.getEntities = exports.WikipediaApi = exports.simplifyEntity = void 0;
22
+ exports.mapRedirects = exports.getEntities = exports.WikipediaApi = exports.simplifyEntity = void 0;
23
23
  const wikidata_1 = require("./wikidata");
24
24
  const api_1 = require("./wikipedia/api");
25
25
  Object.defineProperty(exports, "WikipediaApi", { enumerable: true, get: function () { return api_1.Api; } });
@@ -29,10 +29,30 @@ var simplify_entity_1 = require("./wikidata/simplify_entity");
29
29
  Object.defineProperty(exports, "simplifyEntity", { enumerable: true, get: function () { return simplify_entity_1.simplifyEntity; } });
30
30
  __exportStar(require("./simpleEntity"), exports);
31
31
  __exportStar(require("./types"), exports);
32
+ const getAllEntities = (params) => __awaiter(void 0, void 0, void 0, function* () {
33
+ const entities = yield (0, wikidata_1.getEntities)(params);
34
+ const titles = params.titles || [];
35
+ if (params.redirect === "no" || !titles.length || params.ids)
36
+ return entities;
37
+ const lang = params.language || "en";
38
+ const notFoundTitle = titles.find((title) => !Object.entries(entities).find(([, entity]) => {
39
+ return (!entity.sitelinks ||
40
+ !entity.sitelinks[lang] ||
41
+ entity.sitelinks[lang] !== title);
42
+ }));
43
+ if (notFoundTitle.length === 0)
44
+ return entities;
45
+ const redirects = yield mapRedirects(titles, lang);
46
+ const redirectTitles = [...new Set(Object.values(redirects))];
47
+ if (redirectTitles.length === 0)
48
+ return entities;
49
+ const redirectEntities = yield (0, wikidata_1.getEntities)(Object.assign(Object.assign({}, params), { ids: undefined, titles: redirectTitles }));
50
+ return Object.assign(Object.assign({}, entities), redirectEntities);
51
+ });
32
52
  function getEntities(params) {
33
53
  return __awaiter(this, void 0, void 0, function* () {
34
54
  const lang = params.language || "en";
35
- const entities = yield (0, wikidata_1.getEntities)(params);
55
+ const entities = yield getAllEntities(params);
36
56
  const ids = Object.keys(entities).filter((id) => (0, utils_1.isValidWikiId)(id));
37
57
  if (ids.length === 0)
38
58
  return [];
@@ -100,3 +120,27 @@ function getEntities(params) {
100
120
  });
101
121
  }
102
122
  exports.getEntities = getEntities;
123
+ function mapRedirects(titles, lang) {
124
+ return __awaiter(this, void 0, void 0, function* () {
125
+ const wikiApi = new api_1.Api();
126
+ return wikiApi
127
+ .redirects()
128
+ .query(lang, {
129
+ titles: titles.map((it) => it.replace(/\s+/g, "_")).join("|"),
130
+ redirects: "yes"
131
+ })
132
+ .then((apiResults) => {
133
+ const result = {};
134
+ apiResults.forEach((r) => {
135
+ var _a;
136
+ if ((_a = r.redirects) === null || _a === void 0 ? void 0 : _a.length) {
137
+ const it = r.redirects.find((it) => titles.includes(it));
138
+ if (it && it !== r.title)
139
+ result[it] = r.title;
140
+ }
141
+ });
142
+ return result;
143
+ });
144
+ });
145
+ }
146
+ exports.mapRedirects = mapRedirects;
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getEntityCountryCode = void 0;
4
- const countries = require('../../data/countries.json');
4
+ const countries = require("../../data/countries.json");
5
5
  function getEntityCountryCode(wikiEntity) {
6
- if (!wikiEntity) {
6
+ if (!(wikiEntity === null || wikiEntity === void 0 ? void 0 : wikiEntity.claims)) {
7
7
  return null;
8
8
  }
9
9
  const countryIds = getEntityCountryIds(wikiEntity);
@@ -17,6 +17,9 @@ function getEntityCountryCode(wikiEntity) {
17
17
  }
18
18
  exports.getEntityCountryCode = getEntityCountryCode;
19
19
  function getEntityCountryIds(wikiEntity) {
20
- let prop = wikiEntity.claims.P17 || wikiEntity.claims.P27 || wikiEntity.claims.P495 || wikiEntity.claims.P1532;
21
- return prop && prop.values.map(item => item.value) || [];
20
+ let prop = wikiEntity.claims.P17 ||
21
+ wikiEntity.claims.P27 ||
22
+ wikiEntity.claims.P495 ||
23
+ wikiEntity.claims.P1532;
24
+ return (prop && prop.values.map((item) => item.value)) || [];
22
25
  }
@@ -62,7 +62,7 @@ function exploreEntitiesProperties(entities, lang) {
62
62
  }
63
63
  });
64
64
  });
65
- return null;
65
+ return entities;
66
66
  });
67
67
  }
68
68
  function exploreEntityClaims(claims, params) {
@@ -33,19 +33,19 @@ class Api {
33
33
  if (data && data.query && data.query.pages) {
34
34
  return Object.keys(data.query.pages)
35
35
  .map((id) => data.query.pages[id])
36
- .map((data) => {
36
+ .map((page) => {
37
37
  const item = {
38
- pageid: data.pageid,
39
- title: data.title
38
+ pageid: page.pageid,
39
+ title: page.title
40
40
  };
41
- if (data["categories"]) {
42
- item.categories = data["categories"].map((it) => it.title);
41
+ if (page["categories"]) {
42
+ item.categories = page["categories"].map((it) => it.title);
43
43
  }
44
- if (data["redirects"]) {
45
- item.redirects = data["redirects"].map((it) => it.title);
44
+ if (page["redirects"]) {
45
+ item.redirects = page["redirects"].map((it) => it.title);
46
46
  }
47
- if (data["extract"]) {
48
- item.extract = data["extract"];
47
+ if (page["extract"]) {
48
+ item.extract = page["extract"];
49
49
  }
50
50
  return item;
51
51
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wiki-entity",
3
- "version": "0.5.11",
3
+ "version": "0.6.0",
4
4
  "description": "Wiki entity fetcher",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./types/index.d.ts",
@@ -41,6 +41,6 @@
41
41
  "typescript": "latest"
42
42
  },
43
43
  "dependencies": {
44
- "axios": "^0.21.1"
44
+ "axios": "^1.6.0"
45
45
  }
46
46
  }
package/types/index.d.ts CHANGED
@@ -5,3 +5,4 @@ export { WikipediaApi };
5
5
  export * from "./simpleEntity";
6
6
  export * from "./types";
7
7
  export declare function getEntities(params: WikiEntitiesParams): Promise<WikiEntity[]>;
8
+ export declare function mapRedirects(titles: string[], lang: string): Promise<Record<string, string>>;