-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.js
73 lines (68 loc) · 2.22 KB
/
weather.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
import axios from "axios";
export function getWeather(lat, lon, timezone) {
return axios
.get(
"https://api.open-meteo.com/v1/forecast?hourly=temperature_2m,apparent_temperature,precipitation,weathercode,windspeed_10m&daily=weathercode,temperature_2m_max,temperature_2m_min,apparent_temperature_max,apparent_temperature_min,precipitation_sum¤t_weather=true&temperature_unit=fahrenheit&windspeed_unit=mph&precipitation_unit=inch&timeformat=unixtime",
{
params: {
latitude: lat,
longitude: lon,
timezone,
},
}
)
.then(({ data }) => {
return {
current: parseCurrentWeather(data),
daily: parseDailyWeather(data),
hourly: parseHourlyWeather(data),
};
});
}
function parseCurrentWeather({ current_weather, daily }) {
const {
temperature: currentTemp,
windspeed: windSpeed,
weathercode: iconCode,
} = current_weather;
const {
temperature_2m_max: [maxTemp],
temperature_2m_min: [minTemp],
apparent_temperature_max: [maxFeelsLike],
apparent_temperature_min: [minFeelsLike],
precipitation_sum: [precip],
} = daily;
return {
currentTemp: Math.round(currentTemp),
highTemp: Math.round(maxTemp),
lowTemp: Math.round(minTemp),
highFeelsLike: Math.round(maxFeelsLike),
lowFeelsLike: Math.round(minFeelsLike),
windSpeed: Math.round(windSpeed),
precip: Math.round(precip * 100) / 100,
iconCode,
};
}
function parseDailyWeather({ daily }) {
return daily.time.map((time, index) => {
return {
timestamp: time * 1000,
iconCode: daily.weathercode[index],
maxTemp: Math.round(daily.temperature_2m_max[index]),
};
});
}
function parseHourlyWeather({ hourly, current_weather }) {
return hourly.time
.map((time, index) => {
return {
timestamp: time * 1000,
iconCode: hourly.weathercode[index],
temp: Math.round(hourly.temperature_2m[index]),
feelsLike: Math.round(hourly.apparent_temperature[index]),
windSpeed: Math.round(hourly.windspeed_10m[index]),
precip: Math.round(hourly.precipitation[index] * 100) / 100,
};
})
.filter(({ timestamp }) => timestamp >= current_weather.time * 1000);
}