박지환

Get recent data automatically

...@@ -32,13 +32,11 @@ app.set("view engine", "ejs"); ...@@ -32,13 +32,11 @@ app.set("view engine", "ejs");
32 // To get today date by using Date 32 // To get today date by using Date
33 function getTodayDate() { 33 function getTodayDate() {
34 var today = new Date(); 34 var today = new Date();
35 -
36 var year = today.getFullYear(); 35 var year = today.getFullYear();
37 var month = ("0" + (today.getMonth() + 1)).slice(-2); 36 var month = ("0" + (today.getMonth() + 1)).slice(-2);
38 var day = ("0" + today.getDate()).slice(-2); 37 var day = ("0" + today.getDate()).slice(-2);
39 -
40 var dateString = year + "-" + month + "-" + day; 38 var dateString = year + "-" + month + "-" + day;
41 - 39 + // return "2022-10-04"; // 테스트용 날짜를 입력하세요. 테스트가 끝나면 주석처리 하세요. ex) 2022-09-12, 2022-09-15, 2022-10-04
42 return dateString; 40 return dateString;
43 } 41 }
44 42
...@@ -54,15 +52,15 @@ function getModifiedDate(locdate) { ...@@ -54,15 +52,15 @@ function getModifiedDate(locdate) {
54 } 52 }
55 53
56 // To get remaining days from locdate 54 // To get remaining days from locdate
57 -function getDate(dateName, locdate) { 55 +function getLeftDate(dateName, locdate) {
58 var today = new Date(getTodayDate()); // today date 56 var today = new Date(getTodayDate()); // today date
59 var holiday = new Date(getModifiedDate(locdate)); // holiday date 57 var holiday = new Date(getModifiedDate(locdate)); // holiday date
60 var diffDate = today.getTime() - holiday.getTime(); 58 var diffDate = today.getTime() - holiday.getTime();
61 var dday = Math.abs(diffDate / (1000 * 3600 * 24)); 59 var dday = Math.abs(diffDate / (1000 * 3600 * 24));
62 - console.log(dateName + "까지 " + dday + "일 남았습니다.");
63 return dday; 60 return dday;
64 } 61 }
65 62
63 +// To change parameter
66 function changeParams(year, month, operation) { 64 function changeParams(year, month, operation) {
67 queryParams = "?" + "solYear" + "=" + year; 65 queryParams = "?" + "solYear" + "=" + year;
68 queryParams += "&" + "solMonth" + "=" + month; 66 queryParams += "&" + "solMonth" + "=" + month;
...@@ -70,16 +68,16 @@ function changeParams(year, month, operation) { ...@@ -70,16 +68,16 @@ function changeParams(year, month, operation) {
70 requestUrl = url + operation + queryParams; 68 requestUrl = url + operation + queryParams;
71 } 69 }
72 70
73 -// To initialize datas when calling webpage 71 +// To check is this left holiday
74 -function init() { 72 +function cmpDate(date) {
75 - // Intialize Year, Month 73 + var today = getTodayDate().replace(/\-/g, "");
76 - var date = getTodayDate().split("-"); 74 + if (Number(today) <= Number(date)) {
77 - year = date[0]; 75 + return true;
78 - month = date[1]; 76 + }
79 - operation = "getHoliDeInfo"; 77 +}
80 - console.log(year, month); 78 +
81 - changeParams(year, month, operation); 79 +// To get data from holiday api by using parmas
82 - // Get data from holiday api 80 +function getData() {
83 request.get(requestUrl, (err, res, body) => { 81 request.get(requestUrl, (err, res, body) => {
84 if (err) { 82 if (err) {
85 console.log("err => " + err); 83 console.log("err => " + err);
...@@ -88,24 +86,18 @@ function init() { ...@@ -88,24 +86,18 @@ function init() {
88 // Read url success 86 // Read url success
89 var result = body; 87 var result = body;
90 var xmlToJson = convert.xml2json(result, { compact: true, spaces: 4 }); 88 var xmlToJson = convert.xml2json(result, { compact: true, spaces: 4 });
91 - console.log(result);
92 - console.log(xmlToJson);
93 fs.writeFileSync("holi.xml", result); // Create/Modify holi.xml 89 fs.writeFileSync("holi.xml", result); // Create/Modify holi.xml
94 fs.writeFileSync("holi.json", xmlToJson); // Create/Modify holi.json 90 fs.writeFileSync("holi.json", xmlToJson); // Create/Modify holi.json
95 var parser = new xml2js.Parser(); 91 var parser = new xml2js.Parser();
96 parser.parseString(result, function (err, res) { 92 parser.parseString(result, function (err, res) {
97 - console.log(res);
98 text = JSON.stringify(res); 93 text = JSON.stringify(res);
99 - console.log(text);
100 // Get dataName method 94 // Get dataName method
101 dateName = []; 95 dateName = [];
102 var idx = text.indexOf("dateName", 0); 96 var idx = text.indexOf("dateName", 0);
103 while (idx != -1) { 97 while (idx != -1) {
104 - console.log(idx);
105 var start = text.indexOf("[", idx) + 2; 98 var start = text.indexOf("[", idx) + 2;
106 var end = text.indexOf("]", idx) - 1; 99 var end = text.indexOf("]", idx) - 1;
107 var tempStr = text.substring(start, end); 100 var tempStr = text.substring(start, end);
108 - console.log(tempStr);
109 dateName.push(tempStr); 101 dateName.push(tempStr);
110 idx = text.indexOf("dateName", idx + 1); 102 idx = text.indexOf("dateName", idx + 1);
111 } 103 }
...@@ -114,32 +106,71 @@ function init() { ...@@ -114,32 +106,71 @@ function init() {
114 locdate = []; 106 locdate = [];
115 idx = text.indexOf("locdate", 0); 107 idx = text.indexOf("locdate", 0);
116 while (idx != -1) { 108 while (idx != -1) {
117 - console.log(idx);
118 var start = text.indexOf("[", idx) + 2; 109 var start = text.indexOf("[", idx) + 2;
119 var end = text.indexOf("]", idx) - 1; 110 var end = text.indexOf("]", idx) - 1;
120 var tempStr = text.substring(start, end); 111 var tempStr = text.substring(start, end);
121 - console.log(tempStr);
122 locdate.push(tempStr); 112 locdate.push(tempStr);
123 idx = text.indexOf("locdate", idx + 1); 113 idx = text.indexOf("locdate", idx + 1);
124 } 114 }
125 console.log(locdate); 115 console.log(locdate);
126 - // Get date through locdate
127 var holiArr = []; 116 var holiArr = [];
128 for (var i = 0; i < dateName.length; i++) { 117 for (var i = 0; i < dateName.length; i++) {
129 - holiArr.push(getDate(dateName[i], locdate[i])); 118 + holiArr.push(getLeftDate(dateName[i], locdate[i]));
130 } 119 }
131 - // Create tempArr to save dateName and locdate at once 120 + console.log(holiArr);
121 + // Create tempArr to save dateName and locdate and leftDate at once
132 tempArr = []; 122 tempArr = [];
133 - tempArr.push(dateName); 123 + // To check left holiday
134 - tempArr.push(locdate); 124 + var check = false;
135 - tempArr.push(holiArr); 125 + for (var i = 0; i < locdate.length; i++) {
126 + if (cmpDate(locdate[i])) {
127 + check = true;
128 + tempArr.push(dateName[i]); // Get recent holiday name
129 + tempArr.push(locdate[i]); // Get recent holiday date
130 + tempArr.push(holiArr[i]); // Get leftDate through locdate
131 + break;
132 + }
133 + }
136 console.log(tempArr); 134 console.log(tempArr);
135 + if (!check) {
136 + // If there are no holidays left this month
137 + console.log(
138 + "이번 달에는 남은 공휴일이 없습니다. 다음달 데이터를 불러옵니다."
139 + );
140 + // Get next month data
141 + month = String(Number(month) + 1).padStart(2, "0");
142 + changeParams(year, month, operation);
143 + getData();
144 + }
145 + console.log("* api로부터 데이터를 불러왔습니다.");
146 + console.log("오늘의 날짜는", getTodayDate(), "입니다.");
147 + console.log(
148 + "가장 가까운 공휴일인 [" +
149 + tempArr[0] +
150 + "]의 날짜는 [" +
151 + tempArr[1] +
152 + "]이고, [" +
153 + tempArr[2] +
154 + "]일 남았습니다."
155 + );
137 }); 156 });
138 } 157 }
139 } 158 }
140 }); 159 });
141 } 160 }
142 161
162 +// To initialize datas when calling webpage
163 +function init() {
164 + // Intialize Year, Month
165 + var date = getTodayDate().split("-");
166 + year = date[0];
167 + month = date[1];
168 + operation = "getHoliDeInfo";
169 + changeParams(year, month, operation);
170 + // Get data from holiday api
171 + getData();
172 +}
173 +
143 app.get("/", function (req, res) { 174 app.get("/", function (req, res) {
144 init(); 175 init();
145 // Send data from nodejs to ejs 176 // Send data from nodejs to ejs
...@@ -155,5 +186,5 @@ app.get("/", function (req, res) { ...@@ -155,5 +186,5 @@ app.get("/", function (req, res) {
155 186
156 init(); 187 init();
157 188
158 -const port = 8080; 189 +const port = 8888;
159 app.listen(port, () => console.log("Listening on port " + port)); 190 app.listen(port, () => console.log("Listening on port " + port));
......
...@@ -9,10 +9,8 @@ ...@@ -9,10 +9,8 @@
9 <title>Document</title> 9 <title>Document</title>
10 </head> 10 </head>
11 <body> 11 <body>
12 - <% for(var i = 0; i < data[0].length; i++) {%> 12 + <p>
13 - <p> 13 + <h1>[<%= data[0] %>]까지 <%= data[2] %>일 남았습니다. (<%= data[1] %>)</h1>
14 - <h1><%= data[0][i] %> <%= data[1][i] %> <%= data[2][i] %>일 남았습니다.</h1> 14 + </p>
15 - </p>
16 - <% } %>
17 </body> 15 </body>
18 </html> 16 </html>
...\ No newline at end of file ...\ No newline at end of file
......