From 2f32c57c55177fd28c1f23fbf4a4578b383daa82 Mon Sep 17 00:00:00 2001 From: "hakon.lerring" Date: Thu, 31 Aug 2023 15:22:29 +0200 Subject: [PATCH] Add support for multiple locations Signed-off-by: Vegard Berg --- src/data.rs | 26 ++++++++++++++------------ src/main.rs | 6 +++++- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/data.rs b/src/data.rs index ce3c7ca..7564eac 100644 --- a/src/data.rs +++ b/src/data.rs @@ -6,9 +6,10 @@ use serde::{Serialize, Deserialize}; use crate::html::HTMLStringToVec; -pub(crate) fn get_cached_data() -> anyhow::Result { +pub(crate) fn get_cached_data(location: &str) -> anyhow::Result { let mut cache = dirs::cache_dir().unwrap_or(PathBuf::from(".")); cache.push("n58-kantine"); + cache.push(location); cache.push("data.json"); let s = std::fs::read_to_string(cache)?; @@ -17,9 +18,10 @@ pub(crate) fn get_cached_data() -> anyhow::Result { 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(".")); cache.push("n58-kantine"); + cache.push(location); let dir = cache.clone(); cache.push("data.json"); @@ -35,25 +37,25 @@ pub(crate) fn set_cached_data(data: &CachedData) -> anyhow::Result<()> { } 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 = - "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) -> anyhow::Result { - let cache = match get_cached_data() { +pub(crate) fn get_cached_or_fetch_data(location: &str, now: DateTime) -> anyhow::Result { + let cache = match get_cached_data(location) { Ok(data) => { if (now - data.timestamp).num_minutes() > 60 { let data = CachedData { timestamp: now.clone(), - items: ureq::get(MENU_URL).call()? + items: ureq::get(&format!("{}?lokasjon={}&dato=", MENU_URL, location)).call()? .into_string()? .html_string_to_vec()?, - soup_items: ureq::get(SOUP_URL).call()? + soup_items: ureq::get(&format!("{}?lokasjon={}&dato=",MENU_URL, location)).call()? .into_string()? .html_string_to_vec()?, }; - set_cached_data(&data)?; + set_cached_data(location, &data)?; data } else { data @@ -62,14 +64,14 @@ pub(crate) fn get_cached_or_fetch_data(now: DateTime) -> anyhow::Result { let data = CachedData { timestamp: now.clone(), - items: ureq::get(MENU_URL).call()? + items: ureq::get(&format!("{}?lokasjon={}&dato=", MENU_URL, location)).call()? .into_string()? .html_string_to_vec()?, - soup_items: ureq::get(SOUP_URL).call()? + soup_items: ureq::get(&format!("{}?lokasjon={}&dato=",MENU_URL, location)).call()? .into_string()? .html_string_to_vec()?, }; - set_cached_data(&data)?; + set_cached_data(location,&data)?; data } }; diff --git a/src/main.rs b/src/main.rs index a8e4b31..9b78ea6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,6 +35,10 @@ struct Cli { /// Output today only. Incompatible with --color #[arg(short, long)] 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)] @@ -54,7 +58,7 @@ fn main() -> anyhow::Result<()> { .weekday() .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"); };