161 lines
5.0 KiB
Rust
161 lines
5.0 KiB
Rust
mod cli;
|
|
mod coords;
|
|
mod current_weather_extractor;
|
|
mod current_weather_output;
|
|
mod current_weather_output_model;
|
|
mod current_weather_print_params;
|
|
|
|
mod ifconfig {
|
|
pub mod ifconfig_api;
|
|
}
|
|
|
|
mod formats {
|
|
pub mod data_format;
|
|
pub mod speed_unit;
|
|
pub mod temp_unit;
|
|
}
|
|
|
|
mod ip_api {
|
|
pub mod ip_api_api;
|
|
pub mod ip_location;
|
|
pub mod ip_location_cache;
|
|
}
|
|
|
|
mod open_meteo {
|
|
pub mod current_weather;
|
|
pub mod open_meteo_api;
|
|
}
|
|
|
|
use billboard::{Alignment, Billboard};
|
|
use formats::data_format::DataFormat;
|
|
use ip_api::ip_location_cache::{get_from_cache, save_to_cache};
|
|
use std::process::exit;
|
|
use structopt::StructOpt;
|
|
|
|
use cli::Arguments;
|
|
use coords::Coordinates;
|
|
use current_weather_extractor::CurrentWeatherExtractor;
|
|
use current_weather_print_params::CurrentWeatherPrintParams;
|
|
use ifconfig::ifconfig_api::extract_public_ip;
|
|
use ip_api::ip_api_api::extract_coords_and_city;
|
|
use open_meteo::open_meteo_api::request_current_weather;
|
|
|
|
fn main() {
|
|
let opts = Arguments::from_args();
|
|
match opts {
|
|
Arguments::CurrentWeather {
|
|
latitude,
|
|
longitude,
|
|
all,
|
|
is_day,
|
|
temperature,
|
|
temperature_unit,
|
|
windspeed,
|
|
speed_unit,
|
|
winddirection,
|
|
include_coords,
|
|
include_city,
|
|
mut format,
|
|
clean,
|
|
json,
|
|
} => {
|
|
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 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 ip_location;
|
|
let cache_ip_location = get_from_cache(&ip);
|
|
|
|
if let Some(cache_ip_location) = cache_ip_location {
|
|
ip_location = cache_ip_location;
|
|
} else {
|
|
let ip_location_res = extract_coords_and_city(&ip);
|
|
ip_location = ip_location_res.unwrap_or_else(|_| {
|
|
eprintln!("[ERROR] Could not extract IP location from ip_api");
|
|
exit(1);
|
|
});
|
|
save_to_cache(&ip, &ip_location);
|
|
}
|
|
|
|
let latitude = ip_location.lat;
|
|
let longitude = ip_location.lon;
|
|
coordinates = Coordinates::new(latitude, longitude);
|
|
city = Some(ip_location.city);
|
|
}
|
|
|
|
let result = request_current_weather(&coordinates, &temperature_unit, &speed_unit);
|
|
let current_weather = result.unwrap_or_else(|_| {
|
|
eprintln!(
|
|
"[ERROR] Requesting CurrentWeather for Lat: {}, Lon: {}",
|
|
coordinates.latitude, coordinates.longitude
|
|
);
|
|
exit(1);
|
|
});
|
|
|
|
if clean {
|
|
format = DataFormat::Clean;
|
|
} else if json {
|
|
format = DataFormat::JSON;
|
|
}
|
|
|
|
let print_params = CurrentWeatherPrintParams {
|
|
all,
|
|
is_day,
|
|
temperature,
|
|
temperature_unit,
|
|
windspeed,
|
|
speed_unit,
|
|
winddirection,
|
|
include_coords,
|
|
include_city,
|
|
format,
|
|
};
|
|
|
|
let current_weather_printer =
|
|
CurrentWeatherExtractor::new(current_weather, print_params, coordinates, city);
|
|
let output = current_weather_printer.extract_output();
|
|
if DataFormat::Normal == current_weather_printer.params.format {
|
|
Billboard::builder()
|
|
.text_alignment(Alignment::Left)
|
|
.box_alignment(Alignment::Left)
|
|
.build()
|
|
.print(output.to_string());
|
|
} else {
|
|
println!("{}", output);
|
|
}
|
|
}
|
|
}
|
|
}
|