-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
105 lines (87 loc) · 2.75 KB
/
index.js
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
const cheerio = require('cheerio')
const request = require('request')
const syncRequest = require('sync-request');
const prototype = require('./prototype')
const url = 'https://{0}/sts_sci_md00_001.do?schulCode={1}&schulCrseScCode={2}&schulKndScScore=0{3}&schYm={4}{5}'
const timings = ['조식', '중식', '석식']
class SchoolAPI {
constructor (region, schoolCode, type) {
this.region = region
this.schoolCode = schoolCode
if(!type) {
this.type = this.constructor.Type.HIGH
}
}
getFormattedURL(year, month) {
return url.format(this.region, this.schoolCode, this.type, this.type, year, month.pad())
}
getMonthlyMenus(year, month) {
let menus;
let resp = syncRequest('GET', this.getFormattedURL(year, month))
let $ = cheerio.load(resp.getBody('utf-8'))
return this.getMenusFromCheerio($)
}
getMonthlyMenusAsync(year, month) {
return new Promise ((resolve, reject) => {
request.get(this.getFormattedURL(year, month), (error, resp, body) => {
if (error) {
return reject(error)
}
let $ = cheerio.load(body)
return resolve(this.getMenusFromCheerio($))
})
})
}
getMenusFromCheerio($) {
let menus = {}
$('.tbl_type3.tbl_calendar td').filter((i, cell) => $(cell).text() !== ' ').each((i, cell) => {
let menu = this.getMenuFromData($(cell).text())
menus[i+1] = {
'breakfast': menu[0], 'lunch': menu[1], 'dinner': menu[2]
}
})
return menus
}
getMenuFromData(data) {
let menu = {}
let timing = -1
let matches = data.match(/[가-힣]+\([가-힣]+\)|[가-힣]+/g);
if (matches) {
matches.forEach(text => {
if (text.match(/[조중석]식/)) {
timing = timings.indexOf(text)
menu[timing] = []
} else {
menu[timing].push(text)
}
})
}
return menu
}
}
SchoolAPI.Region = {
SEOUL: 'stu.sen.go.kr',
BUSAN: 'stu.pen.go.kr',
DAEGU: 'stu.dge.go.kr',
INCHEON: 'stu.ice.go.kr',
GWANGJU: 'stu.gen.go.kr',
DAEJEON: 'stu.dje.go.kr',
ULSAN: 'stu.use.go.kr',
SEJONG: 'stu.sje.go.kr',
GYEONGGI: 'stu.cbe.go.kr',
KANGWON: 'stu.kwe.go.kr',
CHUNGBUK: 'stu.cbe.go.kr',
CHUNGNAM: 'stu.cne.go.kr',
JEONBUK: 'stu.jbe.go.kr',
JEONNAM: 'stu.jne.go.kr',
GYEONGBUK: 'stu.gbe.go.kr',
GYEONGNAM: 'stu.gne.go.kr',
JEJU: 'stu.jje.go.kr'
}
SchoolAPI.Type = {
KINDERGARTEN: 1,
ELEMENTARY: 2,
MIDDLE: 3,
HIGH: 4
}
module.exports = SchoolAPI