From 2f32c57c55177fd28c1f23fbf4a4578b383daa82 Mon Sep 17 00:00:00 2001 From: "hakon.lerring" Date: Thu, 31 Aug 2023 15:22:29 +0200 Subject: [PATCH 1/3] 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"); }; -- 2.30.2 From c296dd7731bb256f336835fdd8a10c8c98af00e4 Mon Sep 17 00:00:00 2001 From: "hakon.lerring" Date: Thu, 31 Aug 2023 15:22:44 +0200 Subject: [PATCH 2/3] Remove simd since it's not supported in stable rust Signed-off-by: Vegard Berg --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index fef336b..19d194b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,5 +23,5 @@ owo-colors = { version = "3.5.0", features = ["supports-colors"] } serde = { version = "1.0.152", features = ["derive"] } serde_json = "1.0.91" strum = { version = "0.24.1", features = ["derive"] } -tl = { version = "0.7.7", features = ["simd"] } +tl = { version = "0.7.7" } ureq = "2.6.2" -- 2.30.2 From 0141e9de76980e577cdc9bec5ce88f90cce34568 Mon Sep 17 00:00:00 2001 From: "hakon.lerring" Date: Thu, 31 Aug 2023 15:34:45 +0200 Subject: [PATCH 3/3] Soup items were fetched from menu url Signed-off-by: Vegard Berg --- src/data.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/data.rs b/src/data.rs index 7564eac..564b00d 100644 --- a/src/data.rs +++ b/src/data.rs @@ -50,7 +50,7 @@ pub(crate) fn get_cached_or_fetch_data(location: &str, now: DateTime) -> an items: ureq::get(&format!("{}?lokasjon={}&dato=", MENU_URL, location)).call()? .into_string()? .html_string_to_vec()?, - soup_items: ureq::get(&format!("{}?lokasjon={}&dato=",MENU_URL, location)).call()? + soup_items: ureq::get(&format!("{}?lokasjon={}&dato=",SOUP_URL, location)).call()? .into_string()? .html_string_to_vec()?, }; @@ -67,7 +67,7 @@ pub(crate) fn get_cached_or_fetch_data(location: &str, now: DateTime) -> an items: ureq::get(&format!("{}?lokasjon={}&dato=", MENU_URL, location)).call()? .into_string()? .html_string_to_vec()?, - soup_items: ureq::get(&format!("{}?lokasjon={}&dato=",MENU_URL, location)).call()? + soup_items: ureq::get(&format!("{}?lokasjon={}&dato=",SOUP_URL, location)).call()? .into_string()? .html_string_to_vec()?, }; -- 2.30.2