You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
2.5 KiB
JavaScript

const getDailyData = async () => {
const response = await fetch(
'https://api.open-meteo.com/v1/forecast?latitude=43.7085&longitude=10.4036&hourly=temperature_2m,relative_humidity_2m,apparent_temperature,precipitation,weather_code&timezone=Europe%2FBerlin&forecast_days=3',
)
const body = await response.json()
let today = {}
const startTime = new Date().getHours() + 1
for (let i = startTime; i <= 23; i++) {
today[i] = {
temperature: body.hourly.temperature_2m[i],
precipitation: body.hourly.precipitation[i],
apparentTemperature: body.hourly.apparent_temperature[i],
weatherCode: body.hourly.weather_code[i],
}
}
let tomorrow = {}
for (let i = 0; i <= 23; i++) {
tomorrow[i] = {
temperature: body.hourly.temperature_2m[24 + i],
precipitation: body.hourly.precipitation[24 + i],
apparentTemperature: body.hourly.apparent_temperature[24 + i],
weatherCode: body.hourly.weather_code[24 + i],
}
}
let dayAfterTomorrow = {}
for (let i = 0; i <= 23; i++) {
dayAfterTomorrow[i] = {
temperature: body.hourly.temperature_2m[48 + i],
precipitation: body.hourly.precipitation[48 + i],
apparentTemperature: body.hourly.apparent_temperature[48 + i],
weatherCode: body.hourly.weather_code[48 + i],
}
}
return {
today,
tomorrow,
dayAfterTomorrow,
}
}
const getWeekData = async () => {
const response = await fetch(
'https://api.open-meteo.com/v1/forecast?latitude=43.7085&longitude=10.4036&daily=weather_code,temperature_2m_max,temperature_2m_min,apparent_temperature_max,apparent_temperature_min,precipitation_sum&timezone=Europe%2FBerlin',
)
const body = await response.json()
let week = {}
for (let i = 0; i < 6; i++) {
week[i] = {
minimumTemperature: body.daily.temperature_2m_min[i],
maximumTemperature: body.daily.temperature_2m_max[i],
minimumApparentTemperature: body.daily.apparent_temperature_min[i],
maximumApparentTemperature: body.daily.apparent_temperature_max[i],
precipitationSum: body.daily.precipitation_sum[i],
weatherCode: body.daily.weather_code[i],
}
}
return week
}
export default async () => {
const [daily, week] = await Promise.all([getDailyData(), getWeekData()])
return {
...daily,
week,
}
}