First commit, CLI to request the current weather from Open Meteo
This commit is contained in:
+92
@@ -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 = ¤t_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 = ¤t_weather["temperature"];
|
||||
if clean {
|
||||
final_string_vec.push(format!("{temperature}"));
|
||||
} else {
|
||||
final_string_vec.push(format!("Temperature: {temperature}"));
|
||||
}
|
||||
}
|
||||
|
||||
if windspeed {
|
||||
let windspeed = ¤t_weather["windspeed"];
|
||||
if clean {
|
||||
final_string_vec.push(format!("{windspeed}"));
|
||||
} else {
|
||||
final_string_vec.push(format!("Wind speed: {windspeed}"));
|
||||
}
|
||||
}
|
||||
|
||||
if winddirection {
|
||||
let winddirection = ¤t_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}¤t_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 = ¤t_weather["time"];
|
||||
// println!("[D] Time: {time}");
|
||||
Ok(body)
|
||||
}
|
||||
Reference in New Issue
Block a user