2019102152 김다빈

Add lookup candle function

...@@ -2,64 +2,64 @@ const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fet ...@@ -2,64 +2,64 @@ const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fet
2 const url1 = 'https://api.upbit.com/v1/market/all'; 2 const url1 = 'https://api.upbit.com/v1/market/all';
3 3
4 const options = { method: 'GET', headers: { Accept: 'application/json' } }; 4 const options = { method: 'GET', headers: { Accept: 'application/json' } };
5 -const express=require('express'); 5 +const express = require('express');
6 -const app=express(); 6 +const app = express();
7 -const {Coin}=require("./models/Coin"); 7 +const { Coin } = require("./models/Coin");
8 8
9 - 9 +var sort_info = new Array();
10 -const mongoose=require('mongoose'); 10 +const mongoose = require('mongoose');
11 -const config=require('./config/key'); 11 +const config = require('./config/key');
12 -const connect = mongoose.connect(config.mongoURI,{ 12 +const connect = mongoose.connect(config.mongoURI, {
13 useNewUrlParser: true, useUnifiedTopology: true 13 useNewUrlParser: true, useUnifiedTopology: true
14 - }) 14 +})
15 - .then(()=>console.log('디비연결 성공')) 15 + .then(() => console.log('디비연결 성공'))
16 - .catch((err)=>console.log(err)); 16 + .catch((err) => console.log(err));
17 17
18 18
19 async function get_marketName() { 19 async function get_marketName() {
20 - var data=new Array(); 20 + var data = new Array();
21 //전체 암호화폐 리스트 불러오기 21 //전체 암호화폐 리스트 불러오기
22 - let response= await fetch(url1, options) 22 + let response = await fetch(url1, options)
23 .then(res => res.json()) 23 .then(res => res.json())
24 .then(json => { 24 .then(json => {
25 - for(i in json){ 25 + for (i in json) {
26 data.push(json[i].market); 26 data.push(json[i].market);
27 } 27 }
28 }) 28 })
29 return data; 29 return data;
30 } 30 }
31 -async function get_marketInfo(name_list){ 31 +async function get_marketInfo(name_list) {
32 //각 암호화폐 정보 조회 32 //각 암호화폐 정보 조회
33 - const url2=`https://api.upbit.com/v1/ticker/?markets=${name_list}`; 33 + const url2 = `https://api.upbit.com/v1/ticker/?markets=${name_list}`;
34 - var arr=new Array(); 34 + var arr = new Array();
35 - let response2=await fetch(url2,options) 35 + let response2 = await fetch(url2, options)
36 - .then(res=>res.json()) 36 + .then(res => res.json())
37 - .then(json=>{ 37 + .then(json => {
38 - for(i in json){ 38 + for (i in json) {
39 - if(json[i].acc_trade_price_24h>100000000000){ 39 + if (json[i].acc_trade_price_24h > 100000000000) {
40 - arr.push([json[i].market,json[i].acc_trade_price_24h,json[i].trade_price]); 40 + arr.push([json[i].market, json[i].acc_trade_price_24h, json[i].trade_price]);
41 } 41 }
42 } 42 }
43 }) 43 })
44 return arr 44 return arr
45 } 45 }
46 -async function sort_data(arr){ 46 +async function sort_data(arr) {
47 - arr.sort((a,b)=>{ 47 + arr.sort((a, b) => {
48 - return b[1]-a[1]; 48 + return b[1] - a[1];
49 }) 49 })
50 return arr; 50 return arr;
51 } 51 }
52 -async function save_coin(arr){ 52 +async function save_coin(arr) {
53 - for(var i=0;i<10;i++){ 53 + for (var i = 0; i < 10; i++) {
54 - if(arr[i]){ 54 + if (arr[i]) {
55 - const coin=new Coin({ 55 + const coin = new Coin({
56 - tid:i+1, 56 + tid: i + 1,
57 - name:arr[i][0], 57 + name: arr[i][0],
58 - acc_trade_price_24h:arr[i][1], 58 + acc_trade_price_24h: arr[i][1],
59 - current_price:arr[i][2] 59 + current_price: arr[i][2]
60 }); 60 });
61 - await coin.save((err)=>{ 61 + await coin.save((err) => {
62 - if(err){ 62 + if (err) {
63 console.log(err) 63 console.log(err)
64 } 64 }
65 }) 65 })
...@@ -67,14 +67,36 @@ async function save_coin(arr){ ...@@ -67,14 +67,36 @@ async function save_coin(arr){
67 } 67 }
68 return true; 68 return true;
69 } 69 }
70 -app.get('/get_market',async (req,res)=>{ 70 +async function get_candle(minute, market) {
71 - var name_list=(await get_marketName()); 71 + const url = `https://api.upbit.com/v1/candles/minutes/${minute}?market=${market}&count=1`;
72 - var market_info=(await get_marketInfo(name_list)); 72 + var candle = new Array();
73 - var sort_info=(await sort_data(market_info)); 73 + let response = await fetch(url, options)
74 - console.log(sort_info); 74 + .then(res => res.json())
75 + .then(json => candle = json)
76 + return candle;
77 +}
78 +
79 +app.get('/get_market', async (req, res) => {
80 + var name_list = (await get_marketName());
81 + var market_info = (await get_marketInfo(name_list));
82 + sort_info = (await sort_data(market_info));
75 res.json(await save_coin(sort_info)); 83 res.json(await save_coin(sort_info));
76 84
77 }) 85 })
78 -app.listen(5000,()=>{ 86 +app.get('/get_candle', async (req, res) => {
87 + Coin.find()
88 + .then(result => {
89 + result.forEach((item) => {
90 + get_candle(5, item.name)
91 + .then(result => {
92 + Coin.findOneAndUpdate({ name: result[0].market }, { five_candle: result[0].trade_price }, { new: true }, (err, doc) => {
93 + console.log(doc);
94 + })
95 + })
96 + })
97 + });
98 +
99 +})
100 +app.listen(5000, () => {
79 console.log('server') 101 console.log('server')
80 }) 102 })
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -15,6 +15,9 @@ const coinSchema=mongoose.Schema({ ...@@ -15,6 +15,9 @@ const coinSchema=mongoose.Schema({
15 current_price:{ 15 current_price:{
16 type:Number, 16 type:Number,
17 required:true 17 required:true
18 + },
19 + five_candle:{
20 + type:Number
18 } 21 }
19 }) 22 })
20 23
......
This diff is collapsed. Click to expand it.
...@@ -4,7 +4,8 @@ ...@@ -4,7 +4,8 @@
4 "description": "", 4 "description": "",
5 "main": "index.js", 5 "main": "index.js",
6 "scripts": { 6 "scripts": {
7 - "test": "echo \"Error: no test specified\" && exit 1" 7 + "test": "echo \"Error: no test specified\" && exit 1",
8 + "start":"node index.js"
8 }, 9 },
9 "repository": { 10 "repository": {
10 "type": "git", 11 "type": "git",
...@@ -14,6 +15,7 @@ ...@@ -14,6 +15,7 @@
14 "license": "ISC", 15 "license": "ISC",
15 "dependencies": { 16 "dependencies": {
16 "express": "^4.17.1", 17 "express": "^4.17.1",
18 + "mongoose": "^6.0.12",
17 "node-fetch": "^3.0.0" 19 "node-fetch": "^3.0.0"
18 }, 20 },
19 "devDependencies": { 21 "devDependencies": {
......