-
Notifications
You must be signed in to change notification settings - Fork 2
/
DeadOrAlive.js
160 lines (134 loc) · 4.57 KB
/
DeadOrAlive.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const axios = require('axios');
const qs = require('qs');
const moment = require('moment');
const wiki = require('wikidata-sdk');
const overrides = require('./Overrides');
const { WIKIDATA_ERROR } = require('./constants');
const WikiDataDateFormat = "'+'YYYY-MM-DD'T'hh:mm:ss'Z'";
const DefaultDateFormat = 'MMMM Do YYYY';
axios.interceptors.request.use((request) => {
if (request.method === 'post' && request.headers['Content-Type'] === 'application/x-www-form-urlencoded') {
request.data = qs.stringify(request.data);
}
return request;
});
const parseWikipediaUrl = (title) => {
const parsedTitle = title
.replace(/ /g, '_')
.replace(/\(/g, '%28')
.replace(/\)/g, '%29');
return `https://en.wikipedia.org/wiki/${parsedTitle}`;
};
const getEntityIds = async (searchTerm) => {
const url = wiki.searchEntities({
search: searchTerm,
format: 'json',
});
const searchResult = await axios.get(url);
if (searchResult.data.error) {
throw new Error(WIKIDATA_ERROR);
}
if (!searchResult.data.search || searchResult.data.search.length === 0) {
throw new Error('not-found');
}
return searchResult.data.search.map((entity) => entity.id).slice(0, 5);
};
const getEntity = async (entityId) => {
const entityUrl = wiki.getEntities(entityId);
const result = await axios.get(entityUrl);
return result.data.entities[entityId];
};
const getEntities = (entityIds) => Promise.all(entityIds.map((entityId) => getEntity(entityId)));
const getFirstHumanEntity = (entities) => {
const personEntity = entities.find((entity) => {
if (entity.claims.P31 === undefined) return null;
const instanceOfValue = entity.claims.P31[0].mainsnak.datavalue.value.id;
return instanceOfValue === 'Q5';
});
if (personEntity === undefined || personEntity.sitelinks.enwiki === undefined) {
throw new Error('not-found');
}
return personEntity;
};
const getPersonModel = (personEntity) => {
const {
P569: birthData,
P570: deathData
} = personEntity.claims;
const name = personEntity.labels.en.value;
const url = parseWikipediaUrl(personEntity.sitelinks.enwiki.title);
const hasDOB = birthData !== undefined;
const isDead = deathData !== undefined;
let dateOfBirth = null;
if (hasDOB) {
const dateOfBirthString = birthData[0].mainsnak.datavalue.value.time;
dateOfBirth = moment(dateOfBirthString, WikiDataDateFormat).toDate();
}
let dateOfDeath = null;
if (isDead) {
const dateOfDeathString = deathData[0].mainsnak.datavalue.value.time;
dateOfDeath = moment(dateOfDeathString, WikiDataDateFormat).toDate();
}
return {
name,
dateOfBirth,
dateOfDeath,
url
};
};
const getResultModel = (personModel) => {
const hasDOB = personModel.dateOfBirth !== null;
const isDead = personModel.dateOfDeath !== null;
const { customMessage } = personModel;
let age;
let dateOfDeathFormatted = null;
if (hasDOB) {
const dateOfBirth = moment(personModel.dateOfBirth);
age = moment().diff(dateOfBirth, 'years');
if (isDead) {
const dateOfDeath = moment(personModel.dateOfDeath);
age = dateOfDeath.diff(dateOfBirth, 'years');
dateOfDeathFormatted = dateOfDeath.format(DefaultDateFormat);
}
}
return {
name: personModel.name,
age,
hasDOB,
isDead,
dateOfDeath: dateOfDeathFormatted,
url: personModel.url,
customMessage,
};
};
const matchSearchToOverride = (arrayOrString, searchTerm) => {
if (typeof arrayOrString !== 'object') {
return arrayOrString === searchTerm;
}
return arrayOrString.find((x) => x === searchTerm);
};
const search = async (searchTerm) => {
// search for override terms first
const overrideModel = overrides.find(
(override) => matchSearchToOverride(override.overrideSearchTerm, searchTerm.toLowerCase())
);
if (overrideModel) return getResultModel(overrideModel);
const entityIds = await getEntityIds(searchTerm);
const entities = await getEntities(entityIds);
const personEntity = getFirstHumanEntity(entities);
const wikipediaModel = getPersonModel(personEntity);
return getResultModel(wikipediaModel);
};
module.exports = {
search,
_private: {
getEntities,
getEntity,
getEntityIds,
getFirstHumanEntity,
getPersonModel,
getResultModel,
matchSearchToOverride,
parseWikipediaUrl
},
};