Skip to content

Commit

Permalink
feat: reformat forecast datetime (#3)
Browse files Browse the repository at this point in the history
* feat: parse and reformat forecast times
  • Loading branch information
barakplasma authored Feb 1, 2023
1 parent 30c454b commit 11d98e8
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 6 deletions.
190 changes: 190 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
chrono = "0.4.23"
serde = { version = "1.0.151", features = ["derive"]}
serde-xml-rs = "0.6.0"
ureq = "2.5.0"
47 changes: 42 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use chrono::{DateTime, Utc, NaiveDateTime, LocalResult};
use serde::{Deserialize, Serialize};
use serde_xml_rs::{from_str};
use std::time::Duration;
Expand Down Expand Up @@ -55,16 +56,52 @@ pub struct Forecast {

static WEATHER_URL: &str = "https://ims.gov.il/sites/default/files/ims_data/xml_files/isr_cities_1week_6hr_forecast.xml";

static duration: Duration = Duration::new(180, 0);
pub fn get_israeli_weather_forecast() -> Result<LocationForecasts, serde_xml_rs::Error> {
let forecast_xml = ureq::get(WEATHER_URL)
.timeout(duration)
static DURATION: Duration = Duration::new(180, 0);

pub fn get_israeli_weather_forecast() -> Result<LocationForecasts, i8> {
let agent = ureq::AgentBuilder::new()
.timeout_connect(DURATION)
.timeout_read(DURATION)
.build();

let forecast_xml = agent.get(WEATHER_URL)
.call()
.expect("failed to fetch forecast")
.into_string()
.expect("invalid xml");

let forecasts: Result<LocationForecasts, serde_xml_rs::Error> = from_str(&forecast_xml);

return forecasts;
if let Ok(mut forecasts) = forecasts {
transform_forecast_times_to_datetimes(&mut forecasts);
return Ok(forecasts);
} else {
return Err(0);
}
}

fn parse_time(time: &str) -> Result<DateTime<Utc>, LocalResult<i8>> {
let possible_time = NaiveDateTime::parse_from_str(time, "%Y-%m-%d %H:%M:%S").expect("failed to parse forecast time").and_local_timezone(Utc).latest();
if let Some(time) = possible_time {
return Ok(time);
} else {
return Err(LocalResult::None);
}
}

fn transform_forecast_times_to_datetimes(forecast: &mut LocationForecasts) {
forecast.location.iter_mut().for_each(|location| {
location.location_data.forecast.iter_mut().for_each(|forecast| {
forecast.forecast_time = parse_time(&forecast.forecast_time).expect("failed to parse forecast time").to_rfc3339();
});
});
}

#[cfg(test)]
mod tests {
#[test]
fn parse_time() {
let time = "2023-01-31 02:00:00";
assert_eq!(super::parse_time(time).expect("failed to parse").to_rfc3339(), "2023-01-31T02:00:00+00:00");
}
}
11 changes: 10 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,14 @@ fn main() {
fn print_location(location: &weather::Location) {
println!("");
println!("Location: {}", location.location_meta_data.location_name_eng);
println!("Average Temperature: {}", location.location_data.forecast.iter().map(|f| f.temperature).sum::<i16>() / location.location_data.forecast.len() as i16);
println!("Most Recent Forecast:");
let most_recent_forecast = location.location_data.forecast.last().expect("failed to get most recent forecast");
if Some(most_recent_forecast) != None {
println!("Time: {}", most_recent_forecast.forecast_time);
println!("Temperature: {}", most_recent_forecast.temperature);
println!("Wind Direction: {}", most_recent_forecast.wind_direction);
println!("Wind Speed: {}", most_recent_forecast.wind_speed);
println!("Rain: {}", most_recent_forecast.rain);
println!("");
}
}

0 comments on commit 11d98e8

Please sign in to comment.