-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind.go
63 lines (49 loc) · 1.38 KB
/
find.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
package schoolmeal
import (
"io/ioutil"
"net/http"
"strconv"
"errors"
"github.com/buger/jsonparser"
)
// Find 함수는 학교를 찾아 School을 리턴합니다. 여러 개의 학교가 찾아질 경우 첫번째 학교를 사용합니다.
func Find(zone, schoolName string) (school School, err error) {
link := "https://par." + zone + ".go.kr/spr_ccm_cm01_100.do?kraOrgNm=" + schoolName
req, err := http.NewRequest("GET", link, nil)
if err != nil {
return
}
resp, err := client.Do(req)
if err != nil {
return
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
code, err := jsonparser.GetString(data, "resultSVO", "orgDVOList", "[0]", "orgCode")
if err != nil {
err = errors.New("schoolmeal: Error while finding school code")
return
}
kindString, err := jsonparser.GetString(data, "resultSVO", "orgDVOList", "[0]", "schulCrseScCode")
if err != nil {
err = errors.New("schoolmeal: Error while finding school kind")
return
}
kind, err := strconv.Atoi(kindString)
if err != nil {
err = errors.New("schoolmeal: Error while converting kind to int from string")
return
}
name, err := jsonparser.GetString(data, "resultSVO", "orgDVOList", "[0]", "kraOrgNm")
if err != nil {
err = errors.New("schoolmeal: Error while finding school name")
return
}
school.Code = code
school.Kind = kind
school.Name = name
school.Zone = zone
return
}