Add support for multiple locations

Signed-off-by: Vegard Berg <mail@myrkvi.com>
This commit is contained in:
hakon.lerring 2023-08-31 15:22:29 +02:00 committed by Vegard Berg
parent e64fc803b6
commit 2f32c57c55
2 changed files with 19 additions and 13 deletions

View File

@ -6,9 +6,10 @@ use serde::{Serialize, Deserialize};
use crate::html::HTMLStringToVec; use crate::html::HTMLStringToVec;
pub(crate) fn get_cached_data() -> anyhow::Result<CachedData> { pub(crate) fn get_cached_data(location: &str) -> anyhow::Result<CachedData> {
let mut cache = dirs::cache_dir().unwrap_or(PathBuf::from(".")); let mut cache = dirs::cache_dir().unwrap_or(PathBuf::from("."));
cache.push("n58-kantine"); cache.push("n58-kantine");
cache.push(location);
cache.push("data.json"); cache.push("data.json");
let s = std::fs::read_to_string(cache)?; let s = std::fs::read_to_string(cache)?;
@ -17,9 +18,10 @@ pub(crate) fn get_cached_data() -> anyhow::Result<CachedData> {
Ok(cached_data) Ok(cached_data)
} }
pub(crate) fn set_cached_data(data: &CachedData) -> anyhow::Result<()> { pub(crate) fn set_cached_data(location: &str, data: &CachedData) -> anyhow::Result<()> {
let mut cache = dirs::cache_dir().unwrap_or(PathBuf::from(".")); let mut cache = dirs::cache_dir().unwrap_or(PathBuf::from("."));
cache.push("n58-kantine"); cache.push("n58-kantine");
cache.push(location);
let dir = cache.clone(); let dir = cache.clone();
cache.push("data.json"); cache.push("data.json");
@ -35,25 +37,25 @@ pub(crate) fn set_cached_data(data: &CachedData) -> anyhow::Result<()> {
} }
const MENU_URL: &'static str = const MENU_URL: &'static str =
"http://kantinemeny.azurewebsites.net/ukesmeny?lokasjon=toro@albatross-as.no&dato="; "http://kantinemeny.azurewebsites.net/ukesmeny";
const SOUP_URL: &'static str = const SOUP_URL: &'static str =
"http://kantinemeny.azurewebsites.net/ukesmenysuppe?lokasjon=toro@albatross-as.no&dato="; "http://kantinemeny.azurewebsites.net/ukesmenysuppe";
pub(crate) fn get_cached_or_fetch_data(now: DateTime<Utc>) -> anyhow::Result<CachedData> { pub(crate) fn get_cached_or_fetch_data(location: &str, now: DateTime<Utc>) -> anyhow::Result<CachedData> {
let cache = match get_cached_data() { let cache = match get_cached_data(location) {
Ok(data) => { Ok(data) => {
if (now - data.timestamp).num_minutes() > 60 { if (now - data.timestamp).num_minutes() > 60 {
let data = CachedData { let data = CachedData {
timestamp: now.clone(), timestamp: now.clone(),
items: ureq::get(MENU_URL).call()? items: ureq::get(&format!("{}?lokasjon={}&dato=", MENU_URL, location)).call()?
.into_string()? .into_string()?
.html_string_to_vec()?, .html_string_to_vec()?,
soup_items: ureq::get(SOUP_URL).call()? soup_items: ureq::get(&format!("{}?lokasjon={}&dato=",MENU_URL, location)).call()?
.into_string()? .into_string()?
.html_string_to_vec()?, .html_string_to_vec()?,
}; };
set_cached_data(&data)?; set_cached_data(location, &data)?;
data data
} else { } else {
data data
@ -62,14 +64,14 @@ pub(crate) fn get_cached_or_fetch_data(now: DateTime<Utc>) -> anyhow::Result<Cac
Err(_) => { Err(_) => {
let data = CachedData { let data = CachedData {
timestamp: now.clone(), timestamp: now.clone(),
items: ureq::get(MENU_URL).call()? items: ureq::get(&format!("{}?lokasjon={}&dato=", MENU_URL, location)).call()?
.into_string()? .into_string()?
.html_string_to_vec()?, .html_string_to_vec()?,
soup_items: ureq::get(SOUP_URL).call()? soup_items: ureq::get(&format!("{}?lokasjon={}&dato=",MENU_URL, location)).call()?
.into_string()? .into_string()?
.html_string_to_vec()?, .html_string_to_vec()?,
}; };
set_cached_data(&data)?; set_cached_data(location,&data)?;
data data
} }
}; };

View File

@ -35,6 +35,10 @@ struct Cli {
/// Output today only. Incompatible with --color /// Output today only. Incompatible with --color
#[arg(short, long)] #[arg(short, long)]
pub today: bool, pub today: bool,
/// Location of the cantina
#[arg(short, long, default_value = "toro@albatross-as.no")]
pub location: String,
} }
#[derive(strum::Display, Debug, ValueEnum, Clone, Copy)] #[derive(strum::Display, Debug, ValueEnum, Clone, Copy)]
@ -54,7 +58,7 @@ fn main() -> anyhow::Result<()> {
.weekday() .weekday()
.num_days_from_monday(); .num_days_from_monday();
let Ok(data) = get_cached_or_fetch_data(now) else { let Ok(data) = get_cached_or_fetch_data(&args.location, now) else {
panic!("Failed to get data"); panic!("Failed to get data");
}; };