app.js
2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const express = require('express')
const schedule = require('node-schedule')
const fs = require('fs')
const functions = require('./function')
const app = express();
const readData = async () =>{
const data = []
let title = ""
let tags = []
let url = ""
const today = new Date()
const string = fs.readFileSync(`./datas/${today.getFullYear()}.${today.getMonth()}.${today.getDate()}`, 'utf-8', 'r')
const stringArray = string.split('\n')
const size = stringArray.length
for(let i = 0 ; i < size; i++){
if( i % 3 == 0){
title = stringArray[i].replace("title : ", "")
}
else if( i % 3 == 1){
tags = stringArray[i].replace("tags : ", "").split(",")
}
else if ( i % 3 == 2){
url = stringArray[i].replace("url : ", "")
data.push({
title : title,
tags : tags,
url : url
})
}
}
console.log(data)
}
const save = async()=> {
try {
let string = ""
let data = await functions.getKakaoData()
let temp = ""
for(let i of data){
temp = "title : " + i.title
string = string + temp + "\n"
temp = "tags : " + i.tags.toString()
string = string + temp + "\n"
temp = "url : " + i.url
string = string + temp + "\n"
}
data = await functions.getNaverFunction()
temp = ""
for(let i of data){
temp = "title : " + i.title
string = string + temp + "\n"
if(!!i.tags){
temp = "tags : " + i.tags.toString()
string = string + temp + "\n"
}
temp = "url : " + i.url
string = string + temp + "\n"
}
data = await functions.getProgrammersFunction()
temp = ""
for(let i of data){
temp = "title : " + i.title
string = string + temp + "\n"
temp = "tags : " + i.tags.toString()
string = string + temp + "\n"
temp = "url : " + i.url
string = string + temp + "\n"
}
const today = new Date()
fs.writeFile(`./datas/${today.getFullYear()}.${today.getMonth()}.${today.getDate()}`, string, 'utf-8', (err)=>{
if(err){
save()
}
else console.log("저장완료!")
})
} catch (error) {
console.log(error)
save()
}
}
// 0초 0분 0시 아무날 아무달 아무년
const saveData = schedule.scheduleJob('0 0 0 * * *', save)
const server = app.listen(3000,()=>{
const host = server.address().address
const port = server.address().port
console.log("app listening at http://%s:%s", host, port)
})
app.get('/', async (req, res)=>{
res.send(string)
})