From 0aec1d5a75b8e4dbc4548f0d5c011f8e1e881597 Mon Sep 17 00:00:00 2001 From: Fran314 Date: Mon, 28 Oct 2024 13:06:51 +0100 Subject: [PATCH] added precipitation probability for OpenMeteo --- README.md | 4 ++++ index.js | 11 ++++++++--- scrapers/OpenMeteo.js | 40 +++++++++++++++++++++------------------- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 8925682..1769e09 100644 --- a/README.md +++ b/README.md @@ -173,6 +173,10 @@ Format: "type": "number", "unit": "mm" }, + "precipitationProbability": { + "type": "number", + "unit": "%" + }, "apparentTemperature": { "type": "number", "unit": "°C" diff --git a/index.js b/index.js index a2805c6..aa2ef49 100644 --- a/index.js +++ b/index.js @@ -13,7 +13,12 @@ const opts = process.env.ON_NIX ? NIX_OPS : {} const run = async () => { const browser = await puppeteer.launch(opts) - const [iLMeteo, treBmeteo, openMeteo] = await Promise.all([ + const [ + // Comment out unwanted fields + iLMeteo, + treBmeteo, + openMeteo, + ] = await Promise.all([ fetchILMeteo(browser), fetch3Bmeteo(browser), fetchOpenMeteo(), @@ -22,9 +27,9 @@ const run = async () => { await browser.close() return { - // iLMeteo, + iLMeteo, treBmeteo, - // openMeteo, + openMeteo, } } diff --git a/scrapers/OpenMeteo.js b/scrapers/OpenMeteo.js index cf590a3..72204fd 100644 --- a/scrapers/OpenMeteo.js +++ b/scrapers/OpenMeteo.js @@ -8,6 +8,10 @@ const FORMAT = { type: 'number', unit: 'mm', }, + precipitationProbability: { + type: 'number', + unit: '%', + }, apparentTemperature: { type: 'number', unit: '°C', @@ -47,39 +51,34 @@ const FORMAT = { 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', + 'https://api.open-meteo.com/v1/forecast?latitude=43.7085&longitude=10.4036&hourly=temperature_2m,relative_humidity_2m,apparent_temperature,precipitation,precipitation_probability,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] = { + const dataAt = (body, i) => { + return { temperature: body.hourly.temperature_2m[i], precipitation: body.hourly.precipitation[i], + precipitationProbability: body.hourly.precipitation_probability[i], apparentTemperature: body.hourly.apparent_temperature[i], weatherCode: body.hourly.weather_code[i], } } + let today = {} + const startTime = new Date().getHours() + 1 + for (let i = startTime; i <= 23; i++) { + today[i] = dataAt(body, 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], - } + tomorrow[i] = dataAt(body, 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], - } + dayAfterTomorrow[i] = dataAt(body, 48 + i) } return { today, @@ -90,18 +89,20 @@ const getDailyData = async () => { 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', + '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,precipitation_probability_max&timezone=Europe%2FBerlin', ) const body = await response.json() let week = {} - for (let i = 0; i < 6; i++) { + 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], + precipitationProbabilityMax: + body.daily.precipitation_probability_max[i], weatherCode: body.daily.weather_code[i], } } @@ -112,6 +113,7 @@ export default async () => { const [daily, week] = await Promise.all([getDailyData(), getWeekData()]) return { + format: FORMAT, ...daily, week, }