김대선

데이터 로드시 당일 데이터가 없을 때 전일 데이터 가져옴

...@@ -7,8 +7,31 @@ const read = () =>{ ...@@ -7,8 +7,31 @@ const read = () =>{
7 let tags = [] 7 let tags = []
8 let url = "" 8 let url = ""
9 let companyName = "" 9 let companyName = ""
10 + // const today = new Date()
10 const today = new Date() 11 const today = new Date()
11 - const string = fs.readFileSync(`./datas/${today.getFullYear()}.${today.getMonth()}.${today.getDate()}`, 'utf-8', 'r') 12 + let string
13 + try {
14 + string = fs.readFileSync(`./datas/${today.getFullYear()}.${today.getMonth()}.${today.getDate()}`, 'utf-8', 'r')
15 + } catch (error) {
16 + if(today.getDate()-1 == 0){
17 + 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')
19 + }
20 + else if([4, 6, 9, 11].includes(today.getMonth() - 1)){
21 + string = fs.readFileSync(`./datas/${today.getFullYear()}.${today.getMonth()}.${30}`, 'utf-8', 'r')
22 + }
23 + else if (2 == today.getMonth() - 1){
24 + string = fs.readFileSync(`./datas/${today.getFullYear()}.${today.getMonth()}.${28}`, 'utf-8', 'r')
25 + }
26 + else if (0 == today.getMonth() - 1){
27 + string = fs.readFileSync(`./datas/${today.getFullYear() - 1}.${12}.${31}`, 'utf-8', 'r')
28 + }
29 + }
30 + else{
31 + string = fs.readFileSync(`./datas/${today.getFullYear()}.${today.getMonth()}.${today.getDate()-1}`, 'utf-8', 'r')
32 + }
33 + }
34 + return string
12 const stringArray = string.split('\n') 35 const stringArray = string.split('\n')
13 const size = stringArray.length 36 const size = stringArray.length
14 for(let i = 0 ; i < size; i++){ 37 for(let i = 0 ; i < size; i++){
......