diff --git a/internal/integrations/mealie.go b/internal/integrations/mealie.go index b9235d7c..3bc53b0f 100644 --- a/internal/integrations/mealie.go +++ b/internal/integrations/mealie.go @@ -80,9 +80,44 @@ func (m *MealieRecipe) Schema() models.RecipeSchema { yield = int16(m.RecipeServings) } + extractTimeMinutes := func(s string) int64 { + s = strings.TrimPrefix(s, "PT") + + var t int64 + + before, after, found := strings.Cut(s, "H") + if found { + v, err := strconv.ParseInt(before, 10, 16) + if err == nil { + t += v + } + + before, after, _ = strings.Cut(after, "M") + } else { + before, after, _ = strings.Cut(s, "M") + } + + v, err := strconv.ParseInt(before, 10, 16) + if err == nil { + t += v + } + + return t + } + var cookTime string if m.CookTime != nil { cookTime = *m.CookTime + } else if m.PerformTime != "" && m.PrepTime != "" { + minutesCook := extractTimeMinutes(m.PerformTime) - extractTimeMinutes(m.PrepTime) + + if minutesCook > 60 { + hours := minutesCook / 60 + minutes := minutesCook % 60 + cookTime = "PT" + strconv.FormatInt(hours, 10) + "H" + strconv.FormatInt(minutes, 10) + "M" + } else { + cookTime = "PT" + strconv.FormatInt(minutesCook, 10) + "M" + } } dateUpdated := m.DateUpdated @@ -243,28 +278,50 @@ func (m *MealieRecipe) UnmarshalJSON(data []byte) error { m.RecipeYield = strconv.FormatFloat(temp.RecipeYieldQuantity, 'f', -1, 64) } + normalizeTime := func(s string) string { + if s == "" || strings.HasPrefix(s, "PT") { + return s + } + + t := "PT" + parts := strings.Split(s, " ") + if len(parts) == 2 { + if strings.Contains(s, "min") { + t += parts[0] + "M" + } else { + t += parts[0] + "H" + } + } else if len(parts) == 4 { + t += parts[0] + "H" + parts[1] + "M" + } + + return t + } + if temp.TotalTime != "" { - m.TotalTime = temp.TotalTime + m.TotalTime = normalizeTime(temp.TotalTime) } else if temp.TotalTimeOld != "" { - m.TotalTime = temp.TotalTimeOld + m.TotalTime = normalizeTime(temp.TotalTimeOld) } if temp.PrepTime != "" { - m.PrepTime = temp.PrepTime + m.PrepTime = normalizeTime(temp.PrepTime) } else if temp.PrepTimeOld != "" { - m.PrepTime = temp.PrepTimeOld + m.PrepTime = normalizeTime(temp.PrepTimeOld) } if temp.CookTime != nil { - m.CookTime = temp.CookTime + s := normalizeTime(*temp.CookTime) + m.CookTime = &s } else if temp.CookTimeOld != nil { - m.CookTime = temp.CookTimeOld + s := normalizeTime(*temp.CookTimeOld) + m.CookTime = &s } if temp.PerformTime != "" { - m.PerformTime = temp.PerformTime + m.PerformTime = normalizeTime(temp.PerformTime) } else if temp.PerformTimeOld != "" { - m.PerformTime = temp.PerformTimeOld + m.PerformTime = normalizeTime(temp.PerformTimeOld) } m.Description = temp.Description @@ -811,16 +868,29 @@ func MealieImport(baseURL, username, password string, client *http.Client, uploa } } + normalizeTime := func(s string) string { + s = strings.TrimPrefix(s, "PT") + s = strings.Replace(s, "M", " minutes ", 1) + s = strings.Replace(s, "H", " hours ", 1) + return s + } + + var cook string + if m.CookTime != nil { + cook = normalizeTime(*m.CookTime) + } + times := models.Times{ - Prep: duration.From(m.PrepTime), - Cook: duration.From(m.PerformTime), + Prep: duration.From(normalizeTime(m.PrepTime)), + Cook: duration.From(cook), + Total: duration.From(normalizeTime(m.TotalTime)), } if m.CookTime == nil && m.PrepTime != "" { if m.TotalTime != "" { - times.Cook = duration.From(m.TotalTime) - duration.From(m.PrepTime) + times.Cook = times.Total - times.Prep } else if m.PerformTime != "" { - times.Cook = duration.From(m.PerformTime) - duration.From(m.PrepTime) + times.Cook = duration.From(normalizeTime(m.PerformTime)) - times.Prep } } diff --git a/internal/integrations/mealie_test.go b/internal/integrations/mealie_test.go index 3adbbf19..ead2263c 100644 --- a/internal/integrations/mealie_test.go +++ b/internal/integrations/mealie_test.go @@ -55,7 +55,7 @@ func TestMealieImport(t *testing.T) { TotalCarbohydrates: "24.7", TotalFat: "24.6", }, - Times: models.Times{Prep: 15 * time.Minute, Cook: 30 * time.Minute}, + Times: models.Times{Prep: 15 * time.Minute, Cook: 30 * time.Minute, Total: 45 * time.Minute}, Tools: []models.HowToItem{}, UpdatedAt: time.Date(2024, 04, 12, 0, 0, 0, 0, time.UTC), URL: "https://pinchofyum.com/30-minute-meal-prep-roasted-vegetable-bowls-with-green-tahini", diff --git a/internal/server/handlers_recipes.go b/internal/server/handlers_recipes.go index e82b51ce..16157cc1 100644 --- a/internal/server/handlers_recipes.go +++ b/internal/server/handlers_recipes.go @@ -990,12 +990,9 @@ func (s *Server) recipeScaleHandler() http.HandlerFunc { yield, err := strconv.ParseInt(r.URL.Query().Get("yield"), 10, 16) if err != nil { - s.Brokers.SendToast(models.NewErrorGeneralToast("No yield in the query."), userID) w.WriteHeader(http.StatusBadRequest) return - } - - if yield <= 0 { + } else if yield <= 0 { s.Brokers.SendToast(models.NewErrorGeneralToast("Yield must be greater than zero."), userID) w.WriteHeader(http.StatusBadRequest) return diff --git a/internal/server/handlers_recipes_test.go b/internal/server/handlers_recipes_test.go index fdfde92f..134f3cbe 100644 --- a/internal/server/handlers_recipes_test.go +++ b/internal/server/handlers_recipes_test.go @@ -970,11 +970,6 @@ func TestHandlers_Recipes_Scale(t *testing.T) { in string want string }{ - { - name: "yield query parameter must be present", - in: "", - want: "No yield in the query.", - }, { name: "yield query parameter must be greater than zero", in: "-1",