전현진

Add line.js to print in Line App

Showing 1 changed file with 198 additions and 0 deletions
1 +var express = require('express');
2 +const request = require('request');const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
3 +const TOKEN = '/* 수정 */';
4 +const fs = require('fs');
5 +const path = require('path');
6 +const HTTPS = require('https');
7 +const fetch = require('node-fetch');
8 +const rp = require('request-promise');
9 +const domain = "/* 수정 */"
10 +const sslport = 23023;
11 +const bodyParser = require('body-parser');
12 +var app = express();
13 +var command = '';
14 +app.use(bodyParser.json());
15 +
16 +
17 +app.post('/hook', function (req, res) {
18 +
19 + var eventObj = req.body.events[0];
20 + var source = eventObj.source;
21 + var message = eventObj.message;
22 + // request log
23 + console.log('======================', new Date() ,'======================');
24 + console.log('[request]', req.body);
25 + console.log('[request source] ', source);
26 + console.log('[request message]', message);
27 +
28 + /*=============================================================*/
29 +
30 + if(message.text === '거래량비율')
31 + volumePumpingRatioThreeMinutePerWeek(eventObj.replyToken, message.text);
32 +
33 + /*=============================================================*/
34 +
35 + res.sendStatus(200);
36 +});
37 +
38 +function volumePumpingRatioThreeMinutePerWeek(replyToken, message)
39 +{
40 + fs.readFile('coin_name.txt', 'utf8', function(err,data){
41 + coinNameArr = data.split(",");
42 +
43 +
44 + var volumePerWeekArr = new Array();
45 + function getWeekVolume()
46 + {
47 + var promise = new Promise(function(resolve, reject)
48 + {
49 + for (i = 0; i < coinNameArr.length; ++i)
50 + {
51 + (function (i)
52 + {
53 + setTimeout(function () {
54 + const url = 'https://api.upbit.com/v1/candles/weeks/?market='+coinNameArr[i]+'&count=1';
55 + const options = {method: 'GET', headers: {Accept: 'application/json'}};
56 + var coinJson = new Object();
57 + fetch(url, options)
58 + .then(res => res.json())
59 + .then(json => {
60 + coinJson.coin = coinNameArr[i];
61 + coinJson.tradeVolumePerWeek = json[0].candle_acc_trade_volume;
62 + volumePerWeekArr.push(coinJson);
63 + console.log(coinJson);
64 + if (volumePerWeekArr.length >= coinNameArr.length)
65 + resolve(volumePerWeekArr);
66 + })
67 + .catch(err => console.error('error:' + err));
68 + }, i*220);
69 + })(i);
70 + }
71 + });
72 + return promise;
73 + }
74 +
75 +
76 + var volumePerThreeMinArr = new Array();
77 + function getThreeMinVolume()
78 + {
79 + var promise = new Promise(function(resolve, reject)
80 + {
81 + for (i = 0; i < coinNameArr.length; ++i)
82 + {
83 + (function (i)
84 + {
85 + setTimeout(function () {
86 + const url = 'https://api.upbit.com/v1/candles/minutes/3?market='+coinNameArr[i]+'&count=1';
87 + const options = {method: 'GET', headers: {Accept: 'application/json'}};
88 + var coinJson = new Object();
89 + fetch(url, options)
90 + .then(res => res.json())
91 + .then(json => {
92 + coinJson.coin = coinNameArr[i];
93 + coinJson.tradeVolumePerThreeMin = json[0].candle_acc_trade_volume;
94 + volumePerThreeMinArr.push(coinJson);
95 + console.log(coinJson);
96 + if (volumePerThreeMinArr.length >= coinNameArr.length)
97 + resolve(volumePerThreeMinArr);
98 + })
99 + .catch(err => console.error('error:' + err));
100 + }, i*220);
101 + })(i);
102 + }
103 +
104 + });
105 + return promise;
106 + }
107 +
108 +
109 + var pumpingArr = new Array();
110 + function getResultArr(volArr, minArr)
111 + {
112 + var promise = new Promise(function(resolve, reject)
113 + {
114 + for (var i = 0; i < coinNameArr.length; i++)
115 + {
116 + var coinJson = new Object();
117 + coinJson.name = coinNameArr[i];
118 + coinJson.pumpingRatio = minArr[i].tradeVolumePerThreeMin / volArr[i].tradeVolumePerWeek * 1000000
119 + pumpingArr.push(coinJson);
120 + if (pumpingArr.length >= coinNameArr.length)
121 + resolve(pumpingArr);
122 + }
123 + })
124 + return promise;
125 + }
126 +
127 + function sortingArr(data, ratio)
128 + {
129 + var promise = new Promise(function(resolve, reject)
130 + {
131 + resolve(data.sort(function(first, second)
132 + {
133 + var a = first[ratio];
134 + var b = second[ratio];
135 +
136 + return a > b ? -1 : a < b ? 1 : 0;
137 + }))
138 + });
139 +
140 + return promise;
141 + };
142 +
143 + getWeekVolume().then(function(volArr){
144 + getThreeMinVolume().then(function(minArr){
145 + getResultArr(volArr, minArr).then(function(result){
146 + sortingArr(result, "pumpingRatio").then(function(finalArr){
147 +
148 + console.log(finalArr);
149 + request.post(
150 + {
151 + url: TARGET_URL,
152 + headers: {
153 + 'Authorization': `Bearer ${TOKEN}`
154 + },
155 + json: {
156 + "replyToken":replyToken,
157 + "messages": [
158 + {
159 + "type":"text",
160 + "text": finalArr[0].name + ' : ' + String(finalArr[0].pumpingRatio) + '\n'
161 + + finalArr[1].name + ' : ' + String(finalArr[1].pumpingRatio) + '\n'
162 + + finalArr[2].name + ' : ' + String(finalArr[2].pumpingRatio) + '\n'
163 + + finalArr[3].name + ' : ' + String(finalArr[3].pumpingRatio) + '\n'
164 + + finalArr[4].name + ' : ' + String(finalArr[4].pumpingRatio) + '\n'
165 + + finalArr[5].name + ' : ' + String(finalArr[5].pumpingRatio) + '\n'
166 + + finalArr[6].name + ' : ' + String(finalArr[6].pumpingRatio) + '\n'
167 + + finalArr[7].name + ' : ' + String(finalArr[7].pumpingRatio) + '\n'
168 + + finalArr[8].name + ' : ' + String(finalArr[8].pumpingRatio) + '\n'
169 + + finalArr[9].name + ' : ' + String(finalArr[9].pumpingRatio) + '\n'
170 + }
171 + ]
172 + }
173 + },(error, response, body) => {
174 + console.log(body)
175 + });
176 + res.sendStatus(200);
177 +
178 + })
179 + })
180 + })
181 + })
182 + })
183 +}
184 +
185 +try {
186 + const option = {
187 + ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
188 + key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
189 + cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
190 + };
191 +
192 + HTTPS.createServer(option, app).listen(sslport, () => {
193 + console.log(`[HTTPS] Server is started on port ${sslport}`);
194 + });
195 + } catch (error) {
196 + console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
197 + console.log(error);
198 + }
...\ No newline at end of file ...\ No newline at end of file