77 lines
2.4 KiB
Rust
77 lines
2.4 KiB
Rust
mod cli;
|
|
mod current_weater;
|
|
|
|
use std::process::exit;
|
|
use structopt::StructOpt;
|
|
|
|
use cli::Arguments;
|
|
use current_weater::CurrentWeather;
|
|
|
|
fn main() {
|
|
let opts = Arguments::from_args();
|
|
match opts {
|
|
Arguments::CurrentWeather {
|
|
latitude,
|
|
longitude,
|
|
all,
|
|
is_day,
|
|
temperature,
|
|
windspeed,
|
|
winddirection,
|
|
clean,
|
|
} => {
|
|
if !all && !is_day && !temperature && !windspeed && !winddirection {
|
|
eprintln!("[ERROR] Provide at least one parameter to print, check --help");
|
|
exit(1);
|
|
}
|
|
|
|
let result = extract_weather(latitude, longitude);
|
|
let mut weather = result.unwrap_or_else(|_| {
|
|
eprintln!("[ERROR] Requesting current_weather for Latitude: {latitude}, Longitude: {longitude}");
|
|
exit(1);
|
|
});
|
|
|
|
let mut string_vec: Vec<String> = Vec::new();
|
|
|
|
if !clean {
|
|
string_vec.push(format!(
|
|
"=== Current weather for Latitude: {latitude}, Longitude: {longitude} ==="
|
|
));
|
|
}
|
|
|
|
let current_weather_data = weather["current_weather"].take();
|
|
let current_weather = CurrentWeather::new(current_weather_data, clean);
|
|
|
|
if is_day || all {
|
|
string_vec.push(current_weather.extract_data("is_day", "Is day"));
|
|
}
|
|
|
|
if temperature || all {
|
|
string_vec.push(current_weather.extract_data("temperature", "Temperature"));
|
|
}
|
|
|
|
if windspeed || all {
|
|
string_vec.push(current_weather.extract_data("windspeed", "Wind speed"));
|
|
}
|
|
|
|
if winddirection || all {
|
|
string_vec.push(current_weather.extract_data("winddirection", "Wind direction"));
|
|
}
|
|
|
|
if clean {
|
|
let final_string = string_vec.join(",");
|
|
println!("{final_string}");
|
|
} else {
|
|
let final_string = string_vec.join("\n");
|
|
println!("{final_string}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn extract_weather(latitude: f32, longitude: f32) -> Result<serde_json::Value, ureq::Error> {
|
|
let url = format!("https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}¤t_weather=true");
|
|
let body: serde_json::Value = ureq::get(&url).call()?.into_json()?;
|
|
Ok(body)
|
|
}
|