From e69d8fc24ccfa378deedee040ab2b65d68ae555b Mon Sep 17 00:00:00 2001 From: Matteo Settenvini Date: Mon, 8 Aug 2022 08:06:17 +0200 Subject: [PATCH] Grocery list: Fix filtering of recipes --- .vscode/launch.json | 2 +- examples/example-schedule.csv | 2 +- src/api_client.rs | 66 +++++++++++++++++++---------------- src/commands/groceries.rs | 15 ++++---- 4 files changed, 46 insertions(+), 39 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 205502b..024adfc 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -18,7 +18,7 @@ "kind": "bin" } }, - "args": ["groceries", "Cucina"], + "args": ["groceries", "Cucina", "Note/Spesa/Groceries.md"], "env": { "RUST_LOG": "debug", }, diff --git a/examples/example-schedule.csv b/examples/example-schedule.csv index 7c9eff1..54bed71 100644 --- a/examples/example-schedule.csv +++ b/examples/example-schedule.csv @@ -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" diff --git a/src/api_client.rs b/src/api_client.rs index 229169b..6217140 100644 --- a/src/api_client.rs +++ b/src/api_client.rs @@ -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,37 +133,29 @@ impl ApiClient { .header("Prefer", "return-minimal") .header("Content-Type", "application/xml; charset=utf-8") .header("Depth", 1) - .body(format!( - " - - - - - - - {} - - - - - - - - - - ", - 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() - )) + .body(format!(" + + + + + + + + {} + + + + + + + + + + ", + constants::CALENDAR_PROVIDER, + start, + end + )) .send() .await; Ok(response?) diff --git a/src/commands/groceries.rs b/src/commands/groceries.rs index 18a5c2c..67525c0 100644 --- a/src/commands/groceries.rs +++ b/src/commands/groceries.rs @@ -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( api_client: &ApiClient, recipe_ids: RecipeIds, -) -> Result> +) -> Result> where RecipeIds: IntoIterator, { @@ -74,7 +74,10 @@ where response.json::().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) -> Vec { +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) -> Vec { ingredients } -fn prepare_grocery_list(ingredients: &Vec) -> Result { +fn prepare_grocery_list(ingredients: &Vec<(Ingredient, String)>) -> Result { let mut out = String::new(); use std::fmt::Write; @@ -101,8 +104,8 @@ fn prepare_grocery_list(ingredients: &Vec) -> Result { )?; 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)