전세계

버그: 연속적으로 checkData를 수행할 경우 발생하는 버그 수정(비동기 함수로 전환)

Showing 1 changed file with 36 additions and 21 deletions
...@@ -8,41 +8,58 @@ const cheerio = require('cheerio'); ...@@ -8,41 +8,58 @@ const cheerio = require('cheerio');
8 const url = 'http://lol.inven.co.kr/dataninfo/proteam/progamer.php?code=135'; 8 const url = 'http://lol.inven.co.kr/dataninfo/proteam/progamer.php?code=135';
9 9
10 const version = '0.1'; 10 const version = '0.1';
11 +const dataFileName = 'data.json';
11 12
12 var fakerData = {}; 13 var fakerData = {};
13 14
14 -function checkData() { 15 +async function checkData() {
15 try { 16 try {
16 - const dataBuffer = fs.readFileSync('data.json'); 17 + if (fakerData.version == version) {
17 - console.log("데이터 파일이 존재합니다. 데이터를 읽습니다."); 18 + console.log("이미 데이터가 존재합니다.");
19 + }
20 + else if (fakerData.version == undefined) {
21 + const dataBuffer = fs.readFileSync(dataFileName);
22 + console.log("데이터를 읽습니다.");
18 fakerData = JSON.parse(dataBuffer.toString()); 23 fakerData = JSON.parse(dataBuffer.toString());
19 console.log(fakerData); 24 console.log(fakerData);
20 - 25 + }
21 - if(fakerData.version != version){ 26 + else {
22 console.log("버전이 다릅니다. 데이터를 크롤링합니다."); 27 console.log("버전이 다릅니다. 데이터를 크롤링합니다.");
23 - getData(); 28 + await getData();
24 } 29 }
25 } 30 }
26 catch (exception) { 31 catch (exception) {
27 - console.log(exception);
28 if (exception.code == "ENOENT") { 32 if (exception.code == "ENOENT") {
29 console.log("데이터 파일이 존재하지 않습니다. 데이터를 크롤링합니다."); 33 console.log("데이터 파일이 존재하지 않습니다. 데이터를 크롤링합니다.");
30 - getData(); 34 + await getData();
35 + }
36 + else {
37 + console.log(exception);
31 } 38 }
32 } 39 }
40 +
41 + return new Promise(function (resolve, reject) {
42 + resolve();
43 + });
33 } 44 }
34 45
35 -const getHTML = async () => { 46 +async function getHTML() {
36 try { 47 try {
37 - return await axios.get(url); 48 + return axios.get(url);
38 } catch (error) { 49 } catch (error) {
39 console.error(error); 50 console.error(error);
51 + return null;
40 } 52 }
41 }; 53 };
42 54
43 -function getData() { 55 +async function getData() {
44 - getHTML() 56 + const html = await getHTML();
45 - .then(html => { 57 + if (html == null) {
58 + return;
59 + }
60 +
61 + fakerData['version'] = version;
62 +
46 var today = new Date(); 63 var today = new Date();
47 var dateInfo = `${today.getFullYear()}/${today.getMonth() + 1}/${today.getDate()}`; 64 var dateInfo = `${today.getFullYear()}/${today.getMonth() + 1}/${today.getDate()}`;
48 fakerData['date'] = dateInfo; 65 fakerData['date'] = dateInfo;
...@@ -119,7 +136,7 @@ function getData() { ...@@ -119,7 +136,7 @@ function getData() {
119 var tr = $('div.scriptorium').children('div.listTable').eq(3).find('table tbody').children(); 136 var tr = $('div.scriptorium').children('div.listTable').eq(3).find('table tbody').children();
120 var competitionData = {}; 137 var competitionData = {};
121 138
122 - tr.each(function (i,elem) { 139 + tr.each(function (i, elem) {
123 var td = $(this).children(); 140 var td = $(this).children();
124 var competitionName = td.eq(0).text().replace(td.eq(0).find('span').text(), ''); 141 var competitionName = td.eq(0).text().replace(td.eq(0).find('span').text(), '');
125 142
...@@ -171,11 +188,11 @@ function getData() { ...@@ -171,11 +188,11 @@ function getData() {
171 fakerData['champions'] = championData; 188 fakerData['champions'] = championData;
172 //#endregion 189 //#endregion
173 190
174 - return fakerData; 191 + console.log(fakerData);
175 - }) 192 + fs.writeFileSync(dataFileName, JSON.stringify(fakerData));
176 - .then(res => { 193 +
177 - console.log(res) 194 + return new Promise(function (resolve, reject) {
178 - fs.writeFileSync('data.json', JSON.stringify(res)); 195 + resolve();
179 }); 196 });
180 } 197 }
181 198
...@@ -185,5 +202,3 @@ app.get('/', (req, res) => { ...@@ -185,5 +202,3 @@ app.get('/', (req, res) => {
185 }); 202 });
186 203
187 app.listen(port, () => console.log(`app listening at http://localhost:${port}`)); 204 app.listen(port, () => console.log(`app listening at http://localhost:${port}`));
...\ No newline at end of file ...\ No newline at end of file
188 -
189 -checkData();
...\ No newline at end of file ...\ No newline at end of file
......