From c8c5f1d848811a9db54d8b3a9650c74fe3d4274b Mon Sep 17 00:00:00 2001 From: Turiiya <34311583+ttytm@users.noreply.github.com> Date: Fri, 20 Oct 2023 15:22:38 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20historical=20weather=20requests?= =?UTF-8?q?=20for=20recent=20days=20(#188)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/weather.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/modules/weather.rs b/src/modules/weather.rs index f488398..724c87b 100644 --- a/src/modules/weather.rs +++ b/src/modules/weather.rs @@ -1,5 +1,5 @@ use anyhow::{Context, Result}; -use chrono::NaiveDate; +use chrono::{Local, NaiveDate}; use optional_struct::{optional_struct, Applyable}; use serde::Deserialize; use std::collections::{HashMap, HashSet}; @@ -108,12 +108,18 @@ latitude={lat} } pub async fn get_date(date: NaiveDate, lat: f64, lon: f64, units: &Units) -> Result { + // It takes up to five days until temperature data is available in open-meteo's archive. + // Therefore, we use the past_days endpoints for the last five days. + let base_url = if date.signed_duration_since(Local::now().date_naive()).num_days() > -5 { + "https://api.open-meteo.com/v1/forecast?&past_days=10".to_string() + } else { + format!("https://archive-api.open-meteo.com/v1/archive?&start_date={date}&end_date={date}") + }; + let url = format!( - "https://archive-api.open-meteo.com/v1/archive? -latitude={lat} + "{base_url} +&latitude={lat} &longitude={lon} -&start_date={date} -&end_date={date} &temperature_unit={} &windspeed_unit={} &precipitation_unit={}