Skip to content

Commit

Permalink
🐛 historical weather requests for recent days (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm authored Oct 20, 2023
1 parent 1773a21 commit c8c5f1d
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/modules/weather.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -108,12 +108,18 @@ latitude={lat}
}

pub async fn get_date(date: NaiveDate, lat: f64, lon: f64, units: &Units) -> Result<OptionalWeather> {
// 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={}
Expand Down

0 comments on commit c8c5f1d

Please sign in to comment.