From e926ea77d891c809be1e601c3fbf069617e42d6b Mon Sep 17 00:00:00 2001 From: Namilskyy Date: Sat, 15 Nov 2025 15:19:36 +0300 Subject: Migtated to free open-meteo API --- src/main.rs | 75 ++++++++++------------------------ src/parser.rs | 55 ++++++++++--------------- src/shared.rs | 128 +++++++++++++--------------------------------------------- 3 files changed, 72 insertions(+), 186 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index ffa33d3..7e150dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,68 +32,37 @@ SOFTWARE. extern crate image; -use clap::{Arg, Command}; +use std::os::linux::process; + +use clap::{Cli, Command, Subcommand}; use termimage::{Options}; mod parser; use parser::{get_config, Config}; // use crate::configmanager::{Config, handle_config}; - +#[derive(Subcommand)] +enum Commands { + Config, + Fetch, + Clean, + Help, + Today, + Tomorrow, + RebuildCache +} fn process_config() -> Result> { - get_config(); + let cfg: Config = get_config(); - Ok(Config::load()?) + Ok(cfg); } fn main() -> Result<(), Box> { - let config = Config::load()?; - let matches = Command::new("WeatherFetch") - .version("0.1") - .author("Borisov Alexey ") - .about("Weather fetch with image and ASCII art support") - .arg(Arg::new("image").short('i').long("image").value_name("PATH")) - .arg(Arg::new("exclude").short('e').long("exclude").value_name("TYPE") - .value_parser(["current", "minutely", "hourly", "daily", "alerts"])) - .arg(Arg::new("help").short('h').long("help")) - .arg(Arg::new("lat").short('t').long("lat").value_name("LATITUDE")) - .arg(Arg::new("lon").short('n').long("lon").value_name("LONGITUDE")) - .get_matches(); - - if matches.contains_id("help") { - println!("Usage:"); - println!("VALUES"); - println!("--img=, -i= image, takees a path to .jpg/.png"); - println!("--lat=, -t=, coordinates: takes f64."); - println!("--lon=, -n=, coordinates: takes f64"); - println!("--cfg=, -c=, takes a path to Config.toml"); - println!("--exclude=, -e=, takes exlude type. See the API docs"); - println!("FUNCTIONS"); - println!("--gen-conf, -g, generating standart config"); - println!("--alerts, -a, show alerts"); - - return Ok(()); - } - - if let Some(img_path) = matches.get_one::("image") { - let _img = image::open(img_path)?; - } - - - handle_config(&config)?; - - - let _opts = Options::parse(); // Prefix with underscore if not used - /* - if !opts { - opts = configmanager::Config::load(); - } - - let format = ops::guess_format(&opts.image)?; - let img = ops::load_image(&opts.image, format)?; - - */ - - Ok(()) -} + let config = process_config()?; + + + + } + + 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; +pub type BoxedError = Box; const DEFAULT_PATH: &str = "/home/$USER/.config/WeatherFetch/Config.toml"; +/* const CONF: Lazy>> = Lazy::new(|| { Config::load().map_err(|e| Box::new(e) as Box) }); - +*/ pub fn get_location(coords_args: bool) -> Result<(), BoxedError> { - let config = CONF - .as_ref() - .map_err(|e| Box::::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()`, `lon()`."); - 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> { - let config = CONF - .as_ref() - .map_err(|e| Box::::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> Ok(weather_data) } + pub struct Config { lat: f64, lon: f64, @@ -70,12 +59,12 @@ pub struct Config { lang: String, } -pub fn get_config() -> Result { - let mut file = read(DefaultConfig)?; - let mut content: String = file.read_to_string(&mut contents)?; +pub fn get_config() -> Result> { + 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 diff --git a/src/shared.rs b/src/shared.rs index 1afee09..75a4d1c 100644 --- a/src/shared.rs +++ b/src/shared.rs @@ -1,115 +1,43 @@ +use serde::{Serialize, Deserialize}; //API answer struct`s + #[derive(Debug, Serialize, Deserialize)] pub struct WeatherData { - pub lat: f64, - pub lon: f64, - pub timezone: String, - pub timezone_offset: i32, + pub latitude: f64, + pub longitude: f64, + pub generationtime_ms: Option, + pub utc_offset_seconds: Option, + pub timezone: Option, + pub timezone_abbreviation: Option, + pub elevation: Option, + pub current_units: Option, pub current: Current, - pub minutely: Option>, - pub hourly: Option>, - pub daily: Option>, - pub alerts: Option>, + pub hourly_units: Option, + pub hourly: Hourly, } #[derive(Debug, Serialize, Deserialize)] pub struct Current { - pub dt: u64, - pub sunrise: u64, - pub sunset: u64, - pub temp: f32, - pub feels_like: f32, - pub pressure: u32, - pub humidity: u32, - pub dew_point: f32, - pub uvi: f32, - pub clouds: u32, - pub visibility: u32, - pub wind_speed: f32, - pub wind_deg: u32, - pub wind_gust: f32, - pub weather: Vec, -} - -#[derive(Debug, Serialize, Deserialize)] -pub struct Weather { - pub id: u32, - pub main: String, - pub description: String, - pub icon: String, + pub time: String, + pub temperature_2m: f32, + pub wind_speed_10m: f32, } - #[derive(Debug, Serialize, Deserialize)] -pub struct Minutely { - pub dt: u64, - pub precipitation: f32, +pub struct CurrentUnits { + pub time: Option, + pub temperature_2m: Option, + pub wind_speed_10m: Option, } - #[derive(Debug, Serialize, Deserialize)] pub struct Hourly { - pub dt: u64, - pub temp: f32, - pub feels_like: f32, - pub pressure: u32, - pub humidity: u32, - pub dew_point: f32, - pub uvi: f32, - pub clouds: u32, - pub visibility: u32, - pub wind_speed: f32, - pub wind_deg: u32, - pub wind_gust: f32, - pub weather: Vec, - pub pop: f32, + pub time: Vec, + pub temperature_2m: Vec, + pub relative_humidity_2m: Vec, + pub wind_speed_10m: Vec, } - #[derive(Debug, Serialize, Deserialize)] -pub struct Daily { - pub dt: u64, - pub sunrise: u64, - pub sunset: u64, - pub moonrise: u64, - pub moonset: u64, - pub moon_phase: f32, - pub summary: String, - pub temp: Temp, - pub feels_like: FeelsLike, - pub pressure: u32, - pub humidity: u32, - pub dew_point: f32, - pub wind_speed: f32, - pub wind_deg: u32, - pub wind_gust: f32, - pub weather: Vec, - pub clouds: u32, - pub pop: f32, - pub rain: Option, - pub uvi: f32, +pub struct HourlyUnits { + pub time: Option, + pub temperature_2m: Option, + pub relative_humidity_2m: Option, + pub wind_speed_10m: Option, } - -#[derive(Debug, Serialize, Deserialize)] -pub struct Temp { - pub day: f32, - pub min: f32, - pub max: f32, - pub night: f32, - pub eve: f32, - pub morn: f32, -} - -#[derive(Debug, Serialize, Deserialize)] -pub struct FeelsLike { - pub day: f32, - pub night: f32, - pub eve: f32, - pub morn: f32, -} - -#[derive(Debug, Serialize, Deserialize)] -pub struct Alert { - pub sender_name: String, - pub event: String, - pub start: u64, - pub end: u64, - pub description: String, - pub tags: Vec, -} \ No newline at end of file -- cgit v1.2.3