Skip to content

Commit

Permalink
Fix times when importing from Mealie (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
reaper47 authored Jan 14, 2025
1 parent 71f9ebe commit f50ffc2
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 22 deletions.
94 changes: 82 additions & 12 deletions internal/integrations/mealie.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/integrations/mealie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 1 addition & 4 deletions internal/server/handlers_recipes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions internal/server/handlers_recipes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f50ffc2

Please sign in to comment.