ai cooking
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #182 from paulgmiller/ingredients

reintrocuce a ingredient cli

authored by

Paul Miller and committed by
GitHub
ef5cfbbd 8e902752

+119 -1
+59
cmd/ingredients/main.go
··· 1 + package main 2 + 3 + import ( 4 + "careme/internal/cache" 5 + "careme/internal/config" 6 + "careme/internal/recipes" 7 + "context" 8 + "flag" 9 + "fmt" 10 + "log" 11 + ) 12 + 13 + func main() { 14 + var ingredient string 15 + var location string 16 + flag.StringVar(&ingredient, "ingredient", "", "Ingredient to filter recipes") 17 + flag.StringVar(&ingredient, "i", "", "Ingredient to filter recipes") 18 + flag.StringVar(&location, "location", "", "Location for recipe sourcing (e.g., 70100023)") 19 + flag.StringVar(&location, "l", "", "Location for recipe sourcing (short form)") 20 + flag.Parse() 21 + ctx := context.Background() 22 + cache, err := cache.MakeCache() 23 + if err != nil { 24 + log.Fatalf("failed to create cache: %s", err) 25 + } 26 + 27 + cfg, err := config.Load() 28 + if err != nil { 29 + log.Fatalf("failed to load configuration: %s", err) 30 + } 31 + 32 + generator, err := recipes.NewGenerator(cfg, cache) 33 + if err != nil { 34 + log.Fatalf("failed to create recipe generator: %s", err) 35 + } 36 + 37 + g, ok := generator.(*recipes.Generator) 38 + if !ok { 39 + log.Fatalf("failed to cast generator to *recipes.Generator") 40 + } 41 + 42 + f := recipes.Filter(ingredient, []string{"*"}, false /*frozen*/) 43 + ings, err := g.GetIngredients(ctx, location, f, 0) 44 + if err != nil { 45 + log.Fatalf("failed to get ingredients: %s", err) 46 + } 47 + 48 + for _, i := range ings { 49 + fmt.Println(toString(i.Description)) 50 + } 51 + 52 + } 53 + 54 + func toString(s *string) string { 55 + if s == nil { 56 + return "" 57 + } 58 + return *s 59 + }
+60
cmd/users/main.go
··· 1 + package main 2 + 3 + import ( 4 + "careme/internal/cache" 5 + "careme/internal/users" 6 + "context" 7 + "flag" 8 + "log" 9 + "strings" 10 + ) 11 + 12 + func main() { 13 + var move bool 14 + var userEmail string 15 + flag.BoolVar(&move, "move", false, "Move ingredient to the top of the list") 16 + flag.StringVar(&userEmail, "email", "nobody", "email of user id to move") 17 + flag.Parse() 18 + ctx := context.Background() 19 + cache, err := cache.MakeCache() 20 + if err != nil { 21 + log.Fatalf("failed to create cache: %s", err) 22 + } 23 + 24 + userStorage := users.NewStorage(cache) 25 + userList, err := userStorage.List(ctx) 26 + if err != nil { 27 + log.Fatalf("failed to list users: %v", err) 28 + } 29 + log.Printf("found %d users", len(userList)) 30 + log.Printf("looking for user with email containing \"%s\"", userEmail) 31 + var old users.User 32 + var new []users.User 33 + for _, u := range userList { 34 + //if !slices.Contains(u.Email, userEmail) { 35 + // continue 36 + //} 37 + log.Printf("user: %s, email: %s recipes: %d", u.ID, u.Email, len(u.LastRecipes)) 38 + if isOld(u.ID) { 39 + old = u 40 + } else { 41 + new = append(new, u) 42 + } 43 + } 44 + 45 + for _, n := range new { 46 + log.Printf("%s -> %s, email: %s ", old.ID, n.ID, n.Email) 47 + if move { 48 + n.LastRecipes = append(old.LastRecipes, n.LastRecipes...) 49 + n.FavoriteStore = old.FavoriteStore 50 + if err := userStorage.Update(&n); err != nil { 51 + log.Fatalf("failed to save user: %v", err) 52 + } 53 + } 54 + } 55 + 56 + } 57 + 58 + func isOld(userid string) bool { 59 + return !strings.HasPrefix(userid, "user_") 60 + }
-1
internal/users/storage.go
··· 100 100 } 101 101 var users []User 102 102 for _, id := range userids { 103 - slog.InfoContext(ctx, "loading user", "id", id) 104 103 user, err := s.GetByID(id) 105 104 if err != nil { 106 105 return nil, err