Compare commits
No commits in common. "6daa02e8add82be92dbe5d1effda7197b7923340" and "097745aeed41971a2336ae51e619e4ab26116ff5" have entirely different histories.
6daa02e8ad
...
097745aeed
2 changed files with 29 additions and 60 deletions
25
src/main.rs
25
src/main.rs
|
@ -58,15 +58,11 @@ struct Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
fn downloads(&self, now: DateTime<chrono_tz::Tz>) -> impl Iterator<Item = SimpleDownload> {
|
fn downloads(&self) -> impl Iterator<Item = SimpleDownload> {
|
||||||
self.campfires
|
self.campfires
|
||||||
.iter()
|
.iter()
|
||||||
.map(|cf| cf.dl.clone())
|
.map(|cf| cf.dl.clone())
|
||||||
.chain(
|
.chain(self.common_ninjas.iter().map(|cn| cn.simple_download()))
|
||||||
self.common_ninjas
|
|
||||||
.iter()
|
|
||||||
.map(move |cn| cn.simple_download(now)),
|
|
||||||
)
|
|
||||||
.chain(self.icals.iter().map(|ical| ical.dl.clone()))
|
.chain(self.icals.iter().map(|ical| ical.dl.clone()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -438,14 +434,12 @@ static APP_USER_AGENT: &str = concat!(
|
||||||
async fn do_everything(cli: &CliAuto) -> Result<()> {
|
async fn do_everything(cli: &CliAuto) -> Result<()> {
|
||||||
let config = std::fs::read_to_string(&cli.config)?;
|
let config = std::fs::read_to_string(&cli.config)?;
|
||||||
let config: Config = toml::from_str(&config)?;
|
let config: Config = toml::from_str(&config)?;
|
||||||
let tz = &config.output.timezone;
|
|
||||||
let now = Utc::now().with_timezone(tz);
|
|
||||||
|
|
||||||
tracing::info!(?APP_USER_AGENT);
|
tracing::info!(?APP_USER_AGENT);
|
||||||
let client = reqwest::Client::builder()
|
let client = reqwest::Client::builder()
|
||||||
.user_agent(APP_USER_AGENT)
|
.user_agent(APP_USER_AGENT)
|
||||||
.build()?;
|
.build()?;
|
||||||
for dl in config.downloads(now) {
|
for dl in config.downloads() {
|
||||||
let Some(download_url) = &dl.download_url else {
|
let Some(download_url) = &dl.download_url else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
@ -462,6 +456,9 @@ async fn do_everything(cli: &CliAuto) -> Result<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
let data = read_data_from_disk(&config)?;
|
let data = read_data_from_disk(&config)?;
|
||||||
|
|
||||||
|
let tz = &config.output.timezone;
|
||||||
|
let now = Utc::now().with_timezone(tz);
|
||||||
let instances = process_data(&data, &config.output, now)?;
|
let instances = process_data(&data, &config.output, now)?;
|
||||||
output_html(&config.output, &instances, now)?;
|
output_html(&config.output, &instances, now)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -487,15 +484,15 @@ fn main_auto(cli: CliAuto) -> Result<()> {
|
||||||
fn main_debug_output(cli: CliDebugOutput) -> Result<()> {
|
fn main_debug_output(cli: CliDebugOutput) -> Result<()> {
|
||||||
tracing_subscriber::fmt::init();
|
tracing_subscriber::fmt::init();
|
||||||
tracing::info!("Started tracing");
|
tracing::info!("Started tracing");
|
||||||
let config = std::fs::read_to_string(&cli.config).context("Failed to read config file")?;
|
let config = std::fs::read_to_string(&cli.config)?;
|
||||||
let config: Config = toml::from_str(&config).context("Failed to parse config file")?;
|
let config: Config = toml::from_str(&config)?;
|
||||||
|
|
||||||
let data = read_data_from_disk(&config).context("Failed to read data from disk")?;
|
let data = read_data_from_disk(&config)?;
|
||||||
|
|
||||||
let tz = &config.output.timezone;
|
let tz = &config.output.timezone;
|
||||||
let now = Utc::now().with_timezone(tz);
|
let now = Utc::now().with_timezone(tz);
|
||||||
let instances = process_data(&data, &config.output, now).context("Failed to process data")?;
|
let instances = process_data(&data, &config.output, now)?;
|
||||||
output_html(&config.output, &instances, now).context("Failed to output HTML")?;
|
output_html(&config.output, &instances, now)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,16 @@
|
||||||
use super::{CalendarUi, EventInstance, Parameters, SimpleDownload};
|
use super::{CalendarUi, EventInstance, Parameters, SimpleDownload};
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use chrono::DateTime;
|
|
||||||
|
|
||||||
#[derive(Clone, Deserialize)]
|
#[derive(Clone, Default, Deserialize)]
|
||||||
struct SillyDownloadable {
|
struct SillyDownloadable {
|
||||||
/// URL to scrape to download the file from
|
/// URL to scrape to download the file from
|
||||||
download_url: Url,
|
download_url: Option<Url>,
|
||||||
|
|
||||||
/// Disk location to cache the file for debugging
|
/// Disk location to cache the file for debugging
|
||||||
file_path: Utf8PathBuf,
|
file_path: Utf8PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Deserialize)]
|
#[derive(Clone, Default, Deserialize)]
|
||||||
pub(crate) struct Config {
|
pub(crate) struct Config {
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
pub(crate) dl: SillyDownloadable,
|
pub(crate) dl: SillyDownloadable,
|
||||||
|
@ -21,15 +20,8 @@ pub(crate) struct Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
impl Config {
|
||||||
pub(crate) fn simple_download(&self, now: DateTime<chrono_tz::Tz>) -> SimpleDownload {
|
pub(crate) fn simple_download(&self) -> SimpleDownload {
|
||||||
let date = now.format("%Y-%m-%d").to_string();
|
todo!()
|
||||||
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(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,7 +52,7 @@ pub(crate) struct Calendar {
|
||||||
|
|
||||||
impl Calendar {
|
impl Calendar {
|
||||||
fn to_event_instance(&self, params: &Parameters, ev: &Event) -> Result<Option<EventInstance>> {
|
fn to_event_instance(&self, params: &Parameters, ev: &Event) -> Result<Option<EventInstance>> {
|
||||||
let dt = DateTime::from_timestamp_millis(ev.timestamp)
|
let dt = chrono::DateTime::from_timestamp_millis(ev.timestamp)
|
||||||
.context("cannot represent timestamp as a date")?
|
.context("cannot represent timestamp as a date")?
|
||||||
.with_timezone(¶ms.tz);
|
.with_timezone(¶ms.tz);
|
||||||
let dtstart = crate::DatePerhapsTime { dt, all_day: false };
|
let dtstart = crate::DatePerhapsTime { dt, all_day: false };
|
||||||
|
@ -103,47 +95,27 @@ impl Calendar {
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
fn example_config() -> Config {
|
|
||||||
Config {
|
|
||||||
dl: SillyDownloadable {
|
|
||||||
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]
|
#[test]
|
||||||
fn end_to_end() {
|
fn end_to_end() {
|
||||||
let s = r#"
|
let s = r#"
|
||||||
{"data":{"items":[{"timestamp":1748989800000,"durationInMinutes":90,"title":"Foo Bar","description":""},{"timestamp":1749999600000,"durationInMinutes":30,"title":"Snaf Oo","description":""}]},"success":true,"message":""}
|
{"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 cfg = Config {
|
||||||
|
dl: SillyDownloadable {
|
||||||
|
download_url: None,
|
||||||
|
file_path: ".".into(),
|
||||||
|
},
|
||||||
|
ui: CalendarUi {
|
||||||
|
html_url: None,
|
||||||
|
short_name: "asdf".into(),
|
||||||
|
},
|
||||||
|
};
|
||||||
let cal = Calendar::read_from_str(cfg, s).unwrap();
|
let cal = Calendar::read_from_str(cfg, s).unwrap();
|
||||||
let params = Parameters::new(
|
let params =
|
||||||
DateTime::from_timestamp(1748989700, 0)
|
Parameters::new(Utc::now().with_timezone(&chrono_tz::America::Chicago)).unwrap();
|
||||||
.unwrap()
|
|
||||||
.with_timezone(&chrono_tz::America::Chicago),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
let instances = cal.event_instances(¶ms).unwrap();
|
let instances = cal.event_instances(¶ms).unwrap();
|
||||||
|
|
||||||
assert_eq!(instances.len(), 2);
|
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"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue