This repository has been archived by the owner on Feb 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
203 lines (193 loc) Β· 7.14 KB
/
index.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const XLSX = require('node-xlsx').default;
const path = require('path');
const _ = require('lodash');
const fs = require('fs');
const ObjectID = require('mongodb').ObjectID;
const gardenSheet = XLSX.parse(path.join(__dirname, './garden.xlsx'));
const herbariumnSheet = XLSX.parse(path.join(__dirname, './herbarium.xlsx'));
let amount = process.env.JSON_PARSE_AMOUNT || 999999999;
exports.setAmount = function setAmount(_amount) {
amount = _amount;
}
function encodeRFC5987ValueChars(str) {
return encodeURI(str).
replace(/['()]/g, escape)
}
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
function getScinameKey(sciname) {
const scinameSplit = sciname.split(' ');
return `${scinameSplit[0]} ${scinameSplit[1]}`;
}
function normailizeScientificName(name) {
return name.replace(new RegExp(/\.|[(ΰΉΰΈ‘ΰΉΰΉΰΈΰΈ)]|ΰΈΊ|\//, 'gi'), '')
.replace(new RegExp(/,/), ' ')
.replace(new RegExp(/(\s\s)/), ' ')
.replace(new RegExp(/Β /, 'gi'), ' ')
.replace(new RegExp(/(\s\s)/), ' ')
.toLowerCase()
.trim();
}
let _imagePath = '../static/images/stock/';
exports.setImagePath = (path) => {
_imagePath = path;
}
const findImages = (category, nameOrCuid) => {
if (!nameOrCuid) {
return [];
}
let name = nameOrCuid.replace(new RegExp(/\.+$/, 'gi'), '');
let words = name.split(' ');
if (words.length < 2) {
return [];
}
let images = [];
try{
switch (category) {
case 'museum': {
const dirTest = new RegExp(`${escapeRegExp(words[0])}.*${escapeRegExp(words[1])}`, 'gi');
const dirList = fs.readdirSync(path.join(__dirname, `${_imagePath}${category}`));
dirList.map(dirName => {
if (dirTest.test(dirName)) {
const dirPath = path.join(__dirname, `${_imagePath}${category}/${dirName}`);
if (fs.existsSync(dirPath)) {
const files = fs.readdirSync(dirPath);
images = files.map(file => ({
url: encodeRFC5987ValueChars(`/static/images/stock/${category}/${dirName}/${file}`),
}))
}
}
});
}
break;
case 'herbarium': {
const files = fs.readdirSync(path.join(__dirname, `${_imagePath}herbarium`));
files.forEach(file => {
if ((new RegExp(escapeRegExp(nameOrCuid), 'g')).test(file)) {
images.push({
url: encodeRFC5987ValueChars(`/static/images/stock/${category}/${file}`),
})
}
})
}
}
}catch(e){
}
if (images.length > 0) {
console.log('Image found : ' + name + `, amount: ${images.length}`)
}
return images;
}
const filterOnlyEnglish = (input) => {
const text = new RegExp(/^[a-zA-Z0-9?><\\;,βΓ©ΓͺΓΌ{}()[\]\-_+=!@#$%\^&*|'"\s|\.Γ]*$/, 'i');
const str = normailizeScientificName(input);
if (!text.test(str)) {
// console.log('Remove plant with invalide scientific name : ' + str);
}
return text.test(str);
}
exports.filterOnlyEnglish = filterOnlyEnglish;
let scientificNameKeyCollection = {};
let noPlantIdfiltered = [];
exports.getNoPlantIdItem = () => {
return noPlantIdfiltered;
}
exports.getPlantFromDataSheet = () => {
const scientificNamesFromHerbarium = herbariumnSheet[0].data
.filter((item, i) => i > 0 && i < amount)
.filter((item) => item[4])
.filter((item) => filterOnlyEnglish(item[4]))
.filter((item) => item[4] !== '_')
.filter((item) => item[4] !== '-')
.map(item => {
const _id = ObjectID();
const sciname = normailizeScientificName(item[4]);
scientificNameKeyCollection[getScinameKey(sciname)] = _id;
return {
_id,
scientificName: sciname,
familyName: item[8] ? item[8].trim() : null,
name: (item[5] || item[6] || '').replace(new RegExp(/_|\s+$/, 'gi'), '')
}
});
const scientificNamesFromGarden = gardenSheet[0].data
.filter((item, i) => i > 0 && i < amount)
.filter(item => item[1])
.filter((item) => filterOnlyEnglish(item[1]))
.filter((item) => item[1] !== '_')
.map(item => {
const _id = ObjectID();
const sciname = normailizeScientificName(item[1]);
scientificNameKeyCollection[getScinameKey(sciname)] = _id
return {
_id,
scientificName: normailizeScientificName(item[1]),
familyName: item[2] ? item[2].trim() : null,
name: (item[0] || '').replace(new RegExp(/_|\s+$/, 'gi'), ''),
}
});
const scientificNames = _.union(scientificNamesFromGarden, scientificNamesFromHerbarium);
return _.uniqBy(scientificNames, item => item.scientificName);
}
exports.getMuseumFromDataSheet = () => {
const museums = gardenSheet[1].data
.filter((item, i) => i > 0 && i < amount)
.filter(item => item[2])
.filter((item) => filterOnlyEnglish(item[2]))
.map(item => {
const sciname = normailizeScientificName(item[2]);
return ({
plantId: scientificNameKeyCollection[getScinameKey(sciname)],
scientificName: sciname,
museumLocation: item[3],
images: findImages('museum', item[2])
})
});
return museums.filter(item => {
if (!item.plantId) {
// console.log('remove item with no plant id');
// console.log(item);
noPlantIdfiltered.push(item.scientificName);
}
return item.plantId
});
}
exports.getGardenFromDataSheet = () => {
const gardens = gardenSheet[2].data
.filter((item, i) => i > 0 && item[0] && i < amount)
.map(item => {
const sciname = normailizeScientificName(item[0]);
return {
plantId: scientificNameKeyCollection[getScinameKey(sciname)],
zone: item[2],
scientificName: normailizeScientificName(item[0]),
images: findImages('garden', item[0]),
}
});
return gardens.filter(
item => {
if (!item.plantId) {
// console.log('remove item with no plant id');
// console.log(item);
noPlantIdfiltered.push(item.scientificName);
}
return item.plantId;
}
);
}
exports.getHerbariumFromDataSheet = () => {
const herbariums = herbariumnSheet[0].data
.filter((item, i) => i > 0 && i < amount)
.filter(item => item[4])
.map(item => ({
scientificName: normailizeScientificName(item[4]),
discoverLocation: item[12],
displayLocation: 'ΰΈΰΈΉΰΉ ' + item[1] + ' ΰΈΰΉΰΈΰΈ ' + item[2],
cuid: item[0],
collectedDate: new Date(item[15]),
collector: item[10],
images: findImages('herbarium', item[0]),
}))
return herbariums;
}