Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿ›fix: WSPrice-approvalKeycache์ฝ”๋“œ ์ถ”๊ฐ€ #33

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions src/utils/WSPrice.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
const axios = require('axios');
const WebSocket = require('ws');
require("dotenv").config();
const app = require('../../app');
const appkey = process.env.KIS_APP_KEY;
const appsecret = process.env.KIS_SECRET_KEY;

let approvalKeyCache = null;
let approvalKeyExpiration = null;

async function getApproval(key, secret) {
if (approvalKeyCache && approvalKeyExpiration > Date.now()) {
return approvalKeyCache;
}

const url = 'https://openapi.koreainvestment.com:9443/oauth2/Approval';
const headers = { "content-type": "application/json" };
const body = {
Expand All @@ -16,16 +22,19 @@ async function getApproval(key, secret) {

try {
const response = await axios.post(url, body, { headers });
return response.data.approval_key;
approvalKeyCache = response.data.approval_key;
approvalKeyExpiration = Date.now() + 3600 * 1000; // Set expiration time (1 hour)
return approvalKeyCache;
} catch (error) {
console.error('Error getting approval key:', error.response ? error.response.data : error.message);
return null;
}
}

async function wsdata(ws, id) {
const stockId = String(id);
const approvalKey = await getApproval(appkey,appsecret);
const approvalKey = await getApproval(appkey, appsecret);

const payload = {
"header": {
"approval_key": approvalKey,
Expand Down Expand Up @@ -91,18 +100,18 @@ async function wsdata(ws, id) {
wsConnection.close();
});
}
function stockspurchase(data_cnt, data, ws) {

const menulist = "์œ ๊ฐ€์ฆ๊ถŒ๋‹จ์ถ•์ข…๋ชฉ์ฝ”๋“œ|์ฃผ์‹์ฒด๊ฒฐ์‹œ๊ฐ„|์ฃผ์‹ํ˜„์žฌ๊ฐ€|์ „์ผ๋Œ€๋น„๋ถ€ํ˜ธ|์ „์ผ๋Œ€๋น„|์ „์ผ๋Œ€๋น„์œจ|๊ฐ€์ค‘ํ‰๊ท ์ฃผ์‹๊ฐ€๊ฒฉ|์ฃผ์‹์‹œ๊ฐ€|์ฃผ์‹์ตœ๊ณ ๊ฐ€|์ฃผ์‹์ตœ์ €๊ฐ€|๋งค๋„ํ˜ธ๊ฐ€1|๋งค์ˆ˜ํ˜ธ๊ฐ€1|์ฒด๊ฒฐ๊ฑฐ๋ž˜๋Ÿ‰|๋ˆ„์ ๊ฑฐ๋ž˜๋Ÿ‰|๋ˆ„์ ๊ฑฐ๋ž˜๋Œ€๊ธˆ|๋งค๋„์ฒด๊ฒฐ๊ฑด์ˆ˜|๋งค์ˆ˜์ฒด๊ฒฐ๊ฑด์ˆ˜|์ˆœ๋งค์ˆ˜์ฒด๊ฒฐ๊ฑด์ˆ˜|์ฒด๊ฒฐ๊ฐ•๋„|์ด๋งค๋„์ˆ˜๋Ÿ‰|์ด๋งค์ˆ˜์ˆ˜๋Ÿ‰|์ฒด๊ฒฐ๊ตฌ๋ถ„|๋งค์ˆ˜๋น„์œจ|์ „์ผ๊ฑฐ๋ž˜๋Ÿ‰๋Œ€๋น„๋“ฑ๋ฝ์œจ|์‹œ๊ฐ€์‹œ๊ฐ„|์‹œ๊ฐ€๋Œ€๋น„๊ตฌ๋ถ„|์‹œ๊ฐ€๋Œ€๋น„|์ตœ๊ณ ๊ฐ€์‹œ๊ฐ„|๊ณ ๊ฐ€๋Œ€๋น„๊ตฌ๋ถ„|๊ณ ๊ฐ€๋Œ€๋น„|์ตœ์ €๊ฐ€์‹œ๊ฐ„|์ €๊ฐ€๋Œ€๋น„๊ตฌ๋ถ„|์ €๊ฐ€๋Œ€๋น„|์˜์—…์ผ์ž|์‹ ์žฅ์šด์˜๊ตฌ๋ถ„์ฝ”๋“œ|๊ฑฐ๋ž˜์ •์ง€์—ฌ๋ถ€|๋งค๋„ํ˜ธ๊ฐ€์ž”๋Ÿ‰|๋งค์ˆ˜ํ˜ธ๊ฐ€์ž”๋Ÿ‰|์ด๋งค๋„ํ˜ธ๊ฐ€์ž”๋Ÿ‰|์ด๋งค์ˆ˜ํ˜ธ๊ฐ€์ž”๋Ÿ‰|๊ฑฐ๋ž˜๋Ÿ‰ํšŒ์ „์œจ|์ „์ผ๋™์‹œ๊ฐ„๋ˆ„์ ๊ฑฐ๋ž˜๋Ÿ‰|์ „์ผ๋™์‹œ๊ฐ„๋ˆ„์ ๊ฑฐ๋ž˜๋Ÿ‰๋น„์œจ|์‹œ๊ฐ„๊ตฌ๋ถ„์ฝ”๋“œ|์ž„์˜์ข…๋ฃŒ๊ตฌ๋ถ„์ฝ”๋“œ|์ •์ VI๋ฐœ๋™๊ธฐ์ค€๊ฐ€";
const menustr = menulist.split('|');
const pValue = data.split('^');
const obj = {};
for (let i = 0; i < 6; i++) {
let menu = menustr[i];
obj[menu] = pValue[i];
}
ws.send(JSON.stringify(obj, null, 2));
}

function stockspurchase(data_cnt, data, ws) {
const menulist = "์œ ๊ฐ€์ฆ๊ถŒ๋‹จ์ถ•์ข…๋ชฉ์ฝ”๋“œ|์ฃผ์‹์ฒด๊ฒฐ์‹œ๊ฐ„|์ฃผ์‹ํ˜„์žฌ๊ฐ€|์ „์ผ๋Œ€๋น„๋ถ€ํ˜ธ|์ „์ผ๋Œ€๋น„|์ „์ผ๋Œ€๋น„์œจ|๊ฐ€์ค‘ํ‰๊ท ์ฃผ์‹๊ฐ€๊ฒฉ|์ฃผ์‹์‹œ๊ฐ€|์ฃผ์‹์ตœ๊ณ ๊ฐ€|์ฃผ์‹์ตœ์ €๊ฐ€|๋งค๋„ํ˜ธ๊ฐ€1|๋งค์ˆ˜ํ˜ธ๊ฐ€1|์ฒด๊ฒฐ๊ฑฐ๋ž˜๋Ÿ‰|๋ˆ„์ ๊ฑฐ๋ž˜๋Ÿ‰|๋ˆ„์ ๊ฑฐ๋ž˜๋Œ€๊ธˆ|๋งค๋„์ฒด๊ฒฐ๊ฑด์ˆ˜|๋งค์ˆ˜์ฒด๊ฒฐ๊ฑด์ˆ˜|์ˆœ๋งค์ˆ˜์ฒด๊ฒฐ๊ฑด์ˆ˜|์ฒด๊ฒฐ๊ฐ•๋„|์ด๋งค๋„์ˆ˜๋Ÿ‰|์ด๋งค์ˆ˜์ˆ˜๋Ÿ‰|์ฒด๊ฒฐ๊ตฌ๋ถ„|๋งค์ˆ˜๋น„์œจ|์ „์ผ๊ฑฐ๋ž˜๋Ÿ‰๋Œ€๋น„๋“ฑ๋ฝ์œจ|์‹œ๊ฐ€์‹œ๊ฐ„|์‹œ๊ฐ€๋Œ€๋น„๊ตฌ๋ถ„|์‹œ๊ฐ€๋Œ€๋น„|์ตœ๊ณ ๊ฐ€์‹œ๊ฐ„|๊ณ ๊ฐ€๋Œ€๋น„๊ตฌ๋ถ„|๊ณ ๊ฐ€๋Œ€๋น„|์ตœ์ €๊ฐ€์‹œ๊ฐ„|์ €๊ฐ€๋Œ€๋น„๊ตฌ๋ถ„|์ €๊ฐ€๋Œ€๋น„|์˜์—…์ผ์ž|์‹ ์žฅ์šด์˜๊ตฌ๋ถ„์ฝ”๋“œ|๊ฑฐ๋ž˜์ •์ง€์—ฌ๋ถ€|๋งค๋„ํ˜ธ๊ฐ€์ž”๋Ÿ‰|๋งค์ˆ˜ํ˜ธ๊ฐ€์ž”๋Ÿ‰|์ด๋งค๋„ํ˜ธ๊ฐ€์ž”๋Ÿ‰|์ด๋งค์ˆ˜ํ˜ธ๊ฐ€์ž”๋Ÿ‰|๊ฑฐ๋ž˜๋Ÿ‰ํšŒ์ „์œจ|์ „์ผ๋™์‹œ๊ฐ„๋ˆ„์ ๊ฑฐ๋ž˜๋Ÿ‰|์ „์ผ๋™์‹œ๊ฐ„๋ˆ„์ ๊ฑฐ๋ž˜๋Ÿ‰๋น„์œจ|์‹œ๊ฐ„๊ตฌ๋ถ„์ฝ”๋“œ|์ž„์˜์ข…๋ฃŒ๊ตฌ๋ถ„์ฝ”๋“œ|์ •์ VI๋ฐœ๋™๊ธฐ์ค€๊ฐ€";
const menustr = menulist.split('|');
const pValue = data.split('^');
const obj = {};
for (let i = 0; i < 6; i++) {
let menu = menustr[i];
obj[menu] = pValue[i];
}
console.log(obj)
ws.send(JSON.stringify(obj, null, 2));
}

module.exports = {wsdata}
module.exports = { wsdata }