From f3c9a1043e9848ee0e0c4ba426815ce33154db52 Mon Sep 17 00:00:00 2001 From: Vlad Cealicu Date: Mon, 26 Nov 2018 19:51:58 +0100 Subject: [PATCH] Add API key method and news list endpoint (#32) --- README.md | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 33 ++++++++++++++++++ 2 files changed, 133 insertions(+) diff --git a/README.md b/README.md index a6d02c6..b9fd4da 100644 --- a/README.md +++ b/README.md @@ -27,11 +27,13 @@ Usage - If you are using this in electron, it should work without any configuration. - If you are using this in Node.js, you will need to use [`node-fetch`](https://www.npmjs.com/package/node-fetch). +- The package works without an API key but the IP limits will slowly be reduced over time, to create an API key just go to https://www.cryptocompare.com/cryptopian/api-keys and make sure you give it the "Read All Price Streaming and Polling Endpoints" permission **Example:** ```js global.fetch = require('node-fetch') const cc = require('cryptocompare') + cc.setApiKey('') ``` ### Methods @@ -66,6 +68,7 @@ Get the current list of all cryptocurrencies and the following information about ```js const cc = require('cryptocompare') +cc.setApiKey('') // Usage: cc.coinList() @@ -107,6 +110,7 @@ Returns all the exchanges that CryptoCompare has integrated with. ```js const cc = require('cryptocompare') +cc.setApiKey('') // Usage: cc.exchangeList() @@ -141,6 +145,7 @@ Get the current price of any cryptocurrency in any other currency. ```js const cc = require('cryptocompare') +cc.setApiKey('') // Basic Usage: cc.price('BTC', ['USD', 'EUR']) @@ -173,6 +178,7 @@ Works like `price()`, except it allows you to specify a matrix of From Symbols. ```js const cc = require('cryptocompare') +cc.setApiKey('') // Basic Usage: cc.priceMulti(['BTC', 'ETH'], ['USD', 'EUR']) @@ -206,6 +212,7 @@ Get all the current trading info (price, vol, open, high, low, etc.) of any list ```js const cc = require('cryptocompare') +cc.setApiKey('') cc.priceFull(['BTC', 'ETH'], ['USD', 'EUR']) .then(prices => { @@ -257,6 +264,7 @@ Get the price of any cryptocurrency in any other currency at a given timestamp. ```js const cc = require('cryptocompare') +cc.setApiKey('') // Basic Usage: cc.priceHistorical('BTC', ['USD', 'EUR'], new Date('2017-01-01')) @@ -280,6 +288,7 @@ Compute the current trading info (price, vol, open, high, low etc) of the reques ```js const cc = require('cryptocompare') +cc.setApiKey('') // Basic Usage: cc.generateAvg('BTC', 'USD', ['Coinbase', 'Kraken', 'Bitstamp', 'Bitfinex']) @@ -317,6 +326,7 @@ Get top pairs by volume for a currency. ```js const cc = require('cryptocompare') +cc.setApiKey('') cc.topPairs('BTC', 2) .then(pairs => { @@ -347,6 +357,7 @@ Get top exchanges by volume for a currency pair. ```js const cc = require('cryptocompare') +cc.setApiKey('') cc.topExchanges('BTC', 'USD', 2) .then(exchanges => { @@ -475,6 +486,95 @@ cc.histoMinute('BTC', 'USD') .catch(console.error) ``` +### `newsList()` + +Returns news articles from the providers that CryptoCompare has integrated with. + +`newsList(lang[, options])` + +- `lang` (String) Preferred language - English (EN) or Portuguese (PT) +- `options` (Object) + - `feeds` (Array of Strings | Array) Specific news feeds to retrieve news from, if empty, defaults to all of them. (You can get a list of news feeds with `newsFeedsAndCategories().Feeds`..) + - `categories` (Array of Strings | Array) Category of news articles to return, if empty, defaults to all of them. (You can get a list of news categories with `newsFeedsAndCategories().Categories`..) + - `excludeCategories` (Array of Strings | Array) News article categories to exclude from results, if empty, defaults to none. (You can get a list of news categories with `newsFeedsAndCategories().Categories`..) + - `lTs` (Date) Returns news before that timestamp + +```js +const cc = require('cryptocompare') +cc.setApiKey('') + +// Basic Usage: +cc.newsList('EN') +.then(newsList => { + console.log(newsList) + //[ + // { + // id: "708235" + // guid: "https://www.cryptoglobe.com/latest/2018/11/china-cryptocurrency-mining-machines-are-reportedly-being-sold-according-to-their-weight/" + // published_on: 1542886256 + // imageurl: "https://images.cryptocompare.com/news/cryptoglobe/fwMg0080000.jpeg" + // title: "China: Cryptocurrency Mining Machines Reportedly Being Sold According to Their Weight" + // url: "https://www.cryptoglobe.com/latest/2018/11/china-cryptocurrency-mining-machines-are-reportedly-being-sold-according-to-their-weight/" + // source: "cryptoglobe" + // body: "Cryptocurrency mining machines are reportedly being sold in China according to their weight as miners who haven’t been able to make a profit are seemingly getting rid of their old models to get some of their investment back." + // tags: "" + // categories: "Mining|Asia|Business" + // upvotes: "0" + // downvotes: "0" + // lang: "EN" + // source_info: { + // name: "CryptoGlobe" + // lang: "EN" + // img: "https://images.cryptocompare.com/news/default/cryptoglobe.png" + // } + // } + // .... + //] +}) +.catch(console.error) +``` + +### `newsFeedsAndCategories()` + +Returns all the news feeds (providers) that CryptoCompare has integrated with and the full list of categories. + +`newsFeedsAndCategories()` + +- `No parameters` +- `Returns` (Object) + +```js +const cc = require('cryptocompare') +cc.setApiKey('') + +// Usage: +cc.exchangeList() +.then(newsFeedsAndCategories => { + console.log(newsFeedsAndCategories) + // { + // "Categories": + // [ + // { + // categoryName: "BTC" + // wordsAssociatedWithCategory: ["BTC","BITCOIN", "SATOSHI"] + // } + // ... + // ] + // "Feeds": + // [ + // { + // key: "cryptocompare" + // name: "CryptoCompare" + // lang: "EN" + // img: "https://images.cryptocompare.com/news/default/cryptocompare.png" + // } + // ... + // ] + // } +}) +.catch(console.error) +``` + ## License [MIT](LICENSE.md) diff --git a/index.js b/index.js index 4fad747..a32fdd5 100644 --- a/index.js +++ b/index.js @@ -2,8 +2,21 @@ /* global fetch */ const baseUrl = 'https://min-api.cryptocompare.com/data/' +let apiKey = '' + +function setApiKey (userApiKey) { + apiKey = userApiKey +} function fetchJSON (url) { + if (apiKey !== '') { + if (url.indexOf('?') > -1) { + url += '&api_key=' + } else { + url += '?api_key=' + } + url += apiKey + } return fetch(url) .then(res => { if (!res.ok) { @@ -27,6 +40,23 @@ function exchangeList () { return fetchJSON(url) } +function newsFeedsAndCategories () { + const url = `${baseUrl}data/news/feedsandcategories` + return fetchJSON(url).then(result => result.Data) +} + +function newsList (lang, options) { + let url = `${baseUrl}data/v2/news/?lang=${lang}` + if (options.feeds) url += `&feeds=${options.feeds}` + if (options.categories) url += `&categories=${options.categories}` + if (options.excludeCategories) url += `&categories=${options.excludeCategories}` + if (options.lTs) { + options.lTs = dateToTimestamp(options.lTs) + url += `&lTs=${options.lTs}` + } + return fetchJSON(url).then(result => result.Data) +} + function price (fsym, tsyms, options) { options = options || {} let url = `${baseUrl}price?fsym=${fsym}&tsyms=${tsyms}` @@ -136,8 +166,11 @@ function dateToTimestamp (date) { } module.exports = { + setApiKey, coinList, exchangeList, + newsFeedsAndCategories, + newsList, price, priceMulti, priceFull,