Showing
2 changed files
with
70 additions
and
41 deletions
1 | const fs = require('fs') | 1 | const fs = require('fs') |
2 | const functions = require('./function') | 2 | const functions = require('./function') |
3 | 3 | ||
4 | -const read = () =>{ | 4 | +let status = false |
5 | - const data = [] | ||
6 | - let title = "" | ||
7 | - let tags = [] | ||
8 | - let url = "" | ||
9 | - let companyName = "" | ||
10 | - const today = new Date() | ||
11 | - const string = fs.readFileSync(`./datas/${today.getFullYear()}.${today.getMonth()}.${today.getDate()}`, 'utf-8', 'r') | ||
12 | - const stringArray = string.split('\n') | ||
13 | - const size = stringArray.length | ||
14 | - for(let i = 0 ; i < size; i++){ | ||
15 | - if( i % 4 == 0){ | ||
16 | - title = stringArray[i].replace("title : ", "") | ||
17 | - } | ||
18 | - else if( i % 4 == 1){ | ||
19 | - tags = stringArray[i].replace("tags : ", "").split(",") | ||
20 | - } | ||
21 | - else if( i % 4 == 2){ | ||
22 | - url = stringArray[i].replace("url : ", "") | ||
23 | - } | ||
24 | - else if ( i % 4 == 3){ | ||
25 | - companyName = stringArray[i].replace("companyName : ", "") | ||
26 | - data.push({ | ||
27 | - title : title, | ||
28 | - tags : tags, | ||
29 | - url : url, | ||
30 | - companyName : companyName | ||
31 | - }) | ||
32 | - } | ||
33 | - } | ||
34 | - return data | ||
35 | -} | ||
36 | 5 | ||
37 | const save = async()=> { | 6 | const save = async()=> { |
7 | + if(status){ | ||
8 | + return | ||
9 | + } | ||
10 | + else if(!status){ | ||
11 | + status = true | ||
12 | + } | ||
38 | try { | 13 | try { |
39 | let string = "" | 14 | let string = "" |
40 | let data = await functions.getKakaoData() | 15 | let data = await functions.getKakaoData() |
... | @@ -61,7 +36,10 @@ const save = async()=> { | ... | @@ -61,7 +36,10 @@ const save = async()=> { |
61 | temp = "companyName : " + i.companyName | 36 | temp = "companyName : " + i.companyName |
62 | string = string + temp + "\n" | 37 | string = string + temp + "\n" |
63 | } | 38 | } |
39 | +<<<<<<< HEAD | ||
64 | 40 | ||
41 | +======= | ||
42 | +>>>>>>> 694647de3d94a82e2485ce202270c0702aaed6f7 | ||
65 | data = await functions.getProgrammersFunction() | 43 | data = await functions.getProgrammersFunction() |
66 | temp = "" | 44 | temp = "" |
67 | for(let i of data){ | 45 | for(let i of data){ |
... | @@ -77,18 +55,73 @@ const save = async()=> { | ... | @@ -77,18 +55,73 @@ const save = async()=> { |
77 | 55 | ||
78 | const today = new Date() | 56 | const today = new Date() |
79 | fs.writeFile(`./datas/${today.getFullYear()}.${today.getMonth()}.${today.getDate()}`, string, 'utf-8', (err)=>{ | 57 | fs.writeFile(`./datas/${today.getFullYear()}.${today.getMonth()}.${today.getDate()}`, string, 'utf-8', (err)=>{ |
58 | + status = false | ||
80 | if(err){ | 59 | if(err){ |
81 | console.log("파일저장시에 오류") | 60 | console.log("파일저장시에 오류") |
82 | - console.log(err) | ||
83 | - save() | ||
84 | } | 61 | } |
85 | else console.log("저장완료!") | 62 | else console.log("저장완료!") |
86 | }) | 63 | }) |
87 | } catch (error) { | 64 | } catch (error) { |
88 | - console.log("데이터 가져오는 과정에서 오류") | 65 | + status = false |
89 | - console.log(error) | 66 | + console.log("파일저장시에 오류") |
67 | + } | ||
68 | +} | ||
69 | + | ||
70 | +const read = () =>{ | ||
71 | + const data = [] | ||
72 | + let title = "" | ||
73 | + let tags = [] | ||
74 | + let url = "" | ||
75 | + let companyName = "" | ||
76 | + // const today = new Date() | ||
77 | + const today = new Date() | ||
78 | + let string | ||
79 | + try { | ||
80 | + string = fs.readFileSync(`./datas/${today.getFullYear()}.${today.getMonth()}.${today.getDate()}`, 'utf-8', 'r') | ||
81 | + } catch (error) { | ||
90 | save() | 82 | save() |
83 | + if(today.getDate()-1 == 0){ | ||
84 | + if([1, 3, 5, 7, 8, 10, 12].includes(today.getMonth() - 1)){ | ||
85 | + string = fs.readFileSync(`./datas/${today.getFullYear()}.${today.getMonth() - 1}.${31}`, 'utf-8', 'r') | ||
86 | + } | ||
87 | + else if([4, 6, 9, 11].includes(today.getMonth() - 1)){ | ||
88 | + string = fs.readFileSync(`./datas/${today.getFullYear()}.${today.getMonth()}.${30}`, 'utf-8', 'r') | ||
89 | + } | ||
90 | + else if (2 == today.getMonth() - 1){ | ||
91 | + string = fs.readFileSync(`./datas/${today.getFullYear()}.${today.getMonth()}.${28}`, 'utf-8', 'r') | ||
92 | + } | ||
93 | + else if (0 == today.getMonth() - 1){ | ||
94 | + string = fs.readFileSync(`./datas/${today.getFullYear() - 1}.${12}.${31}`, 'utf-8', 'r') | ||
95 | + } | ||
96 | + } | ||
97 | + else{ | ||
98 | + string = fs.readFileSync(`./datas/${today.getFullYear()}.${today.getMonth()}.${today.getDate()-1}`, 'utf-8', 'r') | ||
99 | + } | ||
91 | } | 100 | } |
101 | + return string | ||
102 | + const stringArray = string.split('\n') | ||
103 | + const size = stringArray.length | ||
104 | + for(let i = 0 ; i < size; i++){ | ||
105 | + if( i % 4 == 0){ | ||
106 | + title = stringArray[i].replace("title : ", "") | ||
107 | + } | ||
108 | + else if( i % 4 == 1){ | ||
109 | + tags = stringArray[i].replace("tags : ", "").split(",") | ||
110 | + } | ||
111 | + else if( i % 4 == 2){ | ||
112 | + url = stringArray[i].replace("url : ", "") | ||
113 | + } | ||
114 | + else if ( i % 4 == 3){ | ||
115 | + companyName = stringArray[i].replace("companyName : ", "") | ||
116 | + data.push({ | ||
117 | + title : title, | ||
118 | + tags : tags, | ||
119 | + url : url, | ||
120 | + companyName : companyName | ||
121 | + }) | ||
122 | + } | ||
123 | + } | ||
124 | + return data | ||
92 | } | 125 | } |
93 | 126 | ||
94 | module.exports = { | 127 | module.exports = { | ... | ... |
... | @@ -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 | ... | ... |
-
Please register or login to post a comment