Ubuntu

Add necessary functions

......@@ -12,9 +12,6 @@ const axios = require('axios')
const cheerio = require('cheerio')
var app = express();
var cron = require('node-cron');
const fs = require('fs');
var app = express();
var stock_code = "005930"
app.use(bodyParser.json());
......@@ -26,7 +23,7 @@ app.post('/hook', function (req, res) {
let stockList = [];
import './stock_names.txt';
//import './stock_names.txt';
fs.readFile('./stock_names.txt', 'utf8', (err,data) => { // 파일 import하고 stockList로 저장
if (err){
......@@ -48,31 +45,14 @@ app.post('/hook', function (req, res) {
console.log('[request source] ', eventObj.source);
console.log('[request message]', eventObj.message);
<<<<<<< HEAD
var s_code = get_stock_code(stockList,stock_code,eventObj.replyToken,eventObj.message.text);
var wanted_price = get_wanted_price(eventObj.replyToken,eventObj.message.text);
cron.schedule('*/5 * * * *', () => {
const temp = axios.get(`https://finance.naver.com/item/main.nhn?code=${stock_code}%22`).then(
res => {
let urList = [];
const $ = cheerio.load(res.data)
const $bodyList = $("dl.blind").children("dd").text();
const result = $bodyList.slice(73, 80) // 실시간으로 변하는 주식 가격 scrape
console.log(result)
}
)
compare(s_code, wanted_price);
});
=======
for (let i = 0; i<stockList.length; i++) {
if (eventObj.message.text == stockList[i][1]){ // 입력한거 나오면 코드로 바꿔줌
stock_code = stockList[i][0]
}
else{
console.log('Not in list');
}
}
// if (eventObj.message.text == "영어"){
// lan = "en"
......@@ -86,7 +66,42 @@ app.post('/hook', function (req, res) {
// else{
// trans(eventObj.replyToken, eventObj.message.text);
// }
>>>>>>> stock_list
// request.post(
// {
// url: TARGET_URL,
// headers: {
// 'Authorization': `Bearer ${TOKEN}`
// },
// json: {
// "replyToken":eventObj.replyToken,
// "messages":[
// {
// "type":"text",
// "text":"Hello, user"
// },
// {
// "type":"text",
// "text":"May I help you?"
// }
// ]
// }
// },(error, response, body) => {
// console.log(body)
// });
res.sendStatus(200);
});
function get_stock_code(stockList,stock_code,replyToken,message){
for (let i = 0; i<stockList.length; i++) {
if (eventObj.message.text == stockList[i][1]){ // 입력한거 나오면 코드로 바꿔줌
stock_code = stockList[i][0]
return stock_code
}
else{ // stockList에 없으면 없다고 보냄
request.post(
{
url: TARGET_URL,
......@@ -98,21 +113,78 @@ app.post('/hook', function (req, res) {
"messages":[
{
"type":"text",
"text":"Hello, user"
"text":"Not in list"
},
]
}
},(error, response, body) => {
console.log(body)
});
}
}
}
function get_wanted_price(replyToken,message){
if (message < 0){
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken":eventObj.replyToken,
"messages":[
{
"type":"text",
"text":"May I help you?"
"text":"Price must be positive number"
},
]
}
},(error, response, body) => {
console.log(body)
});
}
}
function compare(s_code, wanted_price){
const temp = axios.get(`https://finance.naver.com/item/main.nhn?code=${s_code}%22`).then(
res => {
let urList = [];
const $ = cheerio.load(res.data)
const $bodyList = $("dl.blind").children("dd").text();
const result = $bodyList.slice(73, 80) // 실시간으로 변하는 주식 가격 scrape
console.log(result)
}
)
if (int(result) <= wanted_price){
request.post(
{
url: TARGET_URL,
headers: {
'Authorization': `Bearer ${TOKEN}`
},
json: {
"replyToken":eventObj.replyToken,
"messages":[
{
"type":"text",
"text":"Reached wanted price!"
},
]
}
},(error, response, body) => {
console.log(body)
});
}
res.sendStatus(200);
});
}
try {
......
......@@ -12,7 +12,8 @@
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node app.js"
},
"repository": {
"type": "git",
......