From ebbbf7533fa183eb6724cd20fad1ef27008cfbee Mon Sep 17 00:00:00 2001 From: Candifloss Date: Sat, 11 Oct 2025 15:33:11 +0530 Subject: [PATCH] Use cached weather data - Read `weatherd` cache instead of fetching from API --- widget/Cargo.toml | 2 +- widget/src/main.rs | 41 ++++++++++++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/widget/Cargo.toml b/widget/Cargo.toml index c7940c5..e3f2820 100644 --- a/widget/Cargo.toml +++ b/widget/Cargo.toml @@ -9,4 +9,4 @@ owm_widg_config = { path = "../owm_widg_config" } reqwest = {version = "0.12.23", features = ["blocking", "json"] } toml = "0.9.7" dirs = "6.0.0" - +serde_json = "1.0.145" diff --git a/widget/src/main.rs b/widget/src/main.rs index 0b0013a..cbe8335 100644 --- a/widget/src/main.rs +++ b/widget/src/main.rs @@ -1,4 +1,4 @@ -//use owm_api25::current::WeatherResponse; +use owm_rs::free_api_v25::current::WeatherResponse; use owm_widg_config::config::Config; use std::fs; @@ -15,19 +15,38 @@ fn main() -> Result<(), Box> { match cfg.general.api_version.as_str() { "free_2.5" => { - /* - let query = owm_api25::query::QueryParams::from(cfg.query_params); + // Read json data from cache file + let cache_path = dirs::cache_dir() + .ok_or("No cache dir found")? + .join("candydesktop/owm_widget.json"); - let url = query.weather_url()?; - let resp = blocking::get(&url)?.json::()?; + let json_data = fs::read_to_string(&cache_path)?; + let resp: WeatherResponse = serde_json::from_str(&json_data)?; - println!("City: {}, {}", resp.name, resp.sys.country); - if let Some(w) = resp.weather.first() { - println!("Weather: {}", w.main); - println!("Icon: {}", w.icon); + // Print city and country + if let Some(city) = &resp.name { + print!("City: {city}"); + if let Some(sys) = &resp.sys + && let Some(country) = &sys.country + { + print!(", {country}"); + } + + println!(); + } + + // Print weather description + if let Some(weather) = resp.weather.first() { + println!("Weather: {}", weather.main); + println!("Icon: {}", weather.icon); + } + + // Print temperature + if let Some(main_data) = &resp.main + && let Some(temp) = main_data.temp + { + println!("Temperature: {temp:.1}°C"); } - println!("Temperature: {}°C", resp.main.temp); - */ } other => { return Err(format!("Unsupported api_version: {other}").into());