김대선

파일 저장이 안됐을 때, read() 호출하면 다시 save() 구현, save가 너무 많이 호출되는 것을 막기위한 status 변수 사용

1 const fs = require('fs') 1 const fs = require('fs')
2 const functions = require('./function') 2 const functions = require('./function')
3 3
4 +let status = false
5 +
6 +const save = async()=> {
7 + if(status){
8 + return
9 + }
10 + else if(!status){
11 + status = true
12 + }
13 + try {
14 + let string = ""
15 + let data = await functions.getKakaoData()
16 + let temp = ""
17 + for(let i of data){
18 + temp = "title : " + i.title
19 + string = string + temp + "\n"
20 + temp = "tags : " + i.tags.toString()
21 + string = string + temp + "\n"
22 + temp = "url : " + i.url
23 + string = string + temp + "\n"
24 + temp = "companyName : " + i.companyName
25 + string = string + temp + "\n"
26 + }
27 + data = await functions.getNaverFunction()
28 + temp = ""
29 + for(let i of data){
30 + temp = "title : " + i.title
31 + string = string + temp + "\n"
32 + temp = "tags : " + i.tags.toString()
33 + string = string + temp + "\n"
34 + temp = "url : " + i.url
35 + string = string + temp + "\n"
36 + temp = "companyName : " + i.companyName
37 + string = string + temp + "\n"
38 + }
39 + data = await functions.getProgrammersFunction()
40 + temp = ""
41 + for(let i of data){
42 + temp = "title : " + i.title
43 + string = string + temp + "\n"
44 + temp = "tags : " + i.tags.toString()
45 + string = string + temp + "\n"
46 + temp = "url : " + i.url
47 + string = string + temp + "\n"
48 + temp = "companyName : " + i.companyName
49 + string = string + temp + "\n"
50 + }
51 + const today = new Date()
52 + fs.writeFile(`./datas/${today.getFullYear()}.${today.getMonth()}.${today.getDate()}`, string, 'utf-8', (err)=>{
53 + status = false
54 + if(err){
55 + console.log("파일저장시에 오류")
56 + }
57 + else console.log("저장완료!")
58 + })
59 + } catch (error) {
60 + status = false
61 + console.log("파일저장시에 오류")
62 + }
63 +}
64 +
4 const read = () =>{ 65 const read = () =>{
5 const data = [] 66 const data = []
6 let title = "" 67 let title = ""
...@@ -13,6 +74,7 @@ const read = () =>{ ...@@ -13,6 +74,7 @@ const read = () =>{
13 try { 74 try {
14 string = fs.readFileSync(`./datas/${today.getFullYear()}.${today.getMonth()}.${today.getDate()}`, 'utf-8', 'r') 75 string = fs.readFileSync(`./datas/${today.getFullYear()}.${today.getMonth()}.${today.getDate()}`, 'utf-8', 'r')
15 } catch (error) { 76 } catch (error) {
77 + save()
16 if(today.getDate()-1 == 0){ 78 if(today.getDate()-1 == 0){
17 if([1, 3, 5, 7, 8, 10, 12].includes(today.getMonth() - 1)){ 79 if([1, 3, 5, 7, 8, 10, 12].includes(today.getMonth() - 1)){
18 string = fs.readFileSync(`./datas/${today.getFullYear()}.${today.getMonth() - 1}.${31}`, 'utf-8', 'r') 80 string = fs.readFileSync(`./datas/${today.getFullYear()}.${today.getMonth() - 1}.${31}`, 'utf-8', 'r')
...@@ -57,63 +119,6 @@ const read = () =>{ ...@@ -57,63 +119,6 @@ const read = () =>{
57 return data 119 return data
58 } 120 }
59 121
60 -const save = async()=> {
61 - try {
62 - let string = ""
63 - let data = await functions.getKakaoData()
64 - let temp = ""
65 - for(let i of data){
66 - temp = "title : " + i.title
67 - string = string + temp + "\n"
68 - temp = "tags : " + i.tags.toString()
69 - string = string + temp + "\n"
70 - temp = "url : " + i.url
71 - string = string + temp + "\n"
72 - temp = "companyName : " + i.companyName
73 - string = string + temp + "\n"
74 - }
75 -
76 - data = await functions.getNaverFunction()
77 - temp = ""
78 - for(let i of data){
79 - temp = "title : " + i.title
80 - string = string + temp + "\n"
81 - temp = "tags : " + i.tags.toString()
82 - string = string + temp + "\n"
83 - temp = "url : " + i.url
84 - string = string + temp + "\n"
85 - temp = "companyName : " + i.companyName
86 - string = string + temp + "\n"
87 - }
88 -
89 - data = await functions.getProgrammersFunction()
90 - temp = ""
91 - for(let i of data){
92 - temp = "title : " + i.title
93 - string = string + temp + "\n"
94 - temp = "tags : " + i.tags.toString()
95 - string = string + temp + "\n"
96 - temp = "url : " + i.url
97 - string = string + temp + "\n"
98 - temp = "companyName : " + i.companyName
99 - string = string + temp + "\n"
100 - }
101 - const today = new Date()
102 - fs.writeFile(`./datas/${today.getFullYear()}.${today.getMonth()}.${today.getDate()}`, string, 'utf-8', (err)=>{
103 - if(err){
104 - console.log("파일저장시에 오류")
105 - console.log(err)
106 - save()
107 - }
108 - else console.log("저장완료!")
109 - })
110 - } catch (error) {
111 - console.log("데이터 가져오는 과정에서 오류")
112 - console.log(error)
113 - save()
114 - }
115 -}
116 -
117 module.exports = { 122 module.exports = {
118 save : save, 123 save : save,
119 read : read 124 read : read
......
...@@ -57,13 +57,10 @@ const getData = async ()=>{ ...@@ -57,13 +57,10 @@ const getData = async ()=>{
57 decodeEntities : true 57 decodeEntities : true
58 } 58 }
59 }).replace(/(<([^>]+)>)*(\\t)?/gi, "") 59 }).replace(/(<([^>]+)>)*(\\t)?/gi, "")
60 - console.log(final)
61 while(true){ 60 while(true){
62 await page.goto(`https://programmers.co.kr/job?page=${count}`) 61 await page.goto(`https://programmers.co.kr/job?page=${count}`)
63 content = await page.content() 62 content = await page.content()
64 - console.log( final, count)
65 if(final < count){ 63 if(final < count){
66 - console.log("finish", result.length)
67 break; 64 break;
68 } 65 }
69 $ = cheerio.load(content, {decodeEntities: true}) 66 $ = cheerio.load(content, {decodeEntities: true})
...@@ -78,7 +75,6 @@ const getData = async ()=>{ ...@@ -78,7 +75,6 @@ const getData = async ()=>{
78 } 75 }
79 }) 76 })
80 if(item ==''){ 77 if(item ==''){
81 - console.log("break!!!!")
82 break; 78 break;
83 } 79 }
84 item = item.split("</div>`") 80 item = item.split("</div>`")
...@@ -88,7 +84,7 @@ const getData = async ()=>{ ...@@ -88,7 +84,7 @@ const getData = async ()=>{
88 result = result.concat(await makeObject(resArr)) 84 result = result.concat(await makeObject(resArr))
89 count = count + 1 85 count = count + 1
90 } 86 }
91 - console.log(result) 87 + console.log("kakao : ", result.length)
92 return result 88 return result
93 } 89 }
94 90
......