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;
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("."));
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<CachedData> {
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<Utc>) -> anyhow::Result<CachedData> {
let cache = match get_cached_data() {
pub(crate) fn get_cached_or_fetch_data(location: &str, now: DateTime<Utc>) -> anyhow::Result<CachedData> {
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<Utc>) -> anyhow::Result<Cac
Err(_) => {
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
}
};

View File

@ -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");
};