Adding cache for IpLocation
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
2023-06-17 13:36:20 +02:00
parent a611f648fd
commit 0c3a3126f8
6 changed files with 246 additions and 25 deletions
+20 -11
View File
@@ -4,25 +4,26 @@ mod current_weather;
mod current_weather_extractor;
mod current_weather_output;
mod current_weather_output_model;
mod current_weather_print_params;
mod data_format;
mod ifconfig;
mod ip_api;
mod ip_location;
mod ip_location_cache;
mod open_meteo;
mod speed_unit;
mod temp_unit;
use billboard::{Alignment, Billboard};
use current_weather_print_params::CurrentWeatherPrintParams;
use data_format::DataFormat;
use 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::extract_public_ip;
use ip_api::extract_coords_and_city;
use open_meteo::request_current_weather;
@@ -82,16 +83,24 @@ fn main() {
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 ip_location;
let cache_ip_location = get_from_cache(&ip);
let latitude = coords["lat"].as_f64().unwrap();
let longitude = coords["lon"].as_f64().unwrap();
if cache_ip_location.is_some() {
ip_location = cache_ip_location.unwrap();
} 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(coords["city"].as_str().unwrap().to_string());
city = Some(ip_location.city);
}
let result = request_current_weather(&coordinates, &temperature_unit, &speed_unit);