First commit, CLI to request the current weather from Open Meteo

This commit is contained in:
2023-05-19 00:03:14 +02:00
commit f7a7b85111
5 changed files with 698 additions and 0 deletions

29
src/cli.rs Normal file
View File

@ -0,0 +1,29 @@
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
#[structopt(name = "Open Meteo CLI", about = "CLI to extract data from Open Meteo")]
pub enum Options {
#[structopt(about = "Get current weather from a coordinate")]
CurrentWeather {
#[structopt(short = "l", long, help = "Latitude as a decimal number")]
latitude: f32,
#[structopt(short = "g", long, help = "Altitude as a decimal number")]
longitude: f32,
#[structopt(short = "d", long, help = "Extracts if its day or night")]
is_day: bool,
#[structopt(short = "t", long, help = "Extracts decimal temperature")]
temperature: bool,
#[structopt(short = "s", long, help = "Extracts decimal wind speed")]
windspeed: bool,
#[structopt(short = "w", long, help = "Extracts angle direction")]
winddirection: bool,
#[structopt(
short = "c",
long,
help = "Clean the output, and only show the values separates by commas.
The order is: is_day, temperature, windspeed, winddirection"
)]
clean: bool,
},
}

92
src/main.rs Normal file
View File

@ -0,0 +1,92 @@
mod cli;
use cli::Options;
use structopt::StructOpt;
fn main() {
let opts = Options::from_args();
match opts {
Options::CurrentWeather {
latitude,
longitude,
is_day,
temperature,
windspeed,
winddirection,
clean,
} => {
let result = extract_weather(latitude, longitude);
let weather = result.expect("Error requesting weather");
let mut final_string_vec: Vec<String> = Vec::new();
if !clean {
final_string_vec.push(format!(
"=== Current weather for Latitude: {latitude}, Longitude: {longitude} ==="
));
}
// TODO: Check if any flag has been selected.
let current_weather = &weather["current_weather"];
if is_day {
let is_day = &current_weather["is_day"] == 1;
if clean {
final_string_vec.push(format!("{is_day}"));
} else {
final_string_vec.push(format!("Is day: {is_day}"));
}
}
if temperature {
let temperature = &current_weather["temperature"];
if clean {
final_string_vec.push(format!("{temperature}"));
} else {
final_string_vec.push(format!("Temperature: {temperature}"));
}
}
if windspeed {
let windspeed = &current_weather["windspeed"];
if clean {
final_string_vec.push(format!("{windspeed}"));
} else {
final_string_vec.push(format!("Wind speed: {windspeed}"));
}
}
if winddirection {
let winddirection = &current_weather["winddirection"];
if clean {
final_string_vec.push(format!("{winddirection}"));
} else {
final_string_vec.push(format!("Wind direction: {winddirection}"));
}
}
if clean {
let final_string = final_string_vec.join(",");
println!("{final_string}");
} else {
let final_string = final_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}&current_weather=true");
let body: serde_json::Value = ureq::get(&url).call()?.into_json()?;
// TODO: We can also add this data.
// let elevation = &body["elevation"];
// println!("[D] Elevation: {elevation}");
// let timezone = &body["timezone"];
// println!("[D] Timezone: {timezone}");
// let current_weather = &body["current_weather"];
// let time = &current_weather["time"];
// println!("[D] Time: {time}");
Ok(body)
}