전세계

기능: data.json이 존재하는지 확인하고 데이터를 읽거나 크롤링

Showing 1 changed file with 22 additions and 2 deletions
...@@ -9,6 +9,21 @@ const url = 'http://lol.inven.co.kr/dataninfo/proteam/progamer.php?code=135'; ...@@ -9,6 +9,21 @@ const url = 'http://lol.inven.co.kr/dataninfo/proteam/progamer.php?code=135';
9 9
10 var fakerData = {}; 10 var fakerData = {};
11 11
12 +function checkData() {
13 + try {
14 + const dataBuffer = fs.readFileSync('data.json');
15 + fakerData = dataBuffer.toJSON();
16 + console.log(fakerData);
17 + }
18 + catch (exception) {
19 + console.log(exception);
20 + if (exception.code == "ENOENT") {
21 + console.log("데이터 파일이 존재하지 않습니다. 데이터를 크롤링합니다.");
22 + getData();
23 + }
24 + }
25 +}
26 +
12 const getHTML = async () => { 27 const getHTML = async () => {
13 try { 28 try {
14 return await axios.get(url); 29 return await axios.get(url);
...@@ -17,7 +32,8 @@ const getHTML = async () => { ...@@ -17,7 +32,8 @@ const getHTML = async () => {
17 } 32 }
18 }; 33 };
19 34
20 -getHTML() 35 +function getData() {
36 + getHTML()
21 .then(html => { 37 .then(html => {
22 var today = new Date(); 38 var today = new Date();
23 var dateInfo = `${today.getFullYear()}/${today.getMonth() + 1}/${today.getDate()}`; 39 var dateInfo = `${today.getFullYear()}/${today.getMonth() + 1}/${today.getDate()}`;
...@@ -150,9 +166,13 @@ getHTML() ...@@ -150,9 +166,13 @@ getHTML()
150 console.log(res) 166 console.log(res)
151 fs.writeFileSync('data.json', JSON.stringify(res)); 167 fs.writeFileSync('data.json', JSON.stringify(res));
152 }); 168 });
169 +}
170 +
153 171
154 app.get('/', (req, res) => { 172 app.get('/', (req, res) => {
155 res.send('Express Test'); 173 res.send('Express Test');
156 }); 174 });
157 175
158 -app.listen(port, () => console.log(`app listening at http://localhost:${port}`));
...\ No newline at end of file ...\ No newline at end of file
176 +app.listen(port, () => console.log(`app listening at http://localhost:${port}`));
177 +
178 +checkData();
...\ No newline at end of file ...\ No newline at end of file
......