Adding automatic IP address, some refactor and better texts. Upgrading version.
This commit is contained in:
+99
-41
@@ -1,11 +1,23 @@
|
||||
mod cli;
|
||||
mod current_weater;
|
||||
mod coords;
|
||||
|
||||
mod current_weather;
|
||||
mod current_weather_printer;
|
||||
mod ifconfig;
|
||||
mod ip_api;
|
||||
mod open_meteo;
|
||||
|
||||
use billboard::{Alignment, Billboard};
|
||||
use std::process::exit;
|
||||
use structopt::StructOpt;
|
||||
|
||||
use cli::Arguments;
|
||||
use current_weater::CurrentWeather;
|
||||
use coords::Coordinates;
|
||||
|
||||
use current_weather_printer::CurrentWeatherPrinter;
|
||||
use ifconfig::extract_public_ip;
|
||||
use ip_api::extract_coords_and_city;
|
||||
use open_meteo::request_current_weather;
|
||||
|
||||
fn main() {
|
||||
let opts = Arguments::from_args();
|
||||
@@ -18,60 +30,108 @@ fn main() {
|
||||
temperature,
|
||||
windspeed,
|
||||
winddirection,
|
||||
include_coords,
|
||||
include_city,
|
||||
clean,
|
||||
} => {
|
||||
if !all && !is_day && !temperature && !windspeed && !winddirection {
|
||||
if !all
|
||||
&& !is_day
|
||||
&& !temperature
|
||||
&& !windspeed
|
||||
&& !winddirection
|
||||
&& !include_coords
|
||||
&& !include_city
|
||||
{
|
||||
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}");
|
||||
let coordinates: Coordinates;
|
||||
let city: Option<String>;
|
||||
|
||||
if latitude.is_some() && longitude.is_some() {
|
||||
let latitude = latitude.unwrap();
|
||||
let longitude = longitude.unwrap();
|
||||
coordinates = Coordinates::new(latitude, longitude);
|
||||
|
||||
if !coordinates.is_correct_latitude() {
|
||||
eprintln!("[ERROR] {} is not a valid latitude", coordinates.latitude);
|
||||
exit(1);
|
||||
} else if !coordinates.is_correct_longitude() {
|
||||
eprintln!("[ERROR] {} is not a valid longitude", coordinates.longitude);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
city = None;
|
||||
} else {
|
||||
let ip_result = extract_public_ip();
|
||||
let ip = ip_result.unwrap_or_else(|_| {
|
||||
eprintln!("[ERROR] Could not extract public IP from ifconfig");
|
||||
exit(1);
|
||||
});
|
||||
|
||||
let coords_result = extract_coords_and_city(&ip);
|
||||
let coords = coords_result.unwrap_or_else(|_| {
|
||||
eprintln!("[ERROR] Could not extract IP coordinates from ip_api");
|
||||
exit(1);
|
||||
});
|
||||
|
||||
let latitude = coords["lat"].as_f64().unwrap();
|
||||
let longitude = coords["lon"].as_f64().unwrap();
|
||||
coordinates = Coordinates::new(latitude, longitude);
|
||||
city = Some(coords["city"].as_str().unwrap().to_string());
|
||||
}
|
||||
|
||||
let result = request_current_weather(&coordinates);
|
||||
let current_weather = result.unwrap_or_else(|_| {
|
||||
eprintln!(
|
||||
"[ERROR] Requesting CurrentWeather for Lat: {}, Lon: {}",
|
||||
coordinates.latitude, coordinates.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 mut title_header: Vec<String> = Vec::new();
|
||||
title_header.push(String::from("=== Current weather"));
|
||||
|
||||
let current_weather_data = weather["current_weather"].take();
|
||||
let current_weather = CurrentWeather::new(current_weather_data, clean);
|
||||
if (all || include_city) && city.is_some() {
|
||||
title_header.push(String::from("for"));
|
||||
title_header.push(city.unwrap());
|
||||
}
|
||||
|
||||
if is_day || all {
|
||||
let is_day = current_weather.extract_raw("is_day");
|
||||
if is_day == "1" {
|
||||
string_vec.push(current_weather.parse_custom_data(is_day, "Day"));
|
||||
} else {
|
||||
string_vec.push(current_weather.parse_custom_data(is_day, "Night"));
|
||||
title_header.push(String::from("==="));
|
||||
string_vec.push(title_header.join(" "));
|
||||
} else {
|
||||
if all || include_coords {
|
||||
string_vec.push(coordinates.latitude.to_string());
|
||||
string_vec.push(coordinates.longitude.to_string());
|
||||
}
|
||||
if (all || include_city) && city.is_some() {
|
||||
string_vec.push(city.unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
let current_weather_printer = CurrentWeatherPrinter::new(current_weather, clean);
|
||||
if is_day || all {
|
||||
string_vec.push(current_weather_printer.extract_day());
|
||||
}
|
||||
if temperature || all {
|
||||
string_vec.push(current_weather.extract_simple_data(
|
||||
"temperature",
|
||||
"Temperature",
|
||||
Some("°C"),
|
||||
));
|
||||
string_vec.push(current_weather_printer.extract_temperature());
|
||||
}
|
||||
|
||||
if windspeed || all {
|
||||
string_vec.push(current_weather.extract_simple_data(
|
||||
"windspeed",
|
||||
"Wind speed",
|
||||
Some(" km/h"),
|
||||
));
|
||||
string_vec.push(current_weather_printer.extract_wind_speed());
|
||||
}
|
||||
if winddirection || all {
|
||||
string_vec.push(current_weather_printer.extract_wind_direction());
|
||||
}
|
||||
|
||||
if winddirection || all {
|
||||
string_vec.push(current_weather.extract_simple_data(
|
||||
"winddirection",
|
||||
"Wind direction",
|
||||
Some("°"),
|
||||
if !clean && (all || include_coords) {
|
||||
string_vec.push(format!(
|
||||
"Latitude: {}, Longitude: {}",
|
||||
coordinates.latitude, coordinates.longitude
|
||||
));
|
||||
}
|
||||
|
||||
@@ -81,14 +141,12 @@ fn main() {
|
||||
} else {
|
||||
string_vec.push(String::from("=== Weather data by Open-Meteo.com ==="));
|
||||
let final_string = string_vec.join("\n");
|
||||
println!("{final_string}");
|
||||
Billboard::builder()
|
||||
.text_alignment(Alignment::Left)
|
||||
.box_alignment(Alignment::Left)
|
||||
.build()
|
||||
.eprint(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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user