-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
275 lines (230 loc) · 7.53 KB
/
main.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// External Node Modules
const axios = require('axios'); // HTTP Client for Node.js
// Local Files
const config = require('./config.json'); // Mostly just for the API token
// Version
/**
* @typedef {object} VersionData
* @property {string} version - String listing the API version as a date in YYYY-MM format.
*/
//Countries
//List
/**
* @typedef {object} CountryListing
* @property {string} isocode - The ISO code (two letter) for the country.
* @property {string} country - The countries full name
*/
/**
* @typedef {object} CountryListData
* @property {number} count - Number of entries that are returned.
* @property {CountryListing[]} results - Array of matching entries that are returned, with size of count.
*/
//bySpecies
// https://www.wikidata.org/wiki/Property:P627 for taxonid, checking this later might want to add more info
/**
* @typedef {object} SpeciesListing
* @property {number} taxonid - IUCN Taxon ID Number
* @property {?string} scientific_name - The species' scientific name, else null.
* @property {?string} subspecies - The subspecies name, if it exists, else null.
* @property {?string} subpopulation - The subpopulation name, if it exists, else null.
* @property {string} category - The IUCN Red List Category (abbv.)
*/
/**
* @typedef {object} CountrySpeciesData
* @property {number} count - Number of entries that are returned.
* @property {string} country - Country (seemingly not working, always is US)
* @property {string} subcountry - Subcountry (seemingly not working, always is MAS-OO)
* @property {SpeciesListing[]} result - Array of matching entries that are returned, with size of count.
*/
//Regions
/**
* @typedef {object} RegionsListing
* @property {string} name - Long form name of the region.
* @property {string} identifier - Lowercase and underscored name of the region.
*/
/**
* @typedef {object} RegionsData
* @property {number} count - Number of entries that are returned.
* @property {RegionsListing[]} results - Array of matching entries that are returned, with size of count.
*/
//Species
//:(
//Threats
/**
* @typedef {object} ThreatsListing
* @property {string} code - Numerical code (formatted X.Y) representing the threat.
* @property {string} title - Title of the threat.
* @property {string} timing - Timing/current status of the threat (seems like all are marked Ongoing).
* @property {string} scope - The scope of the threat, can have < and > symbols so it might need conversion.
* @property {string} severity - The severity of the threat.
* @property {string} score - A score representing the impact the threat has had.
* @property {?string} invasive - Presumably if the threat is an invasive species
*/
/**
* @typedef {object} ThreatsData
* @property {string} id - String representing the species' taxonid number
* @property {string} [region_identifier] - The region ID, if it is supplied to {@link threats.id}
* @property {ThreatsListing[]} - Array of matching entries that are returned, with size of count.
*/
/**
* @class
* @classdesc Wrapper class for the IUCN Red List API
*/
class IUCNRedList {
/**
* IUCNRedList Constructor
* @constructs
* @param {string} apiKey - Your IUCN Red List Api Token.
*/
constructor(apiKey) {
axios.defaults.params = {}
axios.defaults.baseURL = 'https://apiv3.iucnredlist.org/api/v3/'
axios.defaults.params['token'] = apiKey
// axios.defaults.geaders
// axios.defaults.headers.common['Authorization'] = `Bearer ${apiKey}`; // ignore for now
// Yeah the info I saw was unreliable but it should work now
}
/**
* Checks the IUCN API Version
* @returns {VersionData}
*/
async version() {
const { data } = await axios.get('/version', {'token': null});
return data;
}
// TODO: Change to class later
// TODO: add species by country subcommand
// async countries() {
// const { data } = await axios.get('/country/list');
// return data;
// }
// could do this *or* could just do countries and just add the country code if its sent
countries = {
/**
* Lists the countries in the IUCN Database
* @memberof IUCNRedList
* @async
* @returns {CountryListData}
*/
list: async () => {
const { data } = await axios.get('/country/list');
return data;
},
/**
* Lists the species by country
* @memberof IUCNRedList
* @async
* @param {string} countryCode - ISO code (two letter) for the desired country
* @returns {CountrySpeciesData}
*/
bySpecies: async (countryCode) => {
const { data } = await axios.get('/country/getspecies/' + countryCode);
return data;
},
};
/**
* Gets the list of IUCN regions
* @returns {RegionsData}
*/
async regions() {
const { data } = await axios.get('/region/list');
return data;
}
// this is currently formatted like the iucn red list api page, but it kinda sucks and bloats
// the species listing to be a completely absurd number, will probably split it up after they
// are all implememented and working right.
species = {
list: {},
count: {},
citation: {},
byCategory: {},
specific: {} //by name, synonym, ID
}
// TODO: Change to class later
// TODO: Seperate to global, regional
// TODO: add species count (global, regional)
// TODO: add citations by name (global, regional)
// ...
// ...
// ...
async species() {
}
threats = {
/**
* Lists the threats to a specific species by its name
* @memberof IUCNRedList
* @async
* @param {string} name - Scientific name of the desired species.
* @param {string} [regionID] - Regional Identifying number for the desired region.
* @returns {ThreatsData}
*/
name: async (name, regionID = null) => {
let common = 'threats/species/'
if (regionID) {
const { data } = await axios.get(common + name + '/region/' + regionID)
return data;
}
else {
const { data } = await axios.get(common + 'name/' + name);
return data;
}
console.error('how are you here :3');
},
/**
* Lists the threats to a specific species by its taxonid (id number)
* @memberof IUCNRedList
* @async
* @param {string} id - Taxonid of the desired species.
* @param {string} [regionID] - Regional Identifying number for the desired region.
* @returns {ThreatsData}
*/
id: async (id, regionID = null) => {
let common = 'threats/species/id/' + id
common += regionID ? '/region/' + regionID : ''
const { data } = await axios.get(common);
return data;
}
}
// TODO: Change to class later
// TODO: add threats by name (global, regional)
// TODO: add threats by id (global, regional)
async threats() {
}
// TODO: Change to class later
// TODO: add habitats by species name (global, regional)
// TODO: add habitats by species id (global, regional)
async habitats() {
}
// TODO: Change to class later
// TODO: add CM by name
// TODO: add CM by id ()
async conservationMeasures() {
}
// TODO: Change to class later
// TODO:
// TODO:
async plantGrowthForms() {
}
// TODO: Change to class later
// TODO:
// TODO:
async comprehensiveGroups() {
}
// TODO: Add this group as a class
// Note that no API token is needed
// TODO: Red list website direct link (name of species)
// TODO: Red list website redirect (name of species)
// TODO: Red list website redirect (species id)
// TODO: Red list website redirect per region (species id / region id)
// class RedListLinking {
// constructor(a) {
// this.a = a
// this.tokenOverride = {'token': null};
// }
// async directLink(name) {
// const { data } = await axios.get('/weblink/' + name, {'token': null});
// return data;
// }
// }
}
module.exports = IUCNRedList;