정홍주

Revert "Add Replying Upbit coin price"

This reverts commit 96e1f832
Showing 1 changed file with 34 additions and 76 deletions
1 var express = require('express'); 1 var express = require('express');
2 -const request = require('request');const TARGET_URL = 'https://api.line.me/v2/bot/message/reply'
3 -const TOKEN = '6lwsXwectIjTZ1NWp6c1t5l8nXaOjWgfeYfTOWNRRTVftaXMb4eSMj2zeficMr4/FWtyPPBuUB120kxZp58bwDilssdU5Uhc4mWz4kCPfcgiEcVwzXUktbYRE3xKAJU1zdYAsZZvI+87KY8qZvaRngdB04t89/1O/w1cDnyilFU='
4 -const fs = require('fs');
5 -const path = require('path');
6 -const HTTPS = require('https');
7 -const fetch = require('node-fetch');
8 -const domain = "2018102235.oss2021.tk"
9 -const sslport = 23023;
10 -const bodyParser = require('body-parser');
11 var app = express(); 2 var app = express();
12 -var command = ''; 3 +var fs = require('fs');
4 +const fetch = require('node-fetch');
5 +var bodyParser = require('body-parser');
6 +app.use(bodyParser.urlencoded({ extended: false }));
13 app.use(bodyParser.json()); 7 app.use(bodyParser.json());
14 -app.post('/hook', function (req, res) {
15 -
16 - var eventObj = req.body.events[0];
17 - var source = eventObj.source;
18 - var message = eventObj.message;
19 - // request log
20 - console.log('======================', new Date() ,'======================');
21 - console.log('[request]', req.body);
22 - console.log('[request source] ', source);
23 - console.log('[request message]', message);
24 - if(message.text === '김프'){
25 - command = '김프';
26 - }
27 - else{
28 - if(command === '김프'){
29 - kimp(eventObj.replyToken, message.text);
30 - }
31 - }
32 8
33 - 9 +app.get('/price', function(req, res) {
34 - 10 + fs.readFile('coin_name.txt', 'utf8', function(err,data){
35 - res.sendStatus(200); 11 + arr = data.split(",");
36 -}); 12 + // console.log(arr);
37 - 13 + var responseList = new Array();
38 -function kimp(replyToken,message){ 14 + function print_coin(){
39 -
40 - var upbitPrice = 0;
41 - function korean_price(){
42 var promise = new Promise(function(resolve,reject){ 15 var promise = new Promise(function(resolve,reject){
43 - const url = 'https://api.upbit.com/v1/candles/minutes/1?market=KRW-'+message+'&count=1'; 16 + for(j=0;j<arr.length/10;j++){
17 + for(i=j*10;i<(j+1)*10;i++){
18 + (function(i){
19 + setTimeout(function(){
20 + const url = 'https://api.upbit.com/v1/candles/minutes/1?market='+arr[i]+'&count=1';
44 const options = {method: 'GET', headers: {Accept: 'application/json'}}; 21 const options = {method: 'GET', headers: {Accept: 'application/json'}};
22 + var coinJson = new Object();
45 fetch(url, options) 23 fetch(url, options)
46 .then(res => res.json()) 24 .then(res => res.json())
47 .then(json => { 25 .then(json => {
48 - upbitPrice = json[0].trade_price; 26 + coinJson.coin = arr[i];
49 - resolve(upbitPrice); 27 + coinJson.price = json[0].trade_price;
28 + responseList.push(coinJson);
29 + console.log(i)
30 + if(responseList.length>116){
31 + resolve(responseList);
32 + }
50 }) 33 })
51 .catch(err => console.error('error:' + err)); 34 .catch(err => console.error('error:' + err));
52 - }); 35 + },i*110);
53 - return promise; 36 + })(i);
54 } 37 }
55 - korean_price().then(function(upbitPrice){
56 - request.post(
57 - {
58 - url: TARGET_URL,
59 - headers: {
60 - 'Authorization': `Bearer ${TOKEN}`
61 - },
62 - json: {
63 - "replyToken":replyToken,
64 - "messages":[
65 - {
66 - "type":"text",
67 - "text":String(upbitPrice)
68 - },
69 - ]
70 } 38 }
71 - },(error, response, body) => {
72 - console.log(body)
73 - });
74 - res.sendStatus(200);
75 - });
76 -}
77 -
78 39
79 -try {
80 - const option = {
81 - ca: fs.readFileSync('/etc/letsencrypt/live/' + domain +'/fullchain.pem'),
82 - key: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/privkey.pem'), 'utf8').toString(),
83 - cert: fs.readFileSync(path.resolve(process.cwd(), '/etc/letsencrypt/live/' + domain +'/cert.pem'), 'utf8').toString(),
84 - };
85 -
86 - HTTPS.createServer(option, app).listen(sslport, () => {
87 - console.log(`[HTTPS] Server is started on port ${sslport}`);
88 }); 40 });
89 - } catch (error) { 41 + return promise;
90 - console.log('[HTTPS] HTTPS 오류가 발생하였습니다. HTTPS 서버는 실행되지 않습니다.');
91 - console.log(error);
92 } 42 }
43 + print_coin().then(function(resList){
44 + res.send(resList);
45 + });
46 + })
47 +})
48 +var server = app.listen(8082);
49 +console.log("Server Created..");
50 +
......