summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNamilskyy <alive6863@gmail.com>2025-11-15 15:19:36 +0300
committerNamilskyy <alive6863@gmail.com>2025-11-15 15:21:51 +0300
commite926ea77d891c809be1e601c3fbf069617e42d6b (patch)
tree2c95a244de76b8a371ace535d3d1a85e828900a9 /src
parent26092b2043649a466d07fbb87078adc8c8612621 (diff)
Migtated to free open-meteo API
Diffstat (limited to 'src')
-rw-r--r--src/main.rs75
-rw-r--r--src/parser.rs55
-rw-r--r--src/shared.rs128
3 files changed, 72 insertions, 186 deletions
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<Config, Box<dyn std::error::Error>> {
- get_config();
+ let cfg: Config = get_config();
- Ok(Config::load()?)
+ Ok(cfg);
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
- let config = Config::load()?;
- let matches = Command::new("WeatherFetch")
- .version("0.1")
- .author("Borisov Alexey <arcanetmodl@gmail.com>")
- .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::<String>("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<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
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<f64>,
+ pub utc_offset_seconds: Option<i32>,
+ pub timezone: Option<String>,
+ pub timezone_abbreviation: Option<String>,
+ pub elevation: Option<f64>,
+ pub current_units: Option<CurrentUnits>,
pub current: Current,
- pub minutely: Option<Vec<Minutely>>,
- pub hourly: Option<Vec<Hourly>>,
- pub daily: Option<Vec<Daily>>,
- pub alerts: Option<Vec<Alert>>,
+ pub hourly_units: Option<HourlyUnits>,
+ 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<Weather>,
-}
-
-#[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<String>,
+ pub temperature_2m: Option<String>,
+ pub wind_speed_10m: Option<String>,
}
-
#[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<Weather>,
- pub pop: f32,
+ pub time: Vec<String>,
+ pub temperature_2m: Vec<f32>,
+ pub relative_humidity_2m: Vec<u32>,
+ pub wind_speed_10m: Vec<f32>,
}
-
#[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<Weather>,
- pub clouds: u32,
- pub pop: f32,
- pub rain: Option<f32>,
- pub uvi: f32,
+pub struct HourlyUnits {
+ pub time: Option<String>,
+ pub temperature_2m: Option<String>,
+ pub relative_humidity_2m: Option<String>,
+ pub wind_speed_10m: Option<String>,
}
-
-#[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<String>,
-} \ No newline at end of file