-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinu_restaurant.js
401 lines (350 loc) · 17.1 KB
/
inu_restaurant.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* copyright(c) 2016 All rights reserved by seon-il kim(supercartoon@naver.com) 201101720 정보통신공학과 김선일
* 수정하지 마세요!
*/
var mysql = require('mysql');
var cheerio = require('cheerio');
var request = require('request');
var schedule = require('node-schedule');
console.log("<<< INU 생협 식단 파싱 node.js / by Seon-Il Kim (supercartoon@naver.com) >>>");
console.log("이 작업은 매주 일요일, 월요일 0시 1분에 실행됩니다.");
var j = schedule.scheduleJob("1 0 * * 0,1" , function(){
console.log(new Date());
insertDB();
});
var url = 'https://www.uicoop.ac.kr:41052/shop/shop.php?w=3';
var pool = mysql.createPool(
require('./config.js').db
);
function convertHTML(menu) {
menu = menu.replace(/(^\s*)|(\s*$)/g, "");
menu = menu.replace(/<\/td>/gi, '');
menu = menu.replace(/<td class="menu-list" valign="top">/gi, '');
menu = menu.replace(/<td class="menu-list-corn">/gi, '');
menu = menu.replace(/ /gi, '');
menu = menu.replace(/<\/span>/gi, '');
menu = menu.replace(/<span style="font-weight:bold;color:#0B555B;font-size:1.1em;">/gi, '');
menu = menu.replace(/<span style="font-weight:bold;color:#0B555B;">/gi, '');
menu = menu.replace(/\r/gi, ' ');
menu = menu.replace(/\n/gi, ' ');
menu = menu.replace(/ <br> /gi, ' ');
menu = menu.replace(/<br> /gi, ' ');
menu = menu.replace(/<br>/gi, ' ');
menu = menu.replace(/ /gi, ' ');
menu = menu.replace(/ /gi, ' ');
menu = menu.replace(/ /gi, ' ');
menu = menu.replace(/(^\s*)|(\s*$)/g, "");
return menu;
}
function splitHTML(menu) {
var menuarr = menu.split('<hr style="border:1px dotted #d4d4b8;">');
for (var i = 0; i < menuarr.length; i++) {
menuarr[i] = menuarr[i].replace(/(^\s*)|(\s*$)/g, "");
}
return menuarr;
}
////////////////////////////////////////////////////////////////////////////////
function information() {
console.log("<<< INU restaurant parse node.js / by Seon-Il Kim (supercartoon@naver.com) >>>");
console.log("이 작업은 매일 새벽 2시에 실행됩니다.");
}
function makeObject(callback) {
var object = new Object();
//복지회관
object.menu11 = new Array(); //복지회관 1코너. 2개
object.menu12 = new Array(); //복지회관 2코너. 2개
object.menu13 = new Array(); //복지회관 3코너
object.menu14 = new Array(); //복지회관 4코너
object.menu15 = new Array(); //복지회관 5코너
//카페테리아
object.menu21 = new Array(); //카페테리아 A코너 중식
object.menu22 = new Array(); //카페테리아 A코너 석식. 2개
object.menu23 = new Array(); //카페테리아 B코너
// object.menu24 = new Array();
//사범대
object.menu31 = new Array(); //사범대 중식. 3개
object.menu32 = new Array(); //사범대 석식
//기숙사
object.menu41 = new Array(); //조식
object.menu42 = new Array(); //중식
object.menu43 = new Array(); //석식
//교직원
object.menu51 = new Array(); //중식. 2개
object.menu52 = new Array(); //석식
if (typeof callback === 'function') {
callback(object);
}
}
function getData(callback) {
makeObject(function(object) {
request(url, {
rejectUnauthorized: false
}, function(err, response, html) {
//사범대 식당
if (err) {
console.error(err);
throw err;
}
var $ = cheerio.load(html, {
decodeEntities: false
});
var date = $("#main_background > div.layout > div.layout_A > div.sub_wrap > div.smid > div.detail_left > div > div > span");
// #main_background > div.layout > div.layout_A > div.sub_wrap > div.smid > div.detail_left > div > table > tbody > tr:nth-child(3) > td:nth-child(3)
var all_menu = $("#main_background > div.layout > div.layout_A > div.sub_wrap > div.smid > div.detail_left > div > table");
// #main_background > div.layout > div.layout_A > div.sub_wrap > div.smid > div.detail_left > div > div > span
var date_new = $("#main_background > div.layout > div.layout_A > div.sub_wrap > div.smid > div.detail_left > div > div > span").text();
date_new = date_new.split('(');
date_new = date_new[1].split('~');
date_new = date_new[0].replace(/(^\s*)|(\s*$)/g, "");
console.log("이번주 시작일 : "+date_new);
// var date2 = $(all_menu).find("tr:nth-child(3) > td:nth-child(2)").text();
// date2 = date2.replace("월", "").replace(/(^\s*)|(\s*$)/g, "").replace("월 ", "-").replace("일", "");
// date2 = new Date().getFullYear()+"-"+date2;
// console.log(date2);
// var datearr = date2.split(' ');
// datearr[0] = +datearr[0];
// datearr[1] = +datearr[1];
// console.log(datearr[0]);
// console.log(datearr[1]);
object.start_date = new Date(date_new);
// object.start_date = new Date(new Date().getFullYear(), datearr[0]-1, datearr[1]+1);
// object.start_date = new Date(2017, 1, 9);
for (var i = 0; i < 7; i++) {
//0 : 월요일 ~ 7 : 일요일
object.menu11[i] = convertHTML($(all_menu).find("tr:nth-child(5) > td:nth-child(" + (i + 3) + ")") + "");
// object.menu11[i] = splitHTML(object.menu11[i]);
// console.log(object.menu11[i]);
//menu11[i][j] : 복지회관 1코너 i번째 요일(j+1)번째 메뉴
object.menu12[i] = convertHTML($(all_menu).find("tr:nth-child(6) > td:nth-child(" + (i + 2) + ")") + "");
// object.menu12[i] = splitHTML(object.menu12[i]);
//menu12[i][j] : 복지회관 2코너 i번째 요일(j+1)번째 메뉴
object.menu13[i] = convertHTML($(all_menu).find("tr:nth-child(7) > td:nth-child(" + (i + 2) + ")") + "");
//menu13[i] : 복지회관 3코너 i번째 요일 메뉴
object.menu14[i] = convertHTML($(all_menu).find("tr:nth-child(8) > td:nth-child(" + (i + 3) + ")") + "");
//menu14[i] : 복지회관 3코너 i번째 요일 메뉴
object.menu15[i] = convertHTML($(all_menu).find("tr:nth-child(9) > td:nth-child(" + (i + 2) + ")") + "");
//menu15[i] : 복지회관 3코너 i번째 요일 메뉴
// #main_background > div.layout > div.layout_A > div.sub_wrap > div.smid > div.detail_left > div > table > tbody > tr:nth-child(9) > td:nth-child(3)
object.menu21[i] = convertHTML($(all_menu).find("tr:nth-child(11) > td:nth-child(" + (i + 3) + ")") + "");
//menu21[i] : 카페테리아 A코너 중식 i번째 요일 메뉴
// console.log(menu21[i]);
object.menu22[i] = convertHTML($(all_menu).find("tr:nth-child(12) > td:nth-child(" + (i + 2) + ")") + "");
// object.menu22[i] = splitHTML(object.menu22[i]);
//menu22[i][j] : 카페테리아 A코너 석식 i번째 요일 (j+1)번째 메뉴
// for(var j=0;j<menu22[i].length;j++){
// console.log(menu22[i][j]);
// }
object.menu23[i] = convertHTML($(all_menu).find("tr:nth-child(13) > td:nth-child(" + (i + 2) + ")") + "");
//menu23[i] : 카페테리아 B코너 i번째 요일 메뉴
// console.log(i+" "+object.menu23[i]);
object.menu31[i] = convertHTML($(all_menu).find("tr:nth-child(15) > td:nth-child(" + (i + 2) + ")") + "");
// object.menu31[i] = splitHTML(object.menu31[i]);
//menu31[i][j] : 사범대 중식 i번째 요일 (j+1)번째 메뉴
// for(var j=0;j<menu31[i].length;j++){
// console.log(menu31[i][j]);
// }
object.menu32[i] = convertHTML($(all_menu).find("tr:nth-child(16) > td:nth-child(" + (i + 2) + ")") + "");
//menu32[i] : 사범대 석식 i번째 요일 메뉴
// console.log(menu32[i]);
object.menu41[i] = convertHTML($(all_menu).find("tr:nth-child(18) > td:nth-child(" + (i + 2) + ")") + "");
// console.log(menu41[i]);
object.menu42[i] = convertHTML($(all_menu).find("tr:nth-child(19) > td:nth-child(" + (i + 2) + ")") + "");
// console.log(menu42[i]);
object.menu43[i] = convertHTML($(all_menu).find("tr:nth-child(20) > td:nth-child(" + (i + 2) + ")") + "");
// console.log(menu43[i]);
//menu4x[i] : 기숙사 i번째 요일 메뉴
object.menu51[i] = convertHTML($(all_menu).find("tr:nth-child(22) > td:nth-child(" + (i + 2) + ")") + "");
// object.menu51[i] = splitHTML(object.menu51[i]);
//menu51[i][j] : 교직원 중식 i번째 요일 (j+1)번째 메뉴
// for(var j=0;j<menu51[i].length;j++){
// console.log(menu51[i][j]);
// }
object.menu52[i] = convertHTML($(all_menu).find("tr:nth-child(23) > td:nth-child(" + (i + 2) + ")") + "");
//menu52[i] : 교직원 석식 i번째 요일 메뉴
// console.log(menu52[i]);
}
if (typeof callback === 'function') {
callback(object);
}
});
});
}
function insertDB(callback) {
getData(function(object) {
// var date = object.start_date;
// for(var i=0;i<7;i++){
// // var cd = date.getDate();
// // if(i!=0) date.setDate(cd+1);
// for(var j=0;j<2;j++){
// insertQuery(i, j, object);
// }
// }
// insertSQL(0, object.start_date, object.menu11, function() {
// // callback(null, web_data.length);
// console.log('complete');
// });
// console.log(object.menu11);
var date1 = new Date(object.start_date);
insertSQL(0, date1, '복지회관', '1코너', 0, object.menu11, 1, function() {
// callback(null, web_data.length);
console.log('complete menu11-0');
});
var date2 = new Date(object.start_date);//date 복사
insertSQL(0, date2, '복지회관', '1코너', 1, object.menu11, 2, function() {
// callback(null, web_data.length);
console.log('complete menu11-1');
});
var date02 = new Date(object.start_date);//date 복사
insertSQL(0, date02, '복지회관', '1코너', 2, object.menu11, 3, function() {
// callback(null, web_data.length);
console.log('complete menu11-2');
});
insertSQL(0, new Date(object.start_date), '복지회관', '1코너', 3, object.menu11, 4, function() {
// callback(null, web_data.length);
console.log('complete menu11-3');
});
var date3 = new Date(object.start_date);//date 복사
insertSQL(0, date3, '복지회관', '2코너', 0, object.menu12, 4, function() {
// callback(null, web_data.length);
console.log('complete menu12-0');
});
var date4 = new Date(object.start_date);//date 복사
insertSQL(0, date4, '복지회관', '2코너', 1, object.menu12, 5, function() {
// callback(null, web_data.length);
console.log('complete menu12-1');
});
var date5 = new Date(object.start_date);//date 복사
insertSQL(0, date5, '복지회관', '3코너', 0, object.menu13, 6, function() {
// callback(null, web_data.length);
console.log('complete menu13-0');
});
var date6 = new Date(object.start_date);//date 복사
insertSQL(0, date6, '복지회관', '4코너', 0, object.menu14, 7, function() {
// callback(null, web_data.length);
console.log('complete menu14-0');
});
var date7 = new Date(object.start_date);//date 복사
insertSQL(0, date7, '복지회관', '5코너', 0, object.menu15, 8, function() {
// callback(null, web_data.length);
console.log('complete menu15-0');
});
var date8 = new Date(object.start_date);//date 복사
insertSQL(0, date8, '카페테리아', 'A코너_중식', 0, object.menu21, 1, function() {
// callback(null, web_data.length);
console.log('complete menu21-0');
});
var date9 = new Date(object.start_date);//date 복사
insertSQL(0, date9, '카페테리아', 'A코너_석식', 0, object.menu22, 2, function() {
// callback(null, web_data.length);
console.log('complete menu22-0');
});
var date10 = new Date(object.start_date);//date 복사
insertSQL(0, date10, '카페테리아', 'A코너_석식', 1, object.menu22, 3, function() {
// callback(null, web_data.length);
console.log('complete menu22-1');
});
var date11 = new Date(object.start_date);//date 복사
insertSQL(0, date11, '카페테리아', 'B코너', 0, object.menu23, 4, function() {
// callback(null, web_data.length);
console.log('complete menu23-0');
});
insertSQL(0, new Date(object.start_date), '카페테리아', 'B코너', 1, object.menu23, 5, function() {
// callback(null, web_data.length);
console.log('complete menu23-1');
});
insertSQL(0, new Date(object.start_date), '사범대식당', '중식', 0, object.menu31, 1, function() {
// callback(null, web_data.length);
console.log('complete menu31-0');
});
insertSQL(0, new Date(object.start_date), '사범대식당', '중식', 1, object.menu31, 2, function() {
// callback(null, web_data.length);
console.log('complete menu31-1');
});
insertSQL(0, new Date(object.start_date), '사범대식당', '중식', 2, object.menu31, 3, function() {
// callback(null, web_data.length);
console.log('complete menu31-2');
});
insertSQL(0, new Date(object.start_date), '사범대식당', '석식', 0, object.menu32, 4, function() {
// callback(null, web_data.length);
console.log('complete menu32-0');
});
insertSQL(0, new Date(object.start_date), '생활관', '조식', 0, object.menu41, 1, function() {
// callback(null, web_data.length);
console.log('complete menu41-0');
});
insertSQL(0, new Date(object.start_date), '생활관', '중식', 0, object.menu42, 2, function() {
// callback(null, web_data.length);
console.log('complete menu42-0');
});
insertSQL(0, new Date(object.start_date), '생활관', '석식', 0, object.menu43, 3, function() {
// callback(null, web_data.length);
console.log('complete menu43-0');
});
insertSQL(0, new Date(object.start_date), '교직원', '중식', 0, object.menu51, 1, function() {
// callback(null, web_data.length);
console.log('complete menu51-0');
});
insertSQL(0, new Date(object.start_date), '교직원', '중식', 1, object.menu51, 2, function() {
// callback(null, web_data.length);
console.log('complete menu51-1');
});
insertSQL(0, new Date(object.start_date), '교직원', '중식', 2, object.menu51, 3, function() {
// callback(null, web_data.length);
console.log('complete menu51-2');
});
insertSQL(0, new Date(object.start_date), '교직원', '석식', 0, object.menu52, 4, function() {
// callback(null, web_data.length);
console.log('complete menu52-0');
});
})
}
insertSQL = function(i, sdate, name, corner, menu_num, menu, order, callback){
setTimeout(function() {
if(i==7){
callback();
} else {
pool.getConnection(function(err, connection){
if(err){
console.error(err);
throw err;
} else {
var date = sdate;
var cd = sdate.getDate();
if(i!=0) date.setDate(cd+1);
// console.log(date);
// console.log(menu[i][menu_num]);
var insert_menu = menu[i];
if(insert_menu.indexOf('<hr style="border:1px dotted #d4d4b8;">')!=-1){
//포함하고 있으면
var arr = insert_menu.split('<hr style="border:1px dotted #d4d4b8;">');
// if(menu_num==0){
// insert_menu=arr[0];
// }
// else if(menu_num==1){
// insert_menu=arr[1];
// }
// else if(menu_num==2){
// insert_menu=arr[2];
// }
insert_menu = arr[menu_num];
} else {
if(menu_num==1 || menu_num==2){
insert_menu="";
}
}
// console.log(insert_menu);
connection.query('insert into restaurant values(?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE menu=?', [date, name, corner, order, menu_num, insert_menu, insert_menu], function(err, rows){
if(err){
console.error(err);
// console.log('insert failed');
} else {
// console.log(web_data[i].name+"is inserted");
// console.log('insert success');
}
});
}
connection.release();
});
insertSQL(i+1, sdate, name, corner, menu_num, menu, order, callback);
}
}, 10);
}