/
main_old.go
377 lines (368 loc) · 13.3 KB
/
main_old.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package main
type Keyword struct {
Name string
Category string
}
type EcoscoreData struct {
Adjustments struct {
OriginsOfIngredients struct {
AggregatedOrigins []struct {
EpiScore int `json:"epi_score"`
Origin string `json:"origin"`
Percent int `json:"percent"`
TransportationScore int `json:"transportation_score"`
} `json:"aggregated_origins"`
EpiScore int `json:"epi_score"`
EpiValue int `json:"epi_value"`
OriginsFromOriginsField []string `json:"origins_from_origins_field"`
TransportationScore int `json:"transportation_score"`
TransportationScores struct {
World int `json:"world"`
Xk int `json:"xk"`
} `json:"transportation_scores"`
TransportationValue int `json:"transportation_value"`
TransportationValues struct {
World int `json:"world"`
Xk int `json:"xk"`
} `json:"transportation_values"`
Value int `json:"value"`
Values struct {
World int `json:"world"`
Xk int `json:"xk"`
} `json:"values"`
Warning string `json:"warning"`
} `json:"origins_of_ingredients"`
Packaging struct {
NonRecyclableAndNonBiodegradableMaterials int `json:"non_recyclable_and_non_biodegradable_materials"`
Value int `json:"value"`
Warning string `json:"warning"`
} `json:"packaging"`
ProductionSystem struct {
Labels []interface{} `json:"labels"`
Value int `json:"value"`
Warning string `json:"warning"`
} `json:"production_system"`
ThreatenedSpecies struct {
Warning string `json:"warning"`
} `json:"threatened_species"`
} `json:"adjustments"`
Agribalyse struct {
Warning string `json:"warning"`
} `json:"agribalyse"`
Missing struct {
Categories int `json:"categories"`
Ingredients int `json:"ingredients"`
Labels int `json:"labels"`
Origins int `json:"origins"`
Packagings int `json:"packagings"`
} `json:"missing"`
MissingAgribalyseMatchWarning int `json:"missing_agribalyse_match_warning"`
MissingKeyData int `json:"missing_key_data"`
Scores struct {
} `json:"scores"`
Status string `json:"status"`
}
type Product struct {
ID string `json:"_id"`
Keywords []string `json:"_keywords"`
AddedCountriesTags []string `json:"added_countries_tags"`
Allergens string `json:"allergens"`
AllergensFromIngredients string `json:"allergens_from_ingredients"`
AllergensFromUser string `json:"allergens_from_user"`
AllergensHierarchy []string `json:"allergens_hierarchy"`
AllergensTags []string `json:"allergens_tags"`
CategoriesProperties struct{} `json:"categories_properties"`
CategoriesPropertiesTags []string `json:"categories_properties_tags"`
CheckersTags []string `json:"checkers_tags"`
Code string `json:"code"`
CodesTags []string `json:"codes_tags"`
Complete int `json:"complete"`
Completeness float64 `json:"completeness"`
CorrectorsTags []string `json:"correctors_tags"`
Countries string `json:"countries"`
CountriesHierarchy []string `json:"countries_hierarchy"`
CountriesTags []string `json:"countries_tags"`
CreatedT int `json:"created_t"`
Creator string `json:"creator"`
DataQualityBugsTags []string `json:"data_quality_bugs_tags"`
DataQualityErrorsTags []string `json:"data_quality_errors_tags"`
DataQualityInfoTags []string `json:"data_quality_info_tags"`
DataQualityTags []string `json:"data_quality_tags"`
DataQualityWarningsTags []string `json:"data_quality_warnings_tags"`
EcoscoreData EcoscoreData `json:"ecoscore_data"`
EcoscoreGrade string `json:"ecoscore_grade"`
EcoscoreTags []string `json:"ecoscore_tags"`
EditorsTags []string `json:"editors_tags"`
EntryDatesTags []string `json:"entry_dates_tags"`
ExpirationDate string `json:"expiration_date"`
FoodGroupsTags []string `json:"food_groups_tags"`
ImageFrontSmallURL string `json:"image_front_small_url"`
ImageFrontThumbURL string `json:"image_front_thumb_url"`
ImageFrontURL string `json:"image_front_url"`
ImageIngredientsSmallURL string `json:"image_ingredients_small_url"`
ImageIngredientsThumbURL string `json:"image_ingredients_thumb_url"`
ImageIngredientsURL string `json:"image_ingredients_url"`
ImageSmallURL string `json:"image_small_url"`
ImageThumbURL string `json:"image_thumb_url"`
ImageURL string `json:"image_url"`
Images struct {
// ... nested image structure ...
} `json:"images"`
InformersTags []string `json:"informers_tags"`
InterfaceVersionCreated string `json:"interface_version_created"`
InterfaceVersionModified string `json:"interface_version_modified"`
Lang string `json:"lang"`
Languages struct {
// ... nested languages structure ...
} `json:"languages"`
LanguagesCodes struct {
En int `json:"en"`
Ro int `json:"ro"`
} `json:"languages_codes"`
LanguagesHierarchy []string `json:"languages_hierarchy"`
LanguagesTags []string `json:"languages_tags"`
LastEditDatesTags []string `json:"last_edit_dates_tags"`
LastEditor string `json:"last_editor"`
LastImageDatesTags []string `json:"last_image_dates_tags"`
LastImageT int `json:"last_image_t"`
LastModifiedBy string `json:"last_modified_by"`
LastModifiedT int `json:"last_modified_t"`
LC string `json:"lc"`
MainCountriesTags []string `json:"main_countries_tags"`
MaxImgID string `json:"max_imgid"`
MiscTags []string `json:"misc_tags"`
NovaGroupDebug string `json:"nova_group_debug"`
NovaGroupError string `json:"nova_group_error"`
NovaGroupsTags []string `json:"nova_groups_tags"`
NutrientLevels struct {
// ... nested nutrient levels structure ...
} `json:"nutrient_levels"`
NutrientLevelsTags []string `json:"nutrient_levels_tags"`
Nutriments struct {
// ... nested nutriments structure ...
} `json:"nutriments"`
NutritionDataPer string `json:"nutrition_data_per"`
NutritionDataPreparedPer string `json:"nutrition_data_prepared_per"`
NutritionGradesTags []string `json:"nutrition_grades_tags"`
NutritionScoreBeverage int `json:"nutrition_score_beverage"`
NutritionScoreDebug string `json:"nutrition_score_debug"`
NutritionScoreWarningNoFiber int `json:"nutrition_score_warning_no_fiber"`
PackagingMaterialsTags []string `json:"packaging_materials_tags"`
PackagingRecyclingTags []string `json:"packaging_recycling_tags"`
PackagingShapesTags []string `json:"packaging_shapes_tags"`
Packagings []struct {
// ... nested packagings structure ...
} `json:"packagings"`
PhotographersTags []string `json:"photographers_tags"`
PnnsGroups1 string `json:"pnns_groups_1"`
PnnsGroups1Tags []string `json:"pnns_groups_1_tags"`
PnnsGroups2 string `json:"pnns_groups_2"`
PnnsGroups2Tags []string `json:"pnns_groups_2_tags"`
PopularityKey int `json:"popularity_key"`
PopularityTags []string `json:"popularity_tags"`
ProductNameEn string `json:"product_name_en"`
RemovedCountriesTags []string `json:"removed_countries_tags"`
Rev int `json:"rev"`
ScansN int `json:"scans_n"`
SelectedImages struct {
// ... nested selected images structure ...
} `json:"selected_images"`
Sortkey int `json:"sortkey"`
States string `json:"states"`
StatesHierarchy []string `json:"states_hierarchy"`
StatesTags []string `json:"states_tags"`
Traces string `json:"traces"`
TracesFromIngredients string `json:"traces_from_ingredients"`
TracesFromUser string `json:"traces_from_user"`
TracesHierarchy []string `json:"traces_hierarchy"`
TracesTags []string `json:"traces_tags"`
UniqueScansN int `json:"unique_scans_n"`
UnknownNutrientsTags []string `json:"unknown_nutrients_tags"`
UpdateKey string `json:"update_key"`
URL string `json:"url"`
WeighersTags []string `json:"weighers_tags"`
}
type ProductData struct {
Count int `json:"count"`
Page int `json:"page"`
PageCount int `json:"page_count"`
PageSize int `json:"page_size"`
Products []Product `json:"products"`
Skip int `json:"skip"`
// ... add more fields as needed ...
}
func isBreakfastFood(category string) bool {
// Definim aici categoriile potrivite pentru micul dejun
breakfastCategories := map[string]bool{
"Protein": true,
"Carbs": true,
"Fruits": true,
}
return breakfastCategories[category]
}
//const openFoodFactsAPI = "https://ro.openfoodfacts.org/cgi/search.pl"
//
//func fetchData(url string, wg *sync.WaitGroup, results chan<- []map[string]interface{}) {
// defer wg.Done()
//
// req, err := http.NewRequest(http.MethodGet, url, nil)
// if err != nil {
// fmt.Printf("client: could not create request: %s\n", err)
// return
// }
//
// res, err := http.DefaultClient.Do(req)
// if err != nil {
// fmt.Printf("client: error making http request: %s\n", err)
// return
// }
// defer res.Body.Close()
//
// resBody, err := io.ReadAll(res.Body)
// if err != nil {
// fmt.Printf("client: could not read response body: %s\n", err)
// return
// }
//
// var data map[string]interface{}
//
// err = json.Unmarshal(resBody, &data)
// if err != nil {
// fmt.Println(err)
// return
// }
//
// results <- data
//}
//
//func NutritionPlan(ctx *gin.Context) {
// var wg sync.WaitGroup
// results := make(chan []map[string]interface{})
//
// products := []Keyword{
// {Name: "oua", Category: "Protein"},
// {Name: "iaurt", Category: "Dairy"},
// {Name: "ciocolata", Category: "Dairy"},
// {Name: "Cereale", Category: "Carbs"},
// {Name: "Fructe proaspete", Category: "Fruits"},
// {Name: "Cârnați", Category: "Protein"},
// }
//
// // Afișam produsele pentru micul dejun
// fmt.Println("Produse pentru micul dejun:")
// for _, p := range products {
// //if isBreakfastFood(p.Category) {
// //fmt.Printf("%s\n", p.Name)
// wg.Add(1)
// go fetchData(fmt.Sprintf("%s?action=process&search_terms=%s&page_size=5&json=1", openFoodFactsAPI, p.Name), &wg, results)
// //}
// }
//
// go func() {
// wg.Wait()
// close(results)
// }()
//
// var allData []map[string]interface{}
// for data := range results {
// allData = append(allData, data)
// }
//
// ctx.JSON(http.StatusOK, gin.H{"products": len(allData)})
//}
//func samain() {
// //asdasd
// r := gin.Default()
// // r.GET("/nutrition-plan", NutritionPlan)
// r.POST("api/v1/nutrition-calculator", src.CalculateCalories)
// r.Run(":8088")
//}
//package main
//
//import (
//"encoding/json"
//"fmt"
//"io"
//"net/http"
//"sync"
//)
//
//type Product struct {
// ProductName string `json:"product_name"`
//}
//
//type SearchResult struct {
// Products []Product `json:"products"`
//}
//
//func searchAndCollect(searchTerm string, ch chan<- []Product, wg *sync.WaitGroup) {
// defer wg.Done()
//
// // Construct the API URL
// apiURL := fmt.Sprintf("https://ro.openfoodfacts.org/cgi/search.pl?search_terms=%s&json=1", searchTerm)
//
// // Make the API request
// response, err := http.Get(apiURL)
// if err != nil {
// fmt.Printf("Error making API request for %s: %v\n", searchTerm, err)
// return
// }
// defer response.Body.Close()
//
// // Read the response body
// body, err := io.ReadAll(response.Body)
// if err != nil {
// fmt.Printf("Error reading response body for %s: %v\n", searchTerm, err)
// return
// }
//
// // Parse the JSON response
// var searchResult SearchResult
// err = json.Unmarshal(body, &searchResult)
// if err != nil {
// fmt.Printf("Error parsing JSON for %s: %v\n", searchTerm, err)
// return
// }
//
// // Send the products to the channel
// ch <- searchResult.Products
//}
//
//func main() {
// searchTerms := []string{"lapte", "oua", "branza", "peste"} // The search terms you want to use
//
// var wg sync.WaitGroup
// productChannel := make(chan []Product)
//
// // Start Goroutines to search and collect results
// for _, term := range searchTerms {
// wg.Add(1)
// go searchAndCollect(term, productChannel, &wg)
// }
//
// // Wait for all Goroutines to finish
// go func() {
// wg.Wait()
// close(productChannel)
// }()
// var allProducts []Product
// for products := range productChannel {
// allProducts = append(allProducts, products...)
// }
// // Collect and print the products from the channel
// fmt.Println("number of product", len(allProducts))
// //for products := range productChannel {
// // fmt.Printf("Search results, nr %d:\n", len(products))
// // for i, product := range products {
// // fmt.Printf("%d. %s\n", i+1, product.ProductName)
// // }
// //}
//}
//
//{
//"age":18,
//"weight":90,
//"height":180,
//"gender":1,
//"physical_activity":90
//}