Ubuntu

Add necessary functions

...@@ -12,9 +12,6 @@ const axios = require('axios') ...@@ -12,9 +12,6 @@ const axios = require('axios')
12 const cheerio = require('cheerio') 12 const cheerio = require('cheerio')
13 var app = express(); 13 var app = express();
14 var cron = require('node-cron'); 14 var cron = require('node-cron');
15 -
16 -const fs = require('fs');
17 -var app = express();
18 var stock_code = "005930" 15 var stock_code = "005930"
19 app.use(bodyParser.json()); 16 app.use(bodyParser.json());
20 17
...@@ -26,7 +23,7 @@ app.post('/hook', function (req, res) { ...@@ -26,7 +23,7 @@ app.post('/hook', function (req, res) {
26 23
27 24
28 let stockList = []; 25 let stockList = [];
29 - import './stock_names.txt'; 26 + //import './stock_names.txt';
30 27
31 fs.readFile('./stock_names.txt', 'utf8', (err,data) => { // 파일 import하고 stockList로 저장 28 fs.readFile('./stock_names.txt', 'utf8', (err,data) => { // 파일 import하고 stockList로 저장
32 if (err){ 29 if (err){
...@@ -48,31 +45,14 @@ app.post('/hook', function (req, res) { ...@@ -48,31 +45,14 @@ app.post('/hook', function (req, res) {
48 console.log('[request source] ', eventObj.source); 45 console.log('[request source] ', eventObj.source);
49 console.log('[request message]', eventObj.message); 46 console.log('[request message]', eventObj.message);
50 47
51 -<<<<<<< HEAD 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);
52 50
53 cron.schedule('*/5 * * * *', () => { 51 cron.schedule('*/5 * * * *', () => {
54 - const temp = axios.get(`https://finance.naver.com/item/main.nhn?code=${stock_code}%22`).then( 52 + compare(s_code, wanted_price);
55 - res => {
56 - let urList = [];
57 - const $ = cheerio.load(res.data)
58 - const $bodyList = $("dl.blind").children("dd").text();
59 - const result = $bodyList.slice(73, 80) // 실시간으로 변하는 주식 가격 scrape
60 - console.log(result)
61 - }
62 - )
63 }); 53 });
64 54
65 55
66 -=======
67 - for (let i = 0; i<stockList.length; i++) {
68 - if (eventObj.message.text == stockList[i][1]){ // 입력한거 나오면 코드로 바꿔줌
69 - stock_code = stockList[i][0]
70 - }
71 - else{
72 - console.log('Not in list');
73 - }
74 -
75 - }
76 56
77 // if (eventObj.message.text == "영어"){ 57 // if (eventObj.message.text == "영어"){
78 // lan = "en" 58 // lan = "en"
...@@ -86,33 +66,125 @@ app.post('/hook', function (req, res) { ...@@ -86,33 +66,125 @@ app.post('/hook', function (req, res) {
86 // else{ 66 // else{
87 // trans(eventObj.replyToken, eventObj.message.text); 67 // trans(eventObj.replyToken, eventObj.message.text);
88 // } 68 // }
89 ->>>>>>> stock_list 69 +
90 - request.post( 70 +
91 - { 71 + // request.post(
92 - url: TARGET_URL, 72 + // {
93 - headers: { 73 + // url: TARGET_URL,
94 - 'Authorization': `Bearer ${TOKEN}` 74 + // headers: {
95 - }, 75 + // 'Authorization': `Bearer ${TOKEN}`
96 - json: { 76 + // },
97 - "replyToken":eventObj.replyToken, 77 + // json: {
98 - "messages":[ 78 + // "replyToken":eventObj.replyToken,
99 - { 79 + // "messages":[
100 - "type":"text", 80 + // {
101 - "text":"Hello, user" 81 + // "type":"text",
82 + // "text":"Hello, user"
83 + // },
84 + // {
85 + // "type":"text",
86 + // "text":"May I help you?"
87 + // }
88 + // ]
89 + // }
90 + // },(error, response, body) => {
91 + // console.log(body)
92 + // });
93 +
94 +
95 + res.sendStatus(200);
96 +});
97 +
98 +function get_stock_code(stockList,stock_code,replyToken,message){
99 + for (let i = 0; i<stockList.length; i++) {
100 + if (eventObj.message.text == stockList[i][1]){ // 입력한거 나오면 코드로 바꿔줌
101 + stock_code = stockList[i][0]
102 + return stock_code
103 + }
104 + else{ // stockList에 없으면 없다고 보냄
105 + request.post(
106 + {
107 + url: TARGET_URL,
108 + headers: {
109 + 'Authorization': `Bearer ${TOKEN}`
102 }, 110 },
103 - { 111 + json: {
104 - "type":"text", 112 + "replyToken":eventObj.replyToken,
105 - "text":"May I help you?" 113 + "messages":[
114 + {
115 + "type":"text",
116 + "text":"Not in list"
117 + },
118 + ]
106 } 119 }
107 - ] 120 + },(error, response, body) => {
121 + console.log(body)
122 + });
123 + }
124 +
125 + }
126 +}
127 +
128 +function get_wanted_price(replyToken,message){
129 + if (message < 0){
130 + request.post(
131 + {
132 + url: TARGET_URL,
133 + headers: {
134 + 'Authorization': `Bearer ${TOKEN}`
135 + },
136 + json: {
137 + "replyToken":eventObj.replyToken,
138 + "messages":[
139 + {
140 + "type":"text",
141 + "text":"Price must be positive number"
142 + },
143 + ]
144 + }
145 + },(error, response, body) => {
146 + console.log(body)
147 + });
148 + }
149 +
150 +}
151 +
152 +function compare(s_code, wanted_price){
153 + const temp = axios.get(`https://finance.naver.com/item/main.nhn?code=${s_code}%22`).then(
154 + res => {
155 + let urList = [];
156 + const $ = cheerio.load(res.data)
157 + const $bodyList = $("dl.blind").children("dd").text();
158 + const result = $bodyList.slice(73, 80) // 실시간으로 변하는 주식 가격 scrape
159 + console.log(result)
108 } 160 }
109 - },(error, response, body) => { 161 + )
110 - console.log(body)
111 - });
112 162
163 + if (int(result) <= wanted_price){
164 + request.post(
165 + {
166 + url: TARGET_URL,
167 + headers: {
168 + 'Authorization': `Bearer ${TOKEN}`
169 + },
170 + json: {
171 + "replyToken":eventObj.replyToken,
172 + "messages":[
173 + {
174 + "type":"text",
175 + "text":"Reached wanted price!"
176 + },
177 + ]
178 + }
179 + },(error, response, body) => {
180 + console.log(body)
181 + });
182 + }
113 183
114 - res.sendStatus(200); 184 +
115 -}); 185 +
186 +
187 +}
116 188
117 189
118 try { 190 try {
......
...@@ -12,7 +12,8 @@ ...@@ -12,7 +12,8 @@
12 }, 12 },
13 "devDependencies": {}, 13 "devDependencies": {},
14 "scripts": { 14 "scripts": {
15 - "test": "echo \"Error: no test specified\" && exit 1" 15 + "test": "echo \"Error: no test specified\" && exit 1",
16 + "start": "node app.js"
16 }, 17 },
17 "repository": { 18 "repository": {
18 "type": "git", 19 "type": "git",
......