-
Notifications
You must be signed in to change notification settings - Fork 0
/
meal.go
149 lines (128 loc) · 3.62 KB
/
meal.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
package neisgo
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"
)
type mealTime struct {
Breakfast string
Lunch string
Dinner string
}
// Meal represents meal of the day
type Meal struct {
Date time.Time
Origin mealTime
Ingredients mealTime
mealTime
}
// IsZero returns true if m is the zero value
func (m Meal) IsZero() bool {
return m == (Meal{})
}
// Text returns merged breakfast, lunch, and dinner
func (m Meal) Text() string {
var text string
if m.Breakfast != "" {
text += "[조식]\n" + m.Breakfast
if m.Lunch != "" || m.Dinner != "" {
text += "\n\n"
}
}
if m.Lunch != "" {
text += "[중식]\n" + m.Lunch
if m.Dinner != "" {
text += "\n\n"
}
}
if m.Dinner != "" {
text += "[석식]\n" + m.Dinner
}
return text
}
type mealSchema struct {
Mealservicedietinfo []struct {
Head []struct {
ListTotalCount int `json:"list_total_count,omitempty"`
Result struct {
Code string `json:"CODE"`
Message string `json:"MESSAGE"`
} `json:"RESULT,omitempty"`
} `json:"head,omitempty"`
Row []struct {
AtptOfcdcScCode string `json:"ATPT_OFCDC_SC_CODE"`
AtptOfcdcScNm string `json:"ATPT_OFCDC_SC_NM"`
SdSchulCode string `json:"SD_SCHUL_CODE"`
SchulNm string `json:"SCHUL_NM"`
MmealScCode string `json:"MMEAL_SC_CODE"`
MmealScNm string `json:"MMEAL_SC_NM"`
MlsvYmd string `json:"MLSV_YMD"`
MlsvFgr float32 `json:"MLSV_FGR"`
DdishNm string `json:"DDISH_NM"`
OrplcInfo string `json:"ORPLC_INFO"`
CalInfo string `json:"CAL_INFO"`
NtrInfo string `json:"NTR_INFO"`
MlsvFromYmd string `json:"MLSV_FROM_YMD"`
MlsvToYmd string `json:"MLSV_TO_YMD"`
} `json:"row,omitempty"`
} `json:"mealServiceDietInfo"`
}
// GetMeal gets meal data from neis,
func (n *Neis) GetMeal(year int, month time.Month) ([]Meal, error) {
start := time.Date(year, month, 1, 0, 0, 0, 0, time.Now().Location())
end := time.Date(year, month+1, 0, 0, 0, 0, 0, time.Now().Location())
duration := int(end.Sub(start).Hours()/24) + 1
q := url.Values{
"KEY": []string{n.apiKey},
"Type": []string{"json"},
"ATPT_OFCDC_SC_CODE": []string{n.region},
"SD_SCHUL_CODE": []string{n.code},
"MLSV_FROM_YMD": []string{start.Format("20060102")},
"MLSV_TO_YMD": []string{end.Format("20060102")},
}
url := fmt.Sprintf("https://open.neis.go.kr/hub/mealServiceDietInfo?%s", q.Encode())
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var data mealSchema
var meals = make([]Meal, duration)
if err := json.Unmarshal(b, &data); err != nil {
return nil, err
}
for _, row := range data.Mealservicedietinfo[1].Row {
d, err := time.Parse("20060102", row.MlsvYmd)
if err != nil {
return nil, err
}
index := end.Day() - d.Day()
if meals[index].IsZero() {
meals[index] = Meal{
Date: d,
}
}
switch row.MmealScCode {
case "1":
meals[index].Breakfast = genPlainText(row.DdishNm)
meals[index].Origin.Breakfast = genPlainText(row.OrplcInfo)
meals[index].Ingredients.Breakfast = genPlainText(row.NtrInfo)
case "2":
meals[index].Lunch = genPlainText(row.DdishNm)
meals[index].Origin.Lunch = genPlainText(row.OrplcInfo)
meals[index].Ingredients.Lunch = genPlainText(row.NtrInfo)
case "3":
meals[index].Dinner = genPlainText(row.DdishNm)
meals[index].Origin.Dinner = genPlainText(row.OrplcInfo)
meals[index].Ingredients.Dinner = genPlainText(row.NtrInfo)
}
}
return meals, nil
}