can't recall what I was doing

This commit is contained in:
_ 2025-09-06 23:29:05 +00:00
parent 097745aeed
commit 5d56e8ecc0
2 changed files with 54 additions and 23 deletions

View file

@ -1,16 +1,17 @@
use super::{CalendarUi, EventInstance, Parameters, SimpleDownload};
use crate::prelude::*;
use chrono::DateTime;
#[derive(Clone, Default, Deserialize)]
#[derive(Clone, Deserialize)]
struct SillyDownloadable {
/// URL to scrape to download the file from
download_url: Option<Url>,
download_url: Url,
/// Disk location to cache the file for debugging
file_path: Utf8PathBuf,
}
#[derive(Clone, Default, Deserialize)]
#[derive(Clone, Deserialize)]
pub(crate) struct Config {
#[serde(flatten)]
pub(crate) dl: SillyDownloadable,
@ -20,8 +21,15 @@ pub(crate) struct Config {
}
impl Config {
pub(crate) fn simple_download(&self) -> SimpleDownload {
todo!()
pub(crate) fn simple_download(&self, now: DateTime<chrono_tz::Tz>) -> SimpleDownload {
let date = now.format("%Y-%m-%d").to_string();
let mut url = self.dl.download_url.clone();
url.query_pairs_mut().append_pair("date", &date);
SimpleDownload {
download_url: Some(url),
file_path: self.dl.file_path.clone(),
}
}
}
@ -52,7 +60,7 @@ pub(crate) struct Calendar {
impl Calendar {
fn to_event_instance(&self, params: &Parameters, ev: &Event) -> Result<Option<EventInstance>> {
let dt = chrono::DateTime::from_timestamp_millis(ev.timestamp)
let dt = DateTime::from_timestamp_millis(ev.timestamp)
.context("cannot represent timestamp as a date")?
.with_timezone(&params.tz);
let dtstart = crate::DatePerhapsTime { dt, all_day: false };
@ -95,27 +103,47 @@ impl Calendar {
mod tests {
use super::*;
#[test]
fn end_to_end() {
let s = r#"
{"data":{"items":[{"timestamp":1748989800000,"durationInMinutes":90,"title":"Foo Bar","description":""},{"timestamp":1749999600000,"durationInMinutes":30,"title":"Snaf Oo","description":""}]},"success":true,"message":""}
"#;
let cfg = Config {
fn example_config() -> Config {
Config {
dl: SillyDownloadable {
download_url: None,
download_url: Url::parse("https://www.commoninja.com/api/apps/calendar/get-monthly-events?widgetId=00000000-0000-0000-000000000000").unwrap(),
file_path: ".".into(),
},
ui: CalendarUi {
html_url: None,
short_name: "asdf".into(),
},
};
}
}
#[test]
fn end_to_end() {
let s = r#"
{"data":{"items":[{"timestamp":1748989800000,"durationInMinutes":90,"title":"Foo Bar","description":""},{"timestamp":1749999600000,"durationInMinutes":30,"title":"Snaf Oo","description":""}]},"success":true,"message":""}
"#;
let cfg = example_config();
let cal = Calendar::read_from_str(cfg, s).unwrap();
let params =
Parameters::new(Utc::now().with_timezone(&chrono_tz::America::Chicago)).unwrap();
let params = Parameters::new(
DateTime::from_timestamp(1748989700, 0)
.unwrap()
.with_timezone(&chrono_tz::America::Chicago),
)
.unwrap();
let instances = cal.event_instances(&params).unwrap();
assert_eq!(instances.len(), 2);
}
#[test]
fn unsillify() {
let dt = DateTime::from_timestamp(1756190298, 0)
.unwrap()
.with_timezone(&chrono_tz::America::Chicago);
let cfg = example_config();
assert_eq!(
cfg.simple_download(dt).download_url.unwrap().to_string(),
"https://www.commoninja.com/api/apps/calendar/get-monthly-events?widgetId=00000000-0000-0000-000000000000&date=2025-08-26"
);
}
}