Compare commits

..

No commits in common. "feature/add-location-option" and "main" have entirely different histories.

3 changed files with 14 additions and 20 deletions

View File

@ -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" }
tl = { version = "0.7.7", features = ["simd"] }
ureq = "2.6.2"

View File

@ -6,10 +6,9 @@ use serde::{Serialize, Deserialize};
use crate::html::HTMLStringToVec;
pub(crate) fn get_cached_data(location: &str) -> anyhow::Result<CachedData> {
pub(crate) fn get_cached_data() -> 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)?;
@ -18,10 +17,9 @@ pub(crate) fn get_cached_data(location: &str) -> anyhow::Result<CachedData> {
Ok(cached_data)
}
pub(crate) fn set_cached_data(location: &str, data: &CachedData) -> anyhow::Result<()> {
pub(crate) fn set_cached_data(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");
@ -37,25 +35,25 @@ pub(crate) fn set_cached_data(location: &str, data: &CachedData) -> anyhow::Resu
}
const MENU_URL: &'static str =
"http://kantinemeny.azurewebsites.net/ukesmeny";
"http://kantinemeny.azurewebsites.net/ukesmeny?lokasjon=toro@albatross-as.no&dato=";
const SOUP_URL: &'static str =
"http://kantinemeny.azurewebsites.net/ukesmenysuppe";
"http://kantinemeny.azurewebsites.net/ukesmenysuppe?lokasjon=toro@albatross-as.no&dato=";
pub(crate) fn get_cached_or_fetch_data(location: &str, now: DateTime<Utc>) -> anyhow::Result<CachedData> {
let cache = match get_cached_data(location) {
pub(crate) fn get_cached_or_fetch_data(now: DateTime<Utc>) -> anyhow::Result<CachedData> {
let cache = match get_cached_data() {
Ok(data) => {
if (now - data.timestamp).num_minutes() > 60 {
let data = CachedData {
timestamp: now.clone(),
items: ureq::get(&format!("{}?lokasjon={}&dato=", MENU_URL, location)).call()?
items: ureq::get(MENU_URL).call()?
.into_string()?
.html_string_to_vec()?,
soup_items: ureq::get(&format!("{}?lokasjon={}&dato=",SOUP_URL, location)).call()?
soup_items: ureq::get(SOUP_URL).call()?
.into_string()?
.html_string_to_vec()?,
};
set_cached_data(location, &data)?;
set_cached_data(&data)?;
data
} else {
data
@ -64,14 +62,14 @@ pub(crate) fn get_cached_or_fetch_data(location: &str, now: DateTime<Utc>) -> an
Err(_) => {
let data = CachedData {
timestamp: now.clone(),
items: ureq::get(&format!("{}?lokasjon={}&dato=", MENU_URL, location)).call()?
items: ureq::get(MENU_URL).call()?
.into_string()?
.html_string_to_vec()?,
soup_items: ureq::get(&format!("{}?lokasjon={}&dato=",SOUP_URL, location)).call()?
soup_items: ureq::get(SOUP_URL).call()?
.into_string()?
.html_string_to_vec()?,
};
set_cached_data(location,&data)?;
set_cached_data(&data)?;
data
}
};

View File

@ -35,10 +35,6 @@ 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)]
@ -58,7 +54,7 @@ fn main() -> anyhow::Result<()> {
.weekday()
.num_days_from_monday();
let Ok(data) = get_cached_or_fetch_data(&args.location, now) else {
let Ok(data) = get_cached_or_fetch_data(now) else {
panic!("Failed to get data");
};