forked from leegeunhyeok/comcigan-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
279 lines (235 loc) Β· 8.03 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
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
/**
* comcigan-parser Module
*
* index.js
*
* Github : https://github.com/leegeunhyeok/comcigan-parser
* NPM : https://www.npmjs.com/package/comcigan-parser
*
* @description μ»΄μκ° μκ°ν νμ± λΌμ΄λΈλ¬λ¦¬
* @author Leegeunhyeok
* @license MIT
* @version 0.2.0
*
*/
const request = require('request')
const iconv = require('iconv-lite')
const { appendZero } = require('./util/util')
class Timetable {
constructor () {
this._baseUrl = 'http://comci.kr:4081'
this._url = 'http://comci.kr:4081/st'
this._weekdayString = ['μΌ', 'μ', 'ν', 'μ', 'λͺ©', 'κΈ', 'ν ']
this._classTime = []
}
/**
* @description μκ°ν νμλ₯Ό μ΄κΈ°νν©λλ€.
* @param {any} option μ΄κΈ°ν μ΅μ
κ°μ²΄
*/
async init (option) {
if (option) {
this._option = {
firstNames: option.firstNames || ['κΉ', 'λ°', 'μ΄', 'μ‘'],
maxGrade: option.maxGrade || 3,
timetableThreshold: option.timetableThreshold || 30000
}
if (option['tempSave'] !== undefined) {
console.log('[comcigan-parser] WARNING: tempSave option was deprecated at >= 0.1.0')
}
} else {
this._option = {
firstNames: ['κΉ', 'λ°', 'μ΄', 'μ‘'],
maxGrade: 3,
timetableThreshold: 30000
}
}
await new Promise((resolve, reject) => {
request(this._url, (err, _, body) => {
if (err) {
reject(err)
}
const idx = body.indexOf('school_ra(sc)')
const idx2 = body.indexOf('sc_data(\'')
if (idx === -1 || idx2 === -1) {
reject(new Error('μμ€μμ μλ³ μ½λλ₯Ό μ°Ύμ μ μμ΅λλ€.'))
}
const extractSchoolRa = body.substr(idx, 50).replace(' ', '')
const schoolRa = extractSchoolRa.match(/url:'.(.*?)'/)
// sc_data μΈμκ° μΆμΆ
const extractScData = body.substr(idx2, 30).replace(' ', '')
const scData = extractScData.match(/\(.*?\)/)
if (scData) {
this._scData = scData[0].replace(/[()]/g, '').replace(/'/g, '').split(',')
} else {
reject(new Error('sc_data κ°μ μ°Ύμ μ μμ΅λλ€.'))
}
if (schoolRa) {
this._extractCode = schoolRa[1]
} else {
reject(new Error('school_ra κ°μ μ°Ύμ μ μμ΅λλ€.'))
}
resolve()
})
})
this._initialized = true
}
/**
* @description μκ°ν λ°μ΄ν°λ₯Ό λΆλ¬μ¬ νκ΅λ₯Ό μ€μ ν©λλ€.
* @param {string} keyword νκ΅ κ²μ ν€μλ
* @return {Promise<any>}
*/
async setSchool (keyword) {
if (!this._initialized) {
throw new Error('μ΄κΈ°νκ° μ§νλμ§ μμμ΅λλ€.')
}
let hexString = ''
for (let buf of iconv.encode(keyword, 'euc-kr')) {
hexString += '%' + buf.toString(16)
}
await new Promise((resolve, reject) => {
request(this._baseUrl + this._extractCode + hexString, (err, res, body) => {
let jsonString = body.substr(0, body.lastIndexOf('}') + 1)
let searchData = JSON.parse(jsonString)['νκ΅κ²μ']
if (err) {
reject(err)
}
if (searchData.length <= 0) {
reject(new Error('κ²μλ νκ΅κ° μμ΅λλ€.'))
}
if (searchData.length > 1) {
reject(new Error(`κ²μλ νκ΅κ° λ§μ΅λλ€. λ μμΈν νκ΅λͺ
μ μ
λ ₯ν΄μ£ΌμΈμ. κ²μ κ²°κ³Ό μ: ${searchData.length}`))
}
this._searchData = searchData
resolve()
})
})
this._setSchool = true
}
/**
* @description μ€μ ν νκ΅μ μκ°ν λ°μ΄ν°λ₯Ό λΆλ¬μ΅λλ€
* @return {Promise<any>}
*/
async getTimetable () {
if (!this._initialized) {
throw new Error('μ΄κΈ°νκ° μ§νλμ§ μμμ΅λλ€.')
}
if (!this._setSchool) {
throw new Error('νκ΅ μ€μ μ΄ μ§νλμ§ μμμ΅λλ€.')
}
const da1 = '0'
const s7 = this._scData[0] + this._searchData[0][3]
const sc3 = this._extractCode.split('?')[0] + '?' +
Buffer.from(s7 + '_' + da1 + '_' + this._scData[2]).toString('base64')
// JSON λ°μ΄ν° λ‘λ
const resultJson = await new Promise((resolve, reject) => {
request(this._baseUrl + sc3, (err, res, body) => {
if (err) {
reject(err)
}
if (!body) {
reject(new Error('μκ°ν λ°μ΄ν°λ₯Ό μ°Ύμ μ μμ΅λλ€.'))
}
// String to JSON
resolve(JSON.parse(body.substr(0, body.lastIndexOf('}') + 1)))
})
})
// κ΅μλ³ μμ/μ’
λ£ μκ° λ°μ΄ν°
this._classTime = resultJson['μΌκ³Όμκ°']
let subjectProp = ''
let teacherProp = ''
let timedataProp = ''
// JSON λ°μ΄ν°μ νλ‘νΌν° μ‘°ν
for (let k of Object.keys(resultJson)) {
if (typeof resultJson[k] === 'object' && k.indexOf('μλ£') !== -1) {
if (k.indexOf('κΈ΄') !== -1) {
subjectProp = k
} else {
let teacherCount = 0
let teacherFinished = false
let timetableDataFinished = false
for (let d of resultJson[k]) {
for (let firstName of this._option['firstNames']) {
if (d.indexOf(firstName) !== -1) {
teacherCount++
}
}
if (teacherCount >= 10 && !teacherFinished) {
teacherProp = k
teacherFinished = true
}
for (let dd of d) {
if (dd.length === 7 && typeof dd[0] === 'object') {
let total = 0
dd.forEach(data => {
total += data.reduce((v, acc) => acc + v, 0)
})
if (total > this._option['timetableThreshold']) {
timedataProp = k
break
}
}
}
if (teacherFinished && timetableDataFinished) {
break
}
}
}
}
}
const classCount = resultJson['νκΈμ']
const teachers = resultJson[teacherProp]
const subjects = resultJson[subjectProp]
const data = resultJson[timedataProp]
const time = resultJson['μμΌλ³μμ']
// μ μ₯ λ°μ΄ν° 리μ€νΈ
let timetableData = {}
// 1νλ
~ maxGrade νλ
κ΅μ€ λ°λ³΅
for (let grade = 1; grade <= this._option['maxGrade']; grade++) {
// νλ
λ³ λ° μ λ§νΌ λ°λ³΅
for (let classNum = 1; classNum <= classCount[grade]; classNum++) {
const tempData = data[grade][classNum]
// μ(1) ~ κΈ(5)
for (let weekday = 1; weekday <= 5; weekday++) {
// 1κ΅μ ~ ν΄λΉ λ μ§μ μμ
κ΅μ
for (let classTime = 1; classTime <= time[grade][weekday]; classTime++) {
const code = tempData[weekday][classTime].toString()
var teacherCode = 0
var subjectCode = 0
if (code.length === 3) {
teacherCode = parseInt(appendZero(code.substr(0, 1), 2))
subjectCode = parseInt(appendZero(code.substr(1, 2), 2))
} else {
teacherCode = parseInt(appendZero(code.substr(0, 2), 2))
subjectCode = parseInt(appendZero(code.substr(2, 2), 2))
}
if (!(grade in timetableData)) {
timetableData[grade] = {}
}
if (!(classNum in timetableData[grade])) {
timetableData[grade][classNum] = Array.apply(null, new Array(5)).map(() => [])
}
// μκ°ν λ°μ΄ν° μΆκ°
timetableData[grade][classNum][weekday - 1].push({
grade,
class: classNum,
weekday,
weekdayString: this._weekdayString[weekday],
class_time: classTime,
code,
teacher: teachers[teacherCode],
subject: subjects[subjectCode].replace(/_/g, '')
})
}
}
}
}
return timetableData
}
/**
* @description κ° μμ
κ΅μλ³ μμ/μ’
λ£μκ° λ°μ΄ν°
*/
getClassTime () {
return this._classTime
}
}
module.exports = Timetable