that-hypermnesiac-guy/providers/cryptocompare.js

32 lines
1.2 KiB
JavaScript
Raw Normal View History

2019-04-22 21:54:54 +02:00
import fetch from 'node-fetch'
import cc from 'cryptocompare'
import fs from 'fs-extra'
global.fetch = fetch
cc.rateLimit = function( apiKey )
{
return fetch( `https://min-api.cryptocompare.com/stats/rate/limit${apiKey ? `?api_key=${apiKey}` : ''}` )
.then( res=> { if( !res.ok ){ throw new Error(`${res.status} ${res.statusText}`) }else return res.json() })
.then( body=> { if( body.Response === 'Error' ){ throw body.Message }else return body })
}
// loadByPath( "EXCHANGE/SYM/SYM/PERIOD/TS|/TS|LIMIT" ).then()
export function loadByPath( ccfs )
{
let [ exchange, fsym, tsym, period, start, limit = 2000 ] = ccfs.split("/")
, [ , aggregate, tUnit ] = period.match( /(\d+)([HDWM]*)/ )
, loader = 'histo' + (tUnit == '' ? 'Minute' : tUnit == 'H' ? 'Hour' : 'Day')
// calculate toTs from limit and start
console.log( loader, aggregate*timeFrames[tUnit] )
return cc[loader]( fsym, tsym, { allData: true, limit: 2000, aggregate: aggregate*timeFrames[tUnit], exchange } )
}
const timeFrames = { '': 1, 'H': 1, 'D': 1, 'W': 7, 'M': 30 }
const timeFrameMinutes = { '': 1, 'H': 60, 'D': 60*24, 'W': 60*24*7, 'M': 60*24*30 }
export {
coinList as coins
, exchangeList as exchanges
} from 'cryptocompare'