Skip to content

Commit

Permalink
Add API key method and news list endpoint (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
vcealicu authored and RyanZim committed Nov 26, 2018
1 parent 1ee009b commit f3c9a10
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
100 changes: 100 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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('<your-api-key>')
```

### Methods
Expand Down Expand Up @@ -66,6 +68,7 @@ Get the current list of all cryptocurrencies and the following information about

```js
const cc = require('cryptocompare')
cc.setApiKey('<your-api-key>')

// Usage:
cc.coinList()
Expand Down Expand Up @@ -107,6 +110,7 @@ Returns all the exchanges that CryptoCompare has integrated with.

```js
const cc = require('cryptocompare')
cc.setApiKey('<your-api-key>')

// Usage:
cc.exchangeList()
Expand Down Expand Up @@ -141,6 +145,7 @@ Get the current price of any cryptocurrency in any other currency.

```js
const cc = require('cryptocompare')
cc.setApiKey('<your-api-key>')

// Basic Usage:
cc.price('BTC', ['USD', 'EUR'])
Expand Down Expand Up @@ -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('<your-api-key>')

// Basic Usage:
cc.priceMulti(['BTC', 'ETH'], ['USD', 'EUR'])
Expand Down Expand Up @@ -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('<your-api-key>')

cc.priceFull(['BTC', 'ETH'], ['USD', 'EUR'])
.then(prices => {
Expand Down Expand Up @@ -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('<your-api-key>')

// Basic Usage:
cc.priceHistorical('BTC', ['USD', 'EUR'], new Date('2017-01-01'))
Expand All @@ -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('<your-api-key>')

// Basic Usage:
cc.generateAvg('BTC', 'USD', ['Coinbase', 'Kraken', 'Bitstamp', 'Bitfinex'])
Expand Down Expand Up @@ -317,6 +326,7 @@ Get top pairs by volume for a currency.

```js
const cc = require('cryptocompare')
cc.setApiKey('<your-api-key>')

cc.topPairs('BTC', 2)
.then(pairs => {
Expand Down Expand Up @@ -347,6 +357,7 @@ Get top exchanges by volume for a currency pair.

```js
const cc = require('cryptocompare')
cc.setApiKey('<your-api-key>')

cc.topExchanges('BTC', 'USD', 2)
.then(exchanges => {
Expand Down Expand Up @@ -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('<your-api-key>')

// 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('<your-api-key>')

// 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)
33 changes: 33 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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}`
Expand Down Expand Up @@ -136,8 +166,11 @@ function dateToTimestamp (date) {
}

module.exports = {
setApiKey,
coinList,
exchangeList,
newsFeedsAndCategories,
newsList,
price,
priceMulti,
priceFull,
Expand Down

0 comments on commit f3c9a10

Please sign in to comment.