Showing
2 changed files
with
103 additions
and
30 deletions
... | @@ -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,7 +66,42 @@ app.post('/hook', function (req, res) { | ... | @@ -86,7 +66,42 @@ 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 | + |
70 | + | ||
71 | + // request.post( | ||
72 | + // { | ||
73 | + // url: TARGET_URL, | ||
74 | + // headers: { | ||
75 | + // 'Authorization': `Bearer ${TOKEN}` | ||
76 | + // }, | ||
77 | + // json: { | ||
78 | + // "replyToken":eventObj.replyToken, | ||
79 | + // "messages":[ | ||
80 | + // { | ||
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에 없으면 없다고 보냄 | ||
90 | request.post( | 105 | request.post( |
91 | { | 106 | { |
92 | url: TARGET_URL, | 107 | url: TARGET_URL, |
... | @@ -98,21 +113,78 @@ app.post('/hook', function (req, res) { | ... | @@ -98,21 +113,78 @@ app.post('/hook', function (req, res) { |
98 | "messages":[ | 113 | "messages":[ |
99 | { | 114 | { |
100 | "type":"text", | 115 | "type":"text", |
101 | - "text":"Hello, user" | 116 | + "text":"Not in list" |
102 | }, | 117 | }, |
118 | + ] | ||
119 | + } | ||
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":[ | ||
103 | { | 139 | { |
104 | "type":"text", | 140 | "type":"text", |
105 | - "text":"May I help you?" | 141 | + "text":"Price must be positive number" |
142 | + }, | ||
143 | + ] | ||
106 | } | 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) | ||
160 | + } | ||
161 | + ) | ||
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 | + }, | ||
107 | ] | 177 | ] |
108 | } | 178 | } |
109 | },(error, response, body) => { | 179 | },(error, response, body) => { |
110 | console.log(body) | 180 | console.log(body) |
111 | }); | 181 | }); |
182 | + } | ||
112 | 183 | ||
113 | 184 | ||
114 | - res.sendStatus(200); | 185 | + |
115 | -}); | 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", | ... | ... |
-
Please register or login to post a comment