Grocery list: Fix filtering of recipes
This commit is contained in:
parent
5e796365f1
commit
e69d8fc24c
|
@ -18,7 +18,7 @@
|
|||
"kind": "bin"
|
||||
}
|
||||
},
|
||||
"args": ["groceries", "Cucina"],
|
||||
"args": ["groceries", "Cucina", "Note/Spesa/Groceries.md"],
|
||||
"env": {
|
||||
"RUST_LOG": "debug",
|
||||
},
|
||||
|
|
|
@ -217,7 +217,7 @@
|
|||
"2022-08-04","giovedì",,"https://ricette.giallozafferano.it/Petto-di-pollo-ai-peperoni.html"
|
||||
"2022-08-05","venerdì",,
|
||||
"2022-08-06","sabato","https://ricette.giallozafferano.it/Spaghetti-di-riso-con-carne-e-verdure.html","https://ricette.giallozafferano.it/Pasta-con-pomodorini-e-stracchino.html"
|
||||
"2022-08-07","domenica",,"https://ricette.giallozafferano.it/Insalata-di-riso-vegetariana.html"
|
||||
"2022-08-07","domenica","https://ricette.giallozafferano.it/Insalata-di-riso-vegetariana.html",
|
||||
"2022-08-08","lunedì",,"https://ricette.giallozafferano.it/Torta-salata-di-melanzane.html"
|
||||
"2022-08-09","martedì",,"https://ricette.giallozafferano.it/Pasta-fredda-con-pesto-senz-aglio.html"
|
||||
"2022-08-10","mercoledì",,"https://ricette.giallozafferano.it/Insalata-con-uova-strapazzate.html"
|
||||
|
|
|
|
@ -108,6 +108,18 @@ impl ApiClient {
|
|||
Tz::Offset: std::fmt::Display,
|
||||
{
|
||||
let report_method = reqwest::Method::from_bytes(b"REPORT").unwrap();
|
||||
|
||||
let start = date_range
|
||||
.start
|
||||
.naive_utc()
|
||||
.format(constants::ICAL_UTCTIME_FMT)
|
||||
.to_string();
|
||||
let end = date_range
|
||||
.end
|
||||
.naive_utc()
|
||||
.format(constants::ICAL_UTCTIME_FMT)
|
||||
.to_string();
|
||||
|
||||
let events_xml = self
|
||||
.rest(|client| async {
|
||||
let response = client
|
||||
|
@ -121,8 +133,8 @@ impl ApiClient {
|
|||
.header("Prefer", "return-minimal")
|
||||
.header("Content-Type", "application/xml; charset=utf-8")
|
||||
.header("Depth", 1)
|
||||
.body(format!(
|
||||
"<c:calendar-query xmlns:d=\"DAV:\" xmlns:c=\"urn:ietf:params:xml:ns:caldav\">
|
||||
.body(format!("<?xml version=\"1.0\" encoding=\"utf-8\" ?>
|
||||
<c:calendar-query xmlns:d=\"DAV:\" xmlns:c=\"urn:ietf:params:xml:ns:caldav\">
|
||||
<d:prop>
|
||||
<c:calendar-data />
|
||||
</d:prop>
|
||||
|
@ -133,7 +145,7 @@ impl ApiClient {
|
|||
</c:prop-filter>
|
||||
<c:comp-filter name=\"VEVENT\">
|
||||
<c:prop-filter name=\"DTSTART\">
|
||||
<time-range start=\"{}\" end=\"{}\" />
|
||||
<c:time-range start=\"{}\" end=\"{}\" />
|
||||
</c:prop-filter>
|
||||
</c:comp-filter>
|
||||
</c:comp-filter>
|
||||
|
@ -141,16 +153,8 @@ impl ApiClient {
|
|||
</c:calendar-query>
|
||||
",
|
||||
constants::CALENDAR_PROVIDER,
|
||||
date_range
|
||||
.start
|
||||
.naive_utc()
|
||||
.format(constants::ICAL_UTCTIME_FMT)
|
||||
.to_string(),
|
||||
date_range
|
||||
.end
|
||||
.naive_utc()
|
||||
.format(constants::ICAL_UTCTIME_FMT)
|
||||
.to_string()
|
||||
start,
|
||||
end
|
||||
))
|
||||
.send()
|
||||
.await;
|
||||
|
|
|
@ -40,7 +40,7 @@ async fn map_events_to_recipe_ids(
|
|||
|
||||
let all_events = api_client.get_events(calendar_name, date_range).await?;
|
||||
|
||||
let recipe_id_regex: Regex = Regex::new(r"cookbook@(\d+)").unwrap();
|
||||
let recipe_id_regex: Regex = Regex::new(r"^cookbook@(\d+)$").unwrap();
|
||||
let recipe_ids = all_events
|
||||
.iter()
|
||||
.flat_map(|event| event.property_value("DESCRIPTION"))
|
||||
|
@ -55,7 +55,7 @@ async fn map_events_to_recipe_ids(
|
|||
async fn get_ingredients<RecipeIds>(
|
||||
api_client: &ApiClient,
|
||||
recipe_ids: RecipeIds,
|
||||
) -> Result<Vec<Ingredient>>
|
||||
) -> Result<Vec<(Ingredient, String)>>
|
||||
where
|
||||
RecipeIds: IntoIterator<Item = usize>,
|
||||
{
|
||||
|
@ -74,7 +74,10 @@ where
|
|||
|
||||
response.json::<Recipe>().await.map(|r| {
|
||||
log::info!("Retrieved ingredients for '{}'", r.name);
|
||||
let recipe_name = r.name.clone();
|
||||
r.ingredients
|
||||
.into_iter()
|
||||
.map(move |i| (i, recipe_name.clone()))
|
||||
})
|
||||
});
|
||||
|
||||
|
@ -82,7 +85,7 @@ where
|
|||
Ok(ingredients.into_iter().flatten().collect())
|
||||
}
|
||||
|
||||
fn merge_ingredients(mut ingredients: Vec<Ingredient>) -> Vec<Ingredient> {
|
||||
fn merge_ingredients(mut ingredients: Vec<(Ingredient, String)>) -> Vec<(Ingredient, String)> {
|
||||
ingredients.sort();
|
||||
|
||||
// TODO actual merging
|
||||
|
@ -90,7 +93,7 @@ fn merge_ingredients(mut ingredients: Vec<Ingredient>) -> Vec<Ingredient> {
|
|||
ingredients
|
||||
}
|
||||
|
||||
fn prepare_grocery_list(ingredients: &Vec<Ingredient>) -> Result<String> {
|
||||
fn prepare_grocery_list(ingredients: &Vec<(Ingredient, String)>) -> Result<String> {
|
||||
let mut out = String::new();
|
||||
use std::fmt::Write;
|
||||
|
||||
|
@ -101,8 +104,8 @@ fn prepare_grocery_list(ingredients: &Vec<Ingredient>) -> Result<String> {
|
|||
)?;
|
||||
writeln!(out)?; // leave an empty line
|
||||
for ingredient in ingredients {
|
||||
let ingredient = ingredient.0.as_str();
|
||||
writeln!(out, "- [ ] {}", ingredient)?;
|
||||
let ingredient_s = ingredient.0 .0.as_str();
|
||||
writeln!(out, "- [ ] {} ({})", ingredient_s, ingredient.1)?;
|
||||
}
|
||||
|
||||
Ok(out)
|
||||
|
|
Loading…
Reference in New Issue