-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
161 lines (138 loc) · 3.39 KB
/
main.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
package main
import (
"crypto/sha1"
"encoding/csv"
"encoding/hex"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
)
const (
ColumnName = 0
ColumnUsualMeasure = 1
ColumnWeight = 2
ColumnCarbs = 3
ColumnKiloCalories = 4
ColumnKiloJoules = 5
ColumnCategorie = 6
ColumnSubCategorie = 7
ColumnParty = 8
)
type FoodEntry struct {
Name string
UsualMeasure string
Weight float64
Unit string
Carbs float64
KiloCalories float64
KiloJoules float64
Categorie string
SubCategorie string
Party string
}
func NewFoodEntry(record []string) (*FoodEntry, error) {
if len(record) != 9 {
return nil, fmt.Errorf("invalid record, 9 columns required, %d provided", len(record))
}
carbs, err := strconv.ParseFloat(strings.ReplaceAll(record[ColumnCarbs], ",", "."), 64)
if err != nil {
return nil, err
}
var weight float64
if record[ColumnWeight] == "" {
weight = 0
} else {
weight, err = strconv.ParseFloat(strings.ReplaceAll(record[ColumnWeight], ",", "."), 64)
if err != nil {
return nil, err
}
}
kcal, err := strconv.ParseFloat(strings.ReplaceAll(record[ColumnKiloCalories], ",", "."), 64)
if err != nil {
return nil, err
}
kj, err := strconv.ParseFloat(strings.ReplaceAll(record[ColumnKiloJoules], ",", "."), 64)
if err != nil {
return nil, err
}
unit := "g"
if record[ColumnWeight] == "" || record[ColumnWeight] == "0" {
unit = "pcs"
}
return &FoodEntry{
Name: record[ColumnName],
UsualMeasure: record[ColumnUsualMeasure],
Weight: weight,
Unit: unit,
Carbs: carbs,
KiloCalories: kcal,
KiloJoules: kj,
Categorie: record[ColumnCategorie],
SubCategorie: record[ColumnSubCategorie],
}, nil
}
func main() {
// open file
f, err := os.Open("data.csv")
if err != nil {
log.Fatal(err)
}
// remember to close the file at the end of the program
defer f.Close()
// read csv values using csv.Reader
csvReader := csv.NewReader(f)
for {
rec, err := csvReader.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
line, _ := csvReader.FieldPos(0)
// skip headers
if line == 1 {
continue
}
food, err := NewFoodEntry(rec)
if err != nil {
log.Fatal(err)
}
form := url.Values{}
form.Add("type", "food")
form.Add("category", food.Categorie)
form.Add("subcategory", food.SubCategorie)
form.Add("name", food.Name)
form.Add("portion", fmt.Sprintf("%.2f", food.Weight))
form.Add("carbs", fmt.Sprintf("%.2f", food.Carbs))
// form.Add("fat", "0")
// form.Add("protein", "0")
form.Add("unit", food.Unit)
form.Add("energy", fmt.Sprintf("%.2f", food.KiloJoules))
req, err := http.NewRequest("POST", fmt.Sprintf("%s/api/v1/food/", os.Getenv("NS_BASE_URL")), strings.NewReader(form.Encode()))
if err != nil {
log.Fatal(err)
}
hasher := sha1.New()
hasher.Write([]byte(os.Getenv("NS_API_SECRET")))
apiSecret := hex.EncodeToString(hasher.Sum(nil))
req.Header.Add("Api-Secret", apiSecret)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
res, _ := http.DefaultClient.Do(req)
if res.StatusCode == http.StatusOK {
log.Default().Println("Inserted record ", line)
continue
} else {
body, _ := io.ReadAll(res.Body)
log.Default().Println("StatusCode: ", res.Status)
log.Default().Println(string(body))
}
time.Sleep(200 * time.Millisecond)
}
}