2019102152 김다빈

Add lookup candle function

......@@ -2,64 +2,64 @@ const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fet
const url1 = 'https://api.upbit.com/v1/market/all';
const options = { method: 'GET', headers: { Accept: 'application/json' } };
const express=require('express');
const app=express();
const {Coin}=require("./models/Coin");
const express = require('express');
const app = express();
const { Coin } = require("./models/Coin");
const mongoose=require('mongoose');
const config=require('./config/key');
const connect = mongoose.connect(config.mongoURI,{
var sort_info = new Array();
const mongoose = require('mongoose');
const config = require('./config/key');
const connect = mongoose.connect(config.mongoURI, {
useNewUrlParser: true, useUnifiedTopology: true
})
.then(()=>console.log('디비연결 성공'))
.catch((err)=>console.log(err));
})
.then(() => console.log('디비연결 성공'))
.catch((err) => console.log(err));
async function get_marketName() {
var data=new Array();
var data = new Array();
//전체 암호화폐 리스트 불러오기
let response= await fetch(url1, options)
let response = await fetch(url1, options)
.then(res => res.json())
.then(json => {
for(i in json){
for (i in json) {
data.push(json[i].market);
}
})
return data;
}
async function get_marketInfo(name_list){
async function get_marketInfo(name_list) {
//각 암호화폐 정보 조회
const url2=`https://api.upbit.com/v1/ticker/?markets=${name_list}`;
var arr=new Array();
let response2=await fetch(url2,options)
.then(res=>res.json())
.then(json=>{
for(i in json){
if(json[i].acc_trade_price_24h>100000000000){
arr.push([json[i].market,json[i].acc_trade_price_24h,json[i].trade_price]);
const url2 = `https://api.upbit.com/v1/ticker/?markets=${name_list}`;
var arr = new Array();
let response2 = await fetch(url2, options)
.then(res => res.json())
.then(json => {
for (i in json) {
if (json[i].acc_trade_price_24h > 100000000000) {
arr.push([json[i].market, json[i].acc_trade_price_24h, json[i].trade_price]);
}
}
})
return arr
}
async function sort_data(arr){
arr.sort((a,b)=>{
return b[1]-a[1];
async function sort_data(arr) {
arr.sort((a, b) => {
return b[1] - a[1];
})
return arr;
}
async function save_coin(arr){
for(var i=0;i<10;i++){
if(arr[i]){
const coin=new Coin({
tid:i+1,
name:arr[i][0],
acc_trade_price_24h:arr[i][1],
current_price:arr[i][2]
async function save_coin(arr) {
for (var i = 0; i < 10; i++) {
if (arr[i]) {
const coin = new Coin({
tid: i + 1,
name: arr[i][0],
acc_trade_price_24h: arr[i][1],
current_price: arr[i][2]
});
await coin.save((err)=>{
if(err){
await coin.save((err) => {
if (err) {
console.log(err)
}
})
......@@ -67,14 +67,36 @@ async function save_coin(arr){
}
return true;
}
app.get('/get_market',async (req,res)=>{
var name_list=(await get_marketName());
var market_info=(await get_marketInfo(name_list));
var sort_info=(await sort_data(market_info));
console.log(sort_info);
async function get_candle(minute, market) {
const url = `https://api.upbit.com/v1/candles/minutes/${minute}?market=${market}&count=1`;
var candle = new Array();
let response = await fetch(url, options)
.then(res => res.json())
.then(json => candle = json)
return candle;
}
app.get('/get_market', async (req, res) => {
var name_list = (await get_marketName());
var market_info = (await get_marketInfo(name_list));
sort_info = (await sort_data(market_info));
res.json(await save_coin(sort_info));
})
app.listen(5000,()=>{
app.get('/get_candle', async (req, res) => {
Coin.find()
.then(result => {
result.forEach((item) => {
get_candle(5, item.name)
.then(result => {
Coin.findOneAndUpdate({ name: result[0].market }, { five_candle: result[0].trade_price }, { new: true }, (err, doc) => {
console.log(doc);
})
})
})
});
})
app.listen(5000, () => {
console.log('server')
})
\ No newline at end of file
......
......@@ -15,6 +15,9 @@ const coinSchema=mongoose.Schema({
current_price:{
type:Number,
required:true
},
five_candle:{
type:Number
}
})
......
This diff is collapsed. Click to expand it.
......@@ -4,7 +4,8 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"start":"node index.js"
},
"repository": {
"type": "git",
......@@ -14,6 +15,7 @@
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"mongoose": "^6.0.12",
"node-fetch": "^3.0.0"
},
"devDependencies": {
......