Ubuntu

Debug functions

Showing 1 changed file with 97 additions and 98 deletions
...@@ -15,29 +15,29 @@ var cron = require('node-cron'); ...@@ -15,29 +15,29 @@ 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 -app.post('/hook', function (req, res) { 18 +let stockList = [];
19 - 19 +fs.readFile('./stock_names.txt', 'utf8', (err,data) => { // 파일 import하고 stockList로 저장
20 - var eventObj = req.body.events[0];
21 - var source = eventObj.source;
22 - var message = eventObj.message;
23 -
24 -
25 - let stockList = [];
26 - //import './stock_names.txt';
27 -
28 - fs.readFile('./stock_names.txt', 'utf8', (err,data) => { // 파일 import하고 stockList로 저장
29 if (err){ 20 if (err){
30 console.error(err) 21 console.error(err)
31 return 22 return
32 } 23 }
33 var tempstockList = data.toString().split("\n"); // ex) stockList = [[012312 ~~],[123012 ~~],[192312 ~~],[192310 ~~],[...]] 24 var tempstockList = data.toString().split("\n"); // ex) stockList = [[012312 ~~],[123012 ~~],[192312 ~~],[192310 ~~],[...]]
34 - for (i in tempstockList){ 25 + // console.log("tempStockList:", tempstockList);
35 - var stockList = tempstockList[i].toString().split(" "); // ex) stockList = [[012312,~~],[1230012,~~],[1923,~~],[192310,~~],[...]] 26 + for (let i=0; i<tempstockList.length; i++){
27 + stockList[i] = (tempstockList[i].split("\t")); // ex) stockList = [[012312,~~],[1230012,~~],[1923,~~],[192310,~~],[...]]
36 } 28 }
29 + // console.log(111)
30 + // console.log(stockList.length)
37 // for (i in stockList) { 31 // for (i in stockList) {
38 // console.log(stockList[i]); 32 // console.log(stockList[i]);
39 // } 33 // }
40 - }) 34 +})
35 +
36 +app.post('/hook', function (req, res) {
37 +
38 + var eventObj = req.body.events[0];
39 + var source = eventObj.source;
40 + var message = eventObj.message;
41 41
42 // request log 42 // request log
43 console.log('======================', new Date() ,'======================'); 43 console.log('======================', new Date() ,'======================');
...@@ -46,62 +46,56 @@ app.post('/hook', function (req, res) { ...@@ -46,62 +46,56 @@ 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 -
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 57
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(" ");
63 +
64 + if (temp.length != 2){
65 + request.post(
66 + {
67 + url: TARGET_URL,
68 + headers: {
69 + 'Authorization': `Bearer ${TOKEN}`
70 + },
71 + json: {
72 + "replyToken":replyToken,
73 + "messages":[
74 + {
75 + "type":"text",
76 + "text":"Please type in like the following form"
77 + },
78 + {
79 + "type":"text",
80 + "text":"(기업명) (가격)"
81 + }
82 + ]
83 + }
84 + },(error, response, body) => {
85 + console.log(body)
86 + });
87 + return -1;
88 + }
89 +
90 + else{
91 + //console.log(temp)
101 var wanted_stock_name = temp[0]; 92 var wanted_stock_name = temp[0];
102 var wanted_stock_price = temp[1]; 93 var wanted_stock_price = temp[1];
94 + var is_in_list = false;
95 +
96 + console.log("stockList:",stockList.length);
103 97
104 - if (wanted_stock_price < 0){ 98 + if (wanted_stock_price < 0){ // 원하는 가격 입력받음 (0 이상만)
105 request.post( 99 request.post(
106 { 100 {
107 url: TARGET_URL, 101 url: TARGET_URL,
...@@ -120,16 +114,18 @@ function get_stock_code(stockList,stock_code,replyToken,message){ ...@@ -120,16 +114,18 @@ function get_stock_code(stockList,stock_code,replyToken,message){
120 },(error, response, body) => { 114 },(error, response, body) => {
121 console.log(body) 115 console.log(body)
122 }); 116 });
123 - 117 + return -1;
124 } 118 }
125 119
126 -
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]){ // 입력한거 나오면 코드로 바꿔줌
122 + // console.log(123)
129 stock_code = stockList[i][0] 123 stock_code = stockList[i][0]
130 - return stock_code 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,47 +144,27 @@ function get_stock_code(stockList,stock_code,replyToken,message){ ...@@ -148,47 +144,27 @@ 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 });
147 + return -1;
151 } 148 }
152 -
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);
188 - } 160 + $bodyList = $bodyList.split(' ')[0];
189 - ) 161 + // console.log($bodyList)
162 + let result = $bodyList // 실시간으로 변하는 주식 가격 scrape
190 163
191 - if (int(result) <= wanted_price){ 164 +
165 + result = result.replace(',', '')
166 + console.log("current price:",parseInt(result))
167 + if (parseInt(result) >= wanted_price){
192 request.post( 168 request.post(
193 { 169 {
194 url: TARGET_URL, 170 url: TARGET_URL,
...@@ -208,6 +184,29 @@ function compare(s_code, wanted_price){ ...@@ -208,6 +184,29 @@ function compare(s_code, wanted_price){
208 console.log(body) 184 console.log(body)
209 }); 185 });
210 } 186 }
187 + }
188 + )
189 +
190 + // if (parseInt(result) <= wanted_price){
191 + // request.post(
192 + // {
193 + // url: TARGET_URL,
194 + // headers: {
195 + // 'Authorization': `Bearer ${TOKEN}`
196 + // },
197 + // json: {
198 + // "replyToken":replyToken,
199 + // "messages":[
200 + // {
201 + // "type":"text",
202 + // "text":"Reached wanted price!"
203 + // },
204 + // ]
205 + // }
206 + // },(error, response, body) => {
207 + // console.log(body)
208 + // });
209 + // }
211 210
212 211
213 212
......