-
Notifications
You must be signed in to change notification settings - Fork 0
/
getWeatherDataTest.js
99 lines (81 loc) · 2.62 KB
/
getWeatherDataTest.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
const fetch = require("node-fetch");
const queryString = require('query-string');
const moment = require("moment");
// set the Timelines GET endpoint as the target URL
const getTimelineURL = "https://api.tomorrow.io/v4/timelines";
// get your key from app.tomorrow.io/development/keys
const apikey = ; //Get apiKey string from credentials file
// pick the location, as a latlong pair
let location = [40.758, -73.9855];
// list the fields
const fields = [
"precipitationIntensity",
"precipitationType",
"windSpeed",
"windGust",
"windDirection",
"temperature",
"temperatureApparent",
"cloudCover",
"cloudBase",
"cloudCeiling",
"weatherCode",
];
// choose the unit system, either metric or imperial
const units = "imperial";
// set the timesteps, like "current", "1h" and "1d"
const timesteps = ["current", "1h", "1d"];
// configure the time frame up to 6 hours back and 15 days out
const now = moment.utc();
const startTime = moment.utc(now).add(0, "minutes").toISOString();
const endTime = moment.utc(now).add(1, "days").toISOString();
// specify the timezone, using standard IANA timezone format
const timezone = "America/New_York";
// request the timelines with all the query string parameters as options
const getTimelineParameters = queryString.stringify({
apikey,
location,
fields,
units,
timesteps,
startTime,
endTime,
timezone,
}, {arrayFormat: "comma"});
fetch(getTimelineURL + "?" + getTimelineParameters, {method: "GET"})
.then((result) => result.json())
.then((json) => console.log(json)
.catch((error) => console.error("error: " + err));
/*
---- [PREMIUM FEATURE] contact [email protected] ----
// set the Timelines POST endpoint as the target URL
const postTimelineURL = 'https://api.tomorrow.io/v4/timelines' + "?apikey=" + apikey;
fields = {
"precipitationIntensityMax",
"precipitationTypeMax",
"windSpeedAverage",
"temperatureMin",
"temperatureMax",
"cloudCoverMax"
}
location = {
"type":"Polygon",
"coordinates":
[[[-73.985043,40.753554],[-73.990724,40.75595],[-73.984726,40.764167],[-73.979057,40.761747],[-73.985043,40.753554]]]
}
const postTimelineOptions = {method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
location
fields,
units,
timesteps,
startTime,
endTime,
timezone
})};
fetch(postTimelineURL, postTimelineOptions)
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error('error: ' + err));
*/