Use only the icalendar crate instead of ics
This commit is contained in:
parent
ed5a6d2306
commit
1510eb4f3d
10 changed files with 136 additions and 114 deletions
|
@ -15,12 +15,13 @@ use {
|
|||
pub async fn with(api_client: &ApiClient, calendar_name: &str, days: u32) -> Result<()> {
|
||||
let ids = map_events_to_recipe_ids(api_client, calendar_name, days).await?;
|
||||
let ingredients = get_ingredients(api_client, ids).await?;
|
||||
let ingredients = merge_ingredients(ingredients);
|
||||
let md = prepare_grocery_list(&ingredients)?;
|
||||
|
||||
todo!("Recipe ingredients: {:?}", ingredients)
|
||||
println!("{}", md);
|
||||
|
||||
// let ingredients = merge_ingredients(ingredients);
|
||||
// let md = prepare_grocery_list(&ingredients);
|
||||
// save_grocery_list(api_client, "", md).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn map_events_to_recipe_ids(
|
||||
|
@ -70,3 +71,28 @@ where
|
|||
let ingredients = futures::future::try_join_all(ingredients).await?;
|
||||
Ok(ingredients.into_iter().flatten().collect())
|
||||
}
|
||||
|
||||
fn merge_ingredients(mut ingredients: Vec<Ingredient>) -> Vec<Ingredient> {
|
||||
ingredients.sort();
|
||||
|
||||
// TODO actual merging
|
||||
|
||||
ingredients
|
||||
}
|
||||
|
||||
fn prepare_grocery_list(ingredients: &Vec<Ingredient>) -> Result<String> {
|
||||
let mut out = String::new();
|
||||
use std::fmt::Write;
|
||||
|
||||
writeln!(
|
||||
out,
|
||||
"# Grocery list - {}",
|
||||
chrono::Local::now().format("%Y-%m-%d").to_string()
|
||||
)?;
|
||||
for ingredient in ingredients {
|
||||
let ingredient = ingredient.0.as_str();
|
||||
writeln!(out, "* {}", ingredient)?;
|
||||
}
|
||||
|
||||
Ok(out)
|
||||
}
|
||||
|
|
|
@ -4,9 +4,9 @@
|
|||
use {
|
||||
crate::api_client::ApiClient,
|
||||
crate::commands::import,
|
||||
crate::constants,
|
||||
crate::event::{Event, Meal},
|
||||
crate::recipe,
|
||||
crate::{constants, helpers},
|
||||
anyhow::{bail, Result},
|
||||
chrono::naive::NaiveDate,
|
||||
futures::future::try_join_all,
|
||||
|
@ -110,9 +110,6 @@ async fn publish_events<'a, EventsIter>(
|
|||
where
|
||||
EventsIter: Iterator<Item = Event>,
|
||||
{
|
||||
let calendar_prototype: ics::ICalendar =
|
||||
ics::ICalendar::new("2.0", constants::CALENDAR_PROVIDER);
|
||||
|
||||
let dav_base = api_client
|
||||
.rest()
|
||||
.head(api_client.base_url().join("/.well-known/caldav")?)
|
||||
|
@ -125,18 +122,37 @@ where
|
|||
calendar.to_lowercase().as_str().replace(" ", "-")
|
||||
))?;
|
||||
|
||||
let calendar_prototype = &calendar_prototype;
|
||||
let calendar_url = &calendar_url;
|
||||
let update_requests = events.map(|ev| async move {
|
||||
let url = calendar_url.join(&format!("{}.ics", ev.uid)).unwrap();
|
||||
let mut cal = calendar_prototype.clone();
|
||||
cal.add_event(ev.into());
|
||||
let alarm_text_repr = format!(
|
||||
"BEGIN:VALARM\nACTION:DISPLAY\nTRIGGER:-PT15M\nDESCRIPTION:{}\nEND:VALARM",
|
||||
&helpers::ical_escape_text(&ev.recipe.name)
|
||||
);
|
||||
|
||||
let cal = icalendar::Calendar::new()
|
||||
.push::<icalendar::Event>(ev.into())
|
||||
.done();
|
||||
|
||||
let cal_as_string = (&cal.to_string())
|
||||
.replacen(
|
||||
// need to hack around inability to set PRODID in icalendar::Calendar
|
||||
"PRODID:ICALENDAR-RS",
|
||||
&format!("PRODID:{}", constants::CALENDAR_PROVIDER),
|
||||
1,
|
||||
)
|
||||
.replacen(
|
||||
// need to hack around inability to set VALARM in icalendar::Event
|
||||
"END:VEVENT",
|
||||
&format!("{}\nEND:VEVENT", alarm_text_repr),
|
||||
1,
|
||||
);
|
||||
|
||||
api_client
|
||||
.rest()
|
||||
.put(url)
|
||||
.header("Content-Type", "text/calendar; charset=utf-8")
|
||||
.body(cal.to_string())
|
||||
.body(cal_as_string)
|
||||
.send()
|
||||
.await
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue