Showing
1 changed file
with
127 additions
and
128 deletions
... | @@ -15,30 +15,30 @@ var cron = require('node-cron'); | ... | @@ -15,30 +15,30 @@ var cron = require('node-cron'); |
15 | var stock_code = "005930" | 15 | var stock_code = "005930" |
16 | app.use(bodyParser.json()); | 16 | app.use(bodyParser.json()); |
17 | 17 | ||
18 | +let stockList = []; | ||
19 | +fs.readFile('./stock_names.txt', 'utf8', (err,data) => { // 파일 import하고 stockList로 저장 | ||
20 | + if (err){ | ||
21 | + console.error(err) | ||
22 | + return | ||
23 | + } | ||
24 | + var tempstockList = data.toString().split("\n"); // ex) stockList = [[012312 ~~],[123012 ~~],[192312 ~~],[192310 ~~],[...]] | ||
25 | + // console.log("tempStockList:", tempstockList); | ||
26 | + for (let i=0; i<tempstockList.length; i++){ | ||
27 | + stockList[i] = (tempstockList[i].split("\t")); // ex) stockList = [[012312,~~],[1230012,~~],[1923,~~],[192310,~~],[...]] | ||
28 | + } | ||
29 | + // console.log(111) | ||
30 | + // console.log(stockList.length) | ||
31 | + // for (i in stockList) { | ||
32 | + // console.log(stockList[i]); | ||
33 | + // } | ||
34 | +}) | ||
35 | + | ||
18 | app.post('/hook', function (req, res) { | 36 | app.post('/hook', function (req, res) { |
19 | 37 | ||
20 | var eventObj = req.body.events[0]; | 38 | var eventObj = req.body.events[0]; |
21 | var source = eventObj.source; | 39 | var source = eventObj.source; |
22 | var message = eventObj.message; | 40 | var message = eventObj.message; |
23 | - | 41 | + |
24 | - | ||
25 | - let stockList = []; | ||
26 | - //import './stock_names.txt'; | ||
27 | - | ||
28 | - fs.readFile('./stock_names.txt', 'utf8', (err,data) => { // 파일 import하고 stockList로 저장 | ||
29 | - if (err){ | ||
30 | - console.error(err) | ||
31 | - return | ||
32 | - } | ||
33 | - var tempstockList = data.toString().split("\n"); // ex) stockList = [[012312 ~~],[123012 ~~],[192312 ~~],[192310 ~~],[...]] | ||
34 | - for (i in tempstockList){ | ||
35 | - var stockList = tempstockList[i].toString().split(" "); // ex) stockList = [[012312,~~],[1230012,~~],[1923,~~],[192310,~~],[...]] | ||
36 | - } | ||
37 | - // for (i in stockList) { | ||
38 | - // console.log(stockList[i]); | ||
39 | - // } | ||
40 | - }) | ||
41 | - | ||
42 | // request log | 42 | // request log |
43 | console.log('======================', new Date() ,'======================'); | 43 | console.log('======================', new Date() ,'======================'); |
44 | console.log('[request]', req.body); | 44 | console.log('[request]', req.body); |
... | @@ -46,62 +46,22 @@ app.post('/hook', function (req, res) { | ... | @@ -46,62 +46,22 @@ app.post('/hook', function (req, res) { |
46 | console.log('[request message]', eventObj.message); | 46 | console.log('[request message]', eventObj.message); |
47 | 47 | ||
48 | var s_code = get_stock_code(stockList,stock_code,eventObj.replyToken,eventObj.message.text); | 48 | var s_code = get_stock_code(stockList,stock_code,eventObj.replyToken,eventObj.message.text); |
49 | - //var wanted_price = get_wanted_price(eventObj.replyToken,eventObj.message.text); | 49 | + // var wanted_price = get_wanted_price(eventObj.replyToken,eventObj.message.text); |
50 | - | 50 | + //console.log("s_code:", s_code) |
51 | - cron.schedule('*/5 * * * *', () => { | 51 | + if (s_code != -1){ |
52 | - compare(s_code, wanted_price); | 52 | + cron.schedule('*/2 * * * *', () => { |
53 | - console.log('It works') | 53 | + compare(s_code[0], s_code[1],eventObj.replyToken); |
54 | - }); | 54 | + }); |
55 | - | 55 | + // compare(s_code[0], s_code[1],eventObj.replyToken); |
56 | - | 56 | + } |
57 | - | 57 | + |
58 | - // if (eventObj.message.text == "영어"){ | ||
59 | - // lan = "en" | ||
60 | - // } | ||
61 | - // else if (eventObj.message.text == "일본어"){ | ||
62 | - // lan = "ja" | ||
63 | - // } | ||
64 | - // else if (eventObj.message.text == "프랑스어"){ | ||
65 | - // lan = "fr" | ||
66 | - // } | ||
67 | - // else{ | ||
68 | - // trans(eventObj.replyToken, eventObj.message.text); | ||
69 | - // } | ||
70 | - | ||
71 | - | ||
72 | - // request.post( | ||
73 | - // { | ||
74 | - // url: TARGET_URL, | ||
75 | - // headers: { | ||
76 | - // 'Authorization': `Bearer ${TOKEN}` | ||
77 | - // }, | ||
78 | - // json: { | ||
79 | - // "replyToken":eventObj.replyToken, | ||
80 | - // "messages":[ | ||
81 | - // { | ||
82 | - // "type":"text", | ||
83 | - // "text":"Hello, user" | ||
84 | - // }, | ||
85 | - // { | ||
86 | - // "type":"text", | ||
87 | - // "text":"May I help you?" | ||
88 | - // } | ||
89 | - // ] | ||
90 | - // } | ||
91 | - // },(error, response, body) => { | ||
92 | - // console.log(body) | ||
93 | - // }); | ||
94 | - | ||
95 | - | ||
96 | res.sendStatus(200); | 58 | res.sendStatus(200); |
97 | }); | 59 | }); |
98 | 60 | ||
99 | function get_stock_code(stockList,stock_code,replyToken,message){ | 61 | function get_stock_code(stockList,stock_code,replyToken,message){ |
100 | var temp = message.toString().split(" "); | 62 | var temp = message.toString().split(" "); |
101 | - var wanted_stock_name = temp[0]; | ||
102 | - var wanted_stock_price = temp[1]; | ||
103 | 63 | ||
104 | - if (wanted_stock_price < 0){ | 64 | + if (temp.length != 2){ |
105 | request.post( | 65 | request.post( |
106 | { | 66 | { |
107 | url: TARGET_URL, | 67 | url: TARGET_URL, |
... | @@ -113,23 +73,59 @@ function get_stock_code(stockList,stock_code,replyToken,message){ | ... | @@ -113,23 +73,59 @@ function get_stock_code(stockList,stock_code,replyToken,message){ |
113 | "messages":[ | 73 | "messages":[ |
114 | { | 74 | { |
115 | "type":"text", | 75 | "type":"text", |
116 | - "text":"Price must be positive number" | 76 | + "text":"Please type in like the following form" |
117 | }, | 77 | }, |
78 | + { | ||
79 | + "type":"text", | ||
80 | + "text":"(기업명) (가격)" | ||
81 | + } | ||
118 | ] | 82 | ] |
119 | } | 83 | } |
120 | },(error, response, body) => { | 84 | },(error, response, body) => { |
121 | console.log(body) | 85 | console.log(body) |
122 | }); | 86 | }); |
123 | - | 87 | + return -1; |
124 | } | 88 | } |
125 | 89 | ||
90 | + else{ | ||
91 | + //console.log(temp) | ||
92 | + var wanted_stock_name = temp[0]; | ||
93 | + var wanted_stock_price = temp[1]; | ||
94 | + var is_in_list = false; | ||
95 | + | ||
96 | + console.log("stockList:",stockList.length); | ||
97 | + | ||
98 | + if (wanted_stock_price < 0){ // 원하는 가격 입력받음 (0 이상만) | ||
99 | + request.post( | ||
100 | + { | ||
101 | + url: TARGET_URL, | ||
102 | + headers: { | ||
103 | + 'Authorization': `Bearer ${TOKEN}` | ||
104 | + }, | ||
105 | + json: { | ||
106 | + "replyToken":replyToken, | ||
107 | + "messages":[ | ||
108 | + { | ||
109 | + "type":"text", | ||
110 | + "text":"Price must be positive number" | ||
111 | + }, | ||
112 | + ] | ||
113 | + } | ||
114 | + },(error, response, body) => { | ||
115 | + console.log(body) | ||
116 | + }); | ||
117 | + return -1; | ||
118 | + } | ||
126 | 119 | ||
127 | - for (let i = 0; i<stockList.length; i++) { | 120 | + for (let i = 0; i<stockList.length; i++) { |
128 | - if (wanted_stock_name == stockList[i][1]){ // 입력한거 나오면 코드로 바꿔줌 | 121 | + if (wanted_stock_name == stockList[i][1]){ // 입력한거 나오면 코드로 바꿔줌 |
129 | - stock_code = stockList[i][0] | 122 | + // console.log(123) |
130 | - return stock_code | 123 | + stock_code = stockList[i][0] |
124 | + is_in_list = true | ||
125 | + return [stock_code, wanted_stock_price] | ||
126 | + } | ||
131 | } | 127 | } |
132 | - else{ // stockList에 없으면 없다고 보냄 | 128 | + if (is_in_list == false) { // stockList에 없으면 없다고 보냄 |
133 | request.post( | 129 | request.post( |
134 | { | 130 | { |
135 | url: TARGET_URL, | 131 | url: TARGET_URL, |
... | @@ -148,66 +144,69 @@ function get_stock_code(stockList,stock_code,replyToken,message){ | ... | @@ -148,66 +144,69 @@ function get_stock_code(stockList,stock_code,replyToken,message){ |
148 | },(error, response, body) => { | 144 | },(error, response, body) => { |
149 | console.log(body) | 145 | console.log(body) |
150 | }); | 146 | }); |
151 | - } | 147 | + return -1; |
152 | - | 148 | + } |
153 | } | 149 | } |
154 | } | 150 | } |
155 | 151 | ||
156 | -// function get_wanted_price(replyToken,message){ // 원하는 가격 입력받음 (0 이상만) | 152 | +function compare(s_code, wanted_price,replyToken){ |
157 | -// if (message < 0){ | 153 | + const temp = axios.get(`https://finance.naver.com/item/main.nhn?code=${s_code}`).then( |
158 | -// request.post( | ||
159 | -// { | ||
160 | -// url: TARGET_URL, | ||
161 | -// headers: { | ||
162 | -// 'Authorization': `Bearer ${TOKEN}` | ||
163 | -// }, | ||
164 | -// json: { | ||
165 | -// "replyToken":eventObj.replyToken, | ||
166 | -// "messages":[ | ||
167 | -// { | ||
168 | -// "type":"text", | ||
169 | -// "text":"Price must be positive number" | ||
170 | -// }, | ||
171 | -// ] | ||
172 | -// } | ||
173 | -// },(error, response, body) => { | ||
174 | -// console.log(body) | ||
175 | -// }); | ||
176 | -// } | ||
177 | - | ||
178 | -// } | ||
179 | - | ||
180 | -function compare(s_code, wanted_price){ | ||
181 | - const temp = axios.get(`https://finance.naver.com/item/main.nhn?code=${s_code}%22`).then( | ||
182 | res => { | 154 | res => { |
183 | - let urList = []; | 155 | + // console.log(res.data) |
184 | const $ = cheerio.load(res.data) | 156 | const $ = cheerio.load(res.data) |
185 | - const $bodyList = $("dl.blind").children("dd").text(); | 157 | + let $bodyList = $("dl.blind").text(); |
186 | - const result = $bodyList.slice(73, 80) // 실시간으로 변하는 주식 가격 scrape | 158 | + $bodyList = $bodyList.split('\n')[5]; |
187 | - console.log(result) | 159 | + $bodyList = $bodyList.slice(14, $bodyList.length); |
160 | + $bodyList = $bodyList.split(' ')[0]; | ||
161 | + // console.log($bodyList) | ||
162 | + let result = $bodyList // 실시간으로 변하는 주식 가격 scrape | ||
163 | + | ||
164 | + | ||
165 | + result = result.replace(',', '') | ||
166 | + console.log("current price:",parseInt(result)) | ||
167 | + if (parseInt(result) >= wanted_price){ | ||
168 | + request.post( | ||
169 | + { | ||
170 | + url: TARGET_URL, | ||
171 | + headers: { | ||
172 | + 'Authorization': `Bearer ${TOKEN}` | ||
173 | + }, | ||
174 | + json: { | ||
175 | + "replyToken":replyToken, | ||
176 | + "messages":[ | ||
177 | + { | ||
178 | + "type":"text", | ||
179 | + "text":"Reached wanted price!" | ||
180 | + }, | ||
181 | + ] | ||
182 | + } | ||
183 | + },(error, response, body) => { | ||
184 | + console.log(body) | ||
185 | + }); | ||
186 | + } | ||
188 | } | 187 | } |
189 | ) | 188 | ) |
190 | 189 | ||
191 | - if (int(result) <= wanted_price){ | 190 | + // if (parseInt(result) <= wanted_price){ |
192 | - request.post( | 191 | + // request.post( |
193 | - { | 192 | + // { |
194 | - url: TARGET_URL, | 193 | + // url: TARGET_URL, |
195 | - headers: { | 194 | + // headers: { |
196 | - 'Authorization': `Bearer ${TOKEN}` | 195 | + // 'Authorization': `Bearer ${TOKEN}` |
197 | - }, | 196 | + // }, |
198 | - json: { | 197 | + // json: { |
199 | - "replyToken":replyToken, | 198 | + // "replyToken":replyToken, |
200 | - "messages":[ | 199 | + // "messages":[ |
201 | - { | 200 | + // { |
202 | - "type":"text", | 201 | + // "type":"text", |
203 | - "text":"Reached wanted price!" | 202 | + // "text":"Reached wanted price!" |
204 | - }, | 203 | + // }, |
205 | - ] | 204 | + // ] |
206 | - } | 205 | + // } |
207 | - },(error, response, body) => { | 206 | + // },(error, response, body) => { |
208 | - console.log(body) | 207 | + // console.log(body) |
209 | - }); | 208 | + // }); |
210 | - } | 209 | + // } |
211 | 210 | ||
212 | 211 | ||
213 | 212 | ... | ... |
-
Please register or login to post a comment