// NameToNutrientMigration ... func NameToNutrientMigration(s *State) { for _, recipe := range s.Recipes { recipe.Nutrients = *SetDietaryInfo(&recipe.Nutrients, recipe.Name) fmt.Println(models.RemoveMetaData(recipe.Name)) recipe.Name = models.RemoveMetaData(recipe.Name) x := struct { Name string `json:"name"` Nutrients models.NutrientInfoResponse `json:"nutrients"` ID string `json:"objectId"` UUID string `json:"uuid"` }{ Name: models.RemoveMetaData(recipe.Name), Nutrients: *SetDietaryInfo(&recipe.Nutrients, recipe.Name), ID: recipe.ObjectID(), UUID: lib.GetMD5Hash(models.RemoveMetaData(recipe.Name)), } xString, _ := json.Marshal(x) fmt.Println(string(xString)) _, status, errs := s.DB.Put(x, "Recipe", recipe.ID) if errs != nil || status == 400 { log.Error(status) log.Error(errs) log.Error(errors.Errorf("Unable to post recipe with ID: %s", recipe.ID)) break } // time.Sleep(1 * time.Second) } }
func saveOfferings(s *State, v models.VenueInfo) { offers := []models.ParseOffering{} duplicates, new := 0, 0 for _, item := range v.MealsList { meal := item.Meal for _, menu := range item.Menus { day := v.Date.Day() month := int(v.Date.Month()) year := v.Date.Year() uuidStr := fmt.Sprintf( "%d%d%d%s%s%s", day, month, year, menu.Name, meal.Name, v.Key) uuid := lib.GetMD5Hash(uuidStr) if s.Offerings[uuid].ObjectID() == "" { rs := mmRecipes(s, meal.ID, menu.ID, v.Recipes) offer := models.ParseOffering{ Venue: v.Key, Day: v.Date.Day(), Month: int(v.Date.Month()), Year: v.Date.Year(), MenuName: menu.Name, MealName: meal.Name, Class: "Offering", UUID: uuid, } for _, s := range rs { offer.AddRecipe(s) } new++ offers = append(offers, offer) } else { duplicates++ } } } for _, o := range offers { returnObj, status, errs := s.DB.Post(o) if errs != nil { log.Error(status) log.Error(errors.Errorf("Unable to post recipe with ID: %s", o.UUID)) continue } offering := returnObj.(models.ParseOffering) s.Offerings[offering.UUID] = offering log.Debug("Created new offering with objectId: ", offering.ObjectID()) } log.WithFields(logrus.Fields{ "Saved": new, "Duplicate": duplicates, }).Info("Scraped Offerings") }
func saveRecipes(s *State, v models.VenueInfo) { u := uniqueRecipes(v.Recipes) var duplicates, new int for _, recipe := range u { if s.Recipes[recipe.ID].DartmouthID != recipe.ID { c := models.CreatedBy{ Kind: "Pointer", ClassName: "_User", ObjectID: "95xfYTL7GG", } returnObj, status, errs := s.DB.Post(models.ParseRecipe{ Name: models.RemoveMetaData(recipe.Name), Category: recipe.Category, DartmouthID: recipe.ID, Rank: recipe.Rank, UUID: lib.GetMD5Hash(models.RemoveMetaData(recipe.Name)), Nutrients: *SetDietaryInfo(&recipe.Nutrients, recipe.Name), Class: "Recipe", CreatedBy: c, }) if errs != nil || status == 400 { log.Error(status) log.Error(errors.Errorf("Unable to post recipe with ID: %d", recipe.ID)) continue } returnedRecipe := returnObj.(models.ParseRecipe) s.Recipes[recipe.ID] = returnedRecipe log.Debug("Created new recipe with objectId: ", returnedRecipe.ObjectID()) new++ } else { duplicates++ } } log.WithFields(logrus.Fields{ "Saved": new, "Duplicate": duplicates, }).Info("Scraped Recipes") }
func offeringExists(s *State, vK string, m, ml string, d time.Time) bool { uuidStr := fmt.Sprintf("%d%d%d%s%s%s", d.Day(), int(d.Month()), d.Year(), m, ml, vK) uuid := lib.GetMD5Hash(uuidStr) return s.Offerings[uuid].ObjectID() != "" }