Compare commits
No commits in common. "c296dd7731bb256f336835fdd8a10c8c98af00e4" and "e64fc803b6eb88d6d19591eaa7392c66ecd79cd1" have entirely different histories.
c296dd7731
...
e64fc803b6
|
@ -23,5 +23,5 @@ owo-colors = { version = "3.5.0", features = ["supports-colors"] }
|
||||||
serde = { version = "1.0.152", features = ["derive"] }
|
serde = { version = "1.0.152", features = ["derive"] }
|
||||||
serde_json = "1.0.91"
|
serde_json = "1.0.91"
|
||||||
strum = { version = "0.24.1", features = ["derive"] }
|
strum = { version = "0.24.1", features = ["derive"] }
|
||||||
tl = { version = "0.7.7" }
|
tl = { version = "0.7.7", features = ["simd"] }
|
||||||
ureq = "2.6.2"
|
ureq = "2.6.2"
|
||||||
|
|
26
src/data.rs
26
src/data.rs
|
@ -6,10 +6,9 @@ use serde::{Serialize, Deserialize};
|
||||||
use crate::html::HTMLStringToVec;
|
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("."));
|
let mut cache = dirs::cache_dir().unwrap_or(PathBuf::from("."));
|
||||||
cache.push("n58-kantine");
|
cache.push("n58-kantine");
|
||||||
cache.push(location);
|
|
||||||
cache.push("data.json");
|
cache.push("data.json");
|
||||||
|
|
||||||
let s = std::fs::read_to_string(cache)?;
|
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)
|
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("."));
|
let mut cache = dirs::cache_dir().unwrap_or(PathBuf::from("."));
|
||||||
cache.push("n58-kantine");
|
cache.push("n58-kantine");
|
||||||
cache.push(location);
|
|
||||||
let dir = cache.clone();
|
let dir = cache.clone();
|
||||||
cache.push("data.json");
|
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 =
|
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 =
|
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> {
|
pub(crate) fn get_cached_or_fetch_data(now: DateTime<Utc>) -> anyhow::Result<CachedData> {
|
||||||
let cache = match get_cached_data(location) {
|
let cache = match get_cached_data() {
|
||||||
Ok(data) => {
|
Ok(data) => {
|
||||||
if (now - data.timestamp).num_minutes() > 60 {
|
if (now - data.timestamp).num_minutes() > 60 {
|
||||||
let data = CachedData {
|
let data = CachedData {
|
||||||
timestamp: now.clone(),
|
timestamp: now.clone(),
|
||||||
items: ureq::get(&format!("{}?lokasjon={}&dato=", MENU_URL, location)).call()?
|
items: ureq::get(MENU_URL).call()?
|
||||||
.into_string()?
|
.into_string()?
|
||||||
.html_string_to_vec()?,
|
.html_string_to_vec()?,
|
||||||
soup_items: ureq::get(&format!("{}?lokasjon={}&dato=",MENU_URL, location)).call()?
|
soup_items: ureq::get(SOUP_URL).call()?
|
||||||
.into_string()?
|
.into_string()?
|
||||||
.html_string_to_vec()?,
|
.html_string_to_vec()?,
|
||||||
};
|
};
|
||||||
|
|
||||||
set_cached_data(location, &data)?;
|
set_cached_data(&data)?;
|
||||||
data
|
data
|
||||||
} else {
|
} else {
|
||||||
data
|
data
|
||||||
|
@ -64,14 +62,14 @@ pub(crate) fn get_cached_or_fetch_data(location: &str, now: DateTime<Utc>) -> an
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
let data = CachedData {
|
let data = CachedData {
|
||||||
timestamp: now.clone(),
|
timestamp: now.clone(),
|
||||||
items: ureq::get(&format!("{}?lokasjon={}&dato=", MENU_URL, location)).call()?
|
items: ureq::get(MENU_URL).call()?
|
||||||
.into_string()?
|
.into_string()?
|
||||||
.html_string_to_vec()?,
|
.html_string_to_vec()?,
|
||||||
soup_items: ureq::get(&format!("{}?lokasjon={}&dato=",MENU_URL, location)).call()?
|
soup_items: ureq::get(SOUP_URL).call()?
|
||||||
.into_string()?
|
.into_string()?
|
||||||
.html_string_to_vec()?,
|
.html_string_to_vec()?,
|
||||||
};
|
};
|
||||||
set_cached_data(location,&data)?;
|
set_cached_data(&data)?;
|
||||||
data
|
data
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -35,10 +35,6 @@ struct Cli {
|
||||||
/// Output today only. Incompatible with --color
|
/// Output today only. Incompatible with --color
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
pub today: bool,
|
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)]
|
#[derive(strum::Display, Debug, ValueEnum, Clone, Copy)]
|
||||||
|
@ -58,7 +54,7 @@ fn main() -> anyhow::Result<()> {
|
||||||
.weekday()
|
.weekday()
|
||||||
.num_days_from_monday();
|
.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");
|
panic!("Failed to get data");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue