diff options
| author | Namilskyy <alive6863@gmail.com> | 2025-11-15 15:19:36 +0300 |
|---|---|---|
| committer | Namilskyy <alive6863@gmail.com> | 2025-11-15 15:21:51 +0300 |
| commit | e926ea77d891c809be1e601c3fbf069617e42d6b (patch) | |
| tree | 2c95a244de76b8a371ace535d3d1a85e828900a9 /src/parser.rs | |
| parent | 26092b2043649a466d07fbb87078adc8c8612621 (diff) | |
Migtated to free open-meteo API
Diffstat (limited to 'src/parser.rs')
| -rw-r--r-- | src/parser.rs | 55 |
1 files changed, 22 insertions, 33 deletions
diff --git a/src/parser.rs b/src/parser.rs index 33c679d..14fe992 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,36 +1,30 @@ -//use clap::error::ErrorKind; -use reqwest::{Client}; -use chrono::{DateTime, Utc, prelude::*}; -use serde::{Serialize, Deserialize}; -use once_cell::sync::Lazy; -use std::{error::Error, fs::{read, File}, io::Read}; -use toml::Deserializer; +use reqwest::Client; +use std::fs::File; +use std::io::Read; +use toml; // use crate::configmanager::Config; -mod shared; use crate::shared::*; -//fftype BoxedError = Box<dyn std::error::Error + Send + Sync>; +pub type BoxedError = Box<dyn std::error::Error + Send + Sync>; const DEFAULT_PATH: &str = "/home/$USER/.config/WeatherFetch/Config.toml"; +/* const CONF: Lazy<Result<Config, Box<dyn std::error::Error + Send + Sync>>> = Lazy::new(|| { Config::load().map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send + Sync>) }); - +*/ pub fn get_location(coords_args: bool) -> Result<(), BoxedError> { - let config = CONF - .as_ref() - .map_err(|e| Box::<dyn std::error::Error + Send + Sync>::from(e.to_string()))? - .clone(); + let config = get_config()?; if (config.lat == 0.0 || config.lon == 0.0) && !coords_args { println!("No coordinates in configuration file or conf not founded."); println!("HINT: Try create ~/.config/WeatherFetch/Config.toml"); println!("HINT: And add `lat(<float>)`, `lon(<float>)`."); - println!("HINT: To get more info check https://openweathermap.org/api/one-call-3"); + println!("HINT: To get more info check https://open-meteo.com/en/docs"); Err("Invalid coordinates in config".into()) } else { @@ -39,20 +33,14 @@ pub fn get_location(coords_args: bool) -> Result<(), BoxedError> { } pub async fn parse_weather() -> Result<WeatherData, Box<dyn std::error::Error>> { - let config = CONF - .as_ref() - .map_err(|e| Box::<dyn std::error::Error>::from(e.to_string()))? - .clone(); - + let config = get_config()?; let client = Client::new(); - let response = client.get("https://api.openweathermap.org/data/3.0/onecall") + let response = client.get("https://api.open-meteo.com/v1/forecast") .query(&[ - ("lat", config.lat.to_string()), - ("lon", config.lon.to_string()), - ("exclude", config.exclude), - ("appid", config.appid), - ("units", config.units), - ("lang", config.lang), + ("latitude", config.lat.to_string()), + ("longitude", config.lon.to_string()), + ("current", "temperature_2m,wind_speed_10m"), + ("hourly", "temperature_2m,relative_humidity_2m,wind_speed_10m"), ]) .send() .await?; @@ -61,6 +49,7 @@ pub async fn parse_weather() -> Result<WeatherData, Box<dyn std::error::Error>> Ok(weather_data) } + pub struct Config { lat: f64, lon: f64, @@ -70,12 +59,12 @@ pub struct Config { lang: String, } -pub fn get_config() -> Result<Config, dyn std::error::Error> { - let mut file = read(DefaultConfig)?; - let mut content: String = file.read_to_string(&mut contents)?; +pub fn get_config() -> Result<Config, Box<dyn std::error::Error>> { + let mut file = File::open(DEFAULT_PATH)?; + let mut content = String::new(); + file.read_to_string(&mut content)?; - let mut deserialized_cfg = toml::Deserializer(&content)?; - let config: Config = Deserialize::deserialize(&mut deserialized_cfg)?; + let config: Config = toml::from_str(&content)?; Ok(config) - }
\ No newline at end of file +}
\ No newline at end of file |
