Compare commits
2 Commits
e64fc803b6
...
c296dd7731
Author | SHA1 | Date |
---|---|---|
hakon.lerring | c296dd7731 | |
hakon.lerring | 2f32c57c55 |
|
@ -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"
|
||||
|
|
26
src/data.rs
26
src/data.rs
|
@ -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
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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");
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue