전세계

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

Showing 1 changed file with 36 additions and 21 deletions
......@@ -8,41 +8,58 @@ const cheerio = require('cheerio');
const url = 'http://lol.inven.co.kr/dataninfo/proteam/progamer.php?code=135';
const version = '0.1';
const dataFileName = 'data.json';
var fakerData = {};
function checkData() {
async function checkData() {
try {
const dataBuffer = fs.readFileSync('data.json');
console.log("데이터 파일이 존재합니다. 데이터를 읽습니다.");
if (fakerData.version == version) {
console.log("이미 데이터가 존재합니다.");
}
else if (fakerData.version == undefined) {
const dataBuffer = fs.readFileSync(dataFileName);
console.log("데이터를 읽습니다.");
fakerData = JSON.parse(dataBuffer.toString());
console.log(fakerData);
if(fakerData.version != version){
}
else {
console.log("버전이 다릅니다. 데이터를 크롤링합니다.");
getData();
await getData();
}
}
catch (exception) {
console.log(exception);
if (exception.code == "ENOENT") {
console.log("데이터 파일이 존재하지 않습니다. 데이터를 크롤링합니다.");
getData();
await getData();
}
else {
console.log(exception);
}
}
return new Promise(function (resolve, reject) {
resolve();
});
}
const getHTML = async () => {
async function getHTML() {
try {
return await axios.get(url);
return axios.get(url);
} catch (error) {
console.error(error);
return null;
}
};
function getData() {
getHTML()
.then(html => {
async function getData() {
const html = await getHTML();
if (html == null) {
return;
}
fakerData['version'] = version;
var today = new Date();
var dateInfo = `${today.getFullYear()}/${today.getMonth() + 1}/${today.getDate()}`;
fakerData['date'] = dateInfo;
......@@ -119,7 +136,7 @@ function getData() {
var tr = $('div.scriptorium').children('div.listTable').eq(3).find('table tbody').children();
var competitionData = {};
tr.each(function (i,elem) {
tr.each(function (i, elem) {
var td = $(this).children();
var competitionName = td.eq(0).text().replace(td.eq(0).find('span').text(), '');
......@@ -171,11 +188,11 @@ function getData() {
fakerData['champions'] = championData;
//#endregion
return fakerData;
})
.then(res => {
console.log(res)
fs.writeFileSync('data.json', JSON.stringify(res));
console.log(fakerData);
fs.writeFileSync(dataFileName, JSON.stringify(fakerData));
return new Promise(function (resolve, reject) {
resolve();
});
}
......@@ -185,5 +202,3 @@ app.get('/', (req, res) => {
});
app.listen(port, () => console.log(`app listening at http://localhost:${port}`));
\ No newline at end of file
checkData();
\ No newline at end of file
......