-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
361 lines (309 loc) Β· 9.57 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/**
* 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
*/
const request = require('request');
const iconv = require('iconv-lite');
const cheerio = require('cheerio');
// URL is not defined in old node.js
if (typeof URL === 'undefined') {
URL = require('url').URL;
}
const HOST = 'http://μ»΄μκ°νμ.kr';
class Timetable {
constructor() {
this._baseUrl = null;
this._url = null;
this._initialized = false;
this._pageSource = null;
this._cache = null;
this._cacheAt = null;
this._schoolCode = -1;
this._weekdayString = ['μΌ', 'μ', 'ν', 'μ', 'λͺ©', 'κΈ', 'ν '];
this._option = {
maxGrade: 3,
cache: 0,
};
}
/**
* μκ°ν νμλ₯Ό μ΄κΈ°νν©λλ€.
*
* @param option μ΄κΈ°ν μ΅μ
κ°μ²΄
*/
async init(option) {
if (option) {
this._option = Object.assign(this._option, option);
}
await new Promise((resolve, reject) => {
request(HOST, (err, _res, body) => {
if (err) {
reject(err);
}
const frame = body
.toLowerCase()
.replace(/\'/g, '"')
.match(/<frame [^>]*src="[^"]*"[^>]*>/gm);
if (!frame) {
reject(new Error('frameμ μ°Ύμ μ μμ΅λλ€'));
return;
}
const uri = frame[0].match(/\".*\"/gi);
if (!uri) {
reject(new Error('μ κ·Ό μ£Όμλ₯Ό μ°Ύμ μ μμ΅λλ€'));
return;
}
const frameHref = uri[0].replace(/\"/g, '');
const url = new URL(frameHref);
this._url = frameHref;
this._baseUrl = url.origin;
resolve();
});
});
await new Promise((resolve, reject) => {
request(
{
url: this._url,
encoding: null,
},
(err, _res, body) => {
if (err) {
reject(err);
}
const source = iconv.decode(body, 'EUC-KR');
const idx = source.indexOf('school_ra(sc)');
const idx2 = source.indexOf("sc_data('");
if (idx === -1 || idx2 === -1) {
reject(new Error('μμ€μμ μλ³ μ½λλ₯Ό μ°Ύμ μ μμ΅λλ€.'));
return;
}
const extractSchoolRa = source.substr(idx, 50).replace(' ', '');
const schoolRa = extractSchoolRa.match(/url:'.(.*?)'/);
// sc_data μΈμκ° μΆμΆ
const extractScData = source.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 κ°μ μ°Ύμ μ μμ΅λλ€.'));
return;
}
if (schoolRa) {
this._extractCode = schoolRa[1];
} else {
reject(new Error('school_ra κ°μ μ°Ύμ μ μμ΅λλ€.'));
return;
}
this._pageSource = source;
resolve();
},
);
});
this._initialized = true;
}
/**
* μκ°ν λ°μ΄ν°λ₯Ό λΆλ¬μ¬ νκ΅λ₯Ό μ€μ ν©λλ€.
*
* @param {string} keyword νκ΅ κ²μ ν€μλ
* @returns κ²μλ νκ΅ λͺ©λ‘ `Array<[μ½λ, μ§μ, νκ΅μ΄λ¦, νκ΅μ½λ]>`
*/
search(keyword) {
if (!this._initialized) {
throw new Error('μ΄κΈ°νκ° μ§νλμ§ μμμ΅λλ€.');
}
let hexString = '';
for (let buf of iconv.encode(keyword, 'euc-kr')) {
hexString += '%' + buf.toString(16);
}
return 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('κ²μλ νκ΅κ° μμ΅λλ€.'));
}
resolve(
searchData.map((data) => {
return {
_: data[0],
region: data[1],
name: data[2],
code: data[3],
};
}),
);
});
});
}
/**
* μκ°νλ₯Ό μ‘°νν νκ΅ μ½λλ₯Ό λ±λ‘ν©λλ€
*
* @param school
*/
setSchool(schoolCode) {
this._schoolCode = schoolCode;
this._cache = null;
}
/**
* μ€μ ν νκ΅μ μ κ΅ μκ°ν λ°μ΄ν°λ₯Ό λΆλ¬μ΅λλ€
*
* @return μκ°ν λ°μ΄ν°
*/
async getTimetable() {
this._isReady();
// μΊμ μ§μμκ°μ΄ μ‘΄μ¬νκ³ , μμ§ λ§λ£λμ§ μμλ€λ©΄ κΈ°μ‘΄ κ° μ λ¬
// λ§λ£λμκ±°λ, μΊμκ° λΉνμ±ν(κΈ°λ³Έκ°)λμ΄μλ κ²½μ°μ νμ μλ‘μ΄ κ° νμ±νμ¬ μ λ¬
if (this._option.cache && !this._isCacheExpired()) {
return this._cache;
}
const jsonString = await this._getData();
const resultJson = JSON.parse(jsonString);
const startTag = this._pageSource.match(/<script language(.*?)>/gm)[0];
const regex = new RegExp(startTag + '(.*?)</script>', 'gi');
let match;
let script = '';
// μ»΄μκ° μΉ νμ΄μ§ JS μ½λ μΆμΆ
while ((match = regex.exec(this._pageSource))) {
script += match[1];
}
// λ°μ΄ν° μ²λ¦¬ ν¨μλͺ
μΆμΆ
const functioName = script
.match(/function μλ£[^\(]*/gm)[0]
.replace(/\+s/, '')
.replace('function', '');
// νλ
λ³ μ 체 νκΈ μ
const classCount = resultJson['νκΈμ'];
// μκ°ν λ°μ΄ν° κ°μ²΄
const timetableData = {};
// 1νλ
~ maxGrade νλ
κ΅μ€ λ°λ³΅
for (let grade = 1; grade <= this._option['maxGrade']; grade++) {
if (!timetableData[grade]) {
timetableData[grade] = {};
}
// νλ
λ³ λ° μ λ§νΌ λ°λ³΅
for (let classNum = 1; classNum <= classCount[grade]; classNum++) {
if (!timetableData[grade][classNum]) {
timetableData[grade][classNum] = {};
}
timetableData[grade][classNum] = this._getClassTimetable(
{ data: jsonString, script, functioName },
grade,
classNum,
);
}
}
this._cache = timetableData;
this._cacheAt = +new Date();
return timetableData;
}
/**
* κ΅μλ³ μμ
μκ° μ 보λ₯Ό μ‘°νν©λλ€.
* @returns
*/
async getClassTime() {
this._isReady();
// κ΅μλ³ μμ/μ’
λ£ μκ° λ°μ΄ν°
return JSON.parse(await this._getData())['μΌκ³Όμκ°'];
}
/**
* μ»΄μκ°μ APIλ₯Ό ν΅ν΄ μ 체 μκ°ν λ°μ΄ν°λ₯Ό μμ§/νμ±νμ¬ λ°νν©λλ€.
*/
async _getData() {
const da1 = '0';
const s7 = this._scData[0] + this._schoolCode;
const sc3 =
this._extractCode.split('?')[0] +
'?' +
Buffer.from(s7 + '_' + da1 + '_' + this._scData[2]).toString('base64');
// JSON λ°μ΄ν° λ‘λ
const jsonString = 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(body.substr(0, body.lastIndexOf('}') + 1));
});
});
return jsonString;
}
/**
* μ§μ λ νλ
/λ°μ 1μ£ΌμΌ μκ°νλ₯Ό νμ±ν©λλ€
*
* @param codeConfig λ°μ΄ν°, ν¨μλͺ
, μμ€μ½λ κ°μ²΄
* @param grade νλ
* @param classNumber λ°
* @returns
*/
_getClassTimetable(codeConfig, grade, classNumber) {
const args = [codeConfig.data, grade, classNumber];
const call = codeConfig.functioName + '(' + args.join(',') + ')';
const script = codeConfig.script + '\n\n' + call;
/** DEAD: Sorry about using eval() **/
const res = eval(script);
// Table HTML script
const $ = cheerio.load(res);
const $this = this;
const timetable = [];
$('tr').each(function (timeIdx) {
const currentTime = timeIdx - 2;
// 1, 2λ²μ§Έ trμ μ λͺ© μμμ΄λ―λ‘ μ€ν΅
if (timeIdx <= 1) return;
$(this)
.find('td')
.each(function (weekDayIdx) {
const currentWeekDay = weekDayIdx - 1;
// μ²μ(μ λͺ©)κ³Ό λ(ν μμΌ) μμμ μ€ν΅
if (weekDayIdx === 0 || weekDayIdx === 6) return;
if (!timetable[currentWeekDay]) {
timetable[currentWeekDay] = [];
}
const subject = $(this).contents().first().text();
const teacher = $(this).contents().last().text();
timetable[currentWeekDay][currentTime] = {
grade,
class: classNumber,
weekday: weekDayIdx - 1,
weekdayString: $this._weekdayString[weekDayIdx],
classTime: currentTime + 1,
teacher,
subject,
};
});
});
return timetable;
}
/**
* μ΄κΈ°ν λ° νκ΅ μ€μ μ΄ λͺ¨λ μ€λΉλμλμ§ νμΈν©λλ€.
*/
_isReady() {
if (!this._initialized) {
throw new Error('μ΄κΈ°νκ° μ§νλμ§ μμμ΅λλ€.');
}
if (this._schoolCode === -1) {
throw new Error('νκ΅ μ€μ μ΄ μ§νλμ§ μμμ΅λλ€.');
}
}
/**
* μ¬μ©μκ° μΈν
ν μΊμ μ§μ μκ°μ νμΈνμ¬ λ§λ£ μ¬λΆλ₯Ό λ°νν©λλ€.
*
* @returns μΊμ λ§λ£ μ¬λΆ
*/
_isCacheExpired() {
return +new Date() - this._cacheAt >= this._option.cache;
}
}
module.exports = Timetable;