-
Notifications
You must be signed in to change notification settings - Fork 2
/
mountainhub.js
60 lines (53 loc) · 1.73 KB
/
mountainhub.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
const axios = require("axios");
const { generateId } = require("../utils")
const PROVIDER_URL = 'https://api.mountainhub.com/timeline';
const HEADER = { 'Accept-version': '1' };
const rawData = async function(min_date, max_date) {
const args = {
publisher : 'all',
limit : 10000,
since : min_date.getTime(),
before : max_date.getTime()
};
let snow_conditions_response;
let snowpack_test_response;
try {
snow_conditions_response = await axios.get(PROVIDER_URL, {params: { ...args, obs_type: 'snow_conditions' }, headers: HEADER});
snow_conditions_response = snow_conditions_response.data.results
} catch (error) {
console.error(error)
snow_conditions_response = []
}
try {
snowpack_test_response = await axios.get(PROVIDER_URL, {params: { ...args, obs_type: 'snowpack_test' }, headers: HEADER});
snowpack_test_response = snowpack_test_response.data.results
} catch (error) {
console.error(error)
snowpack_test_response = []
}
return snow_conditions_response.concat(snowpack_test_response);
}
const parseData = (record) => {
try {
const format = {
author_name: record.actor.full_name || record.actor.fullName,
id: generateId(record.observation._id),
timestamp: new Date(record.observation.reported_at),
lat: record.location.coordinates[1],
long: record.location.coordinates[0],
depth: Number(record.observation.details[0].snowpack_depth),
comment: record.observation.description,
type: record.observation.type,
source: "MountainHub"
};
if (isNaN(format.depth) || !format.depth) throw new Error("Snow Depth Undefined");
return format;
}
catch (error) {
return null;
}
}
module.exports = {
rawData,
parseData
}