Use cached weather data
- Read `weatherd` cache instead of fetching from API
This commit is contained in:
parent
a5a7e6a5db
commit
ebbbf7533f
@ -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"
|
||||
|
@ -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<dyn std::error::Error>> {
|
||||
|
||||
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::<WeatherResponse>()?;
|
||||
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());
|
||||
|
Loading…
x
Reference in New Issue
Block a user