1
mirror of git://git.acid.vegas/coinmarketcap.git synced 2024-11-21 22:26:38 +00:00

Initial commit

This commit is contained in:
Dionysus 2020-04-09 20:37:48 -04:00
commit 9d4eb03273
Signed by: acidvegas
GPG Key ID: EF4B922DB85DC9DE
3 changed files with 121 additions and 0 deletions

15
LICENSE Normal file
View File

@ -0,0 +1,15 @@
ISC License
Copyright (c) 2020, acidvegas <acid.vegas@acid.vegas>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

50
README.md Normal file
View File

@ -0,0 +1,50 @@
# coinmarketcap
> A Python class for the API on [CoinMarketCap](https://coinmarketcap.com)
## Requirements
* [Python](https://www.python.org/downloads/) *(**Note:** This script was developed to be used with the latest version of Python)*
## API Documentation
- [CoinMarketCap API Documentation](https://coinmarketcap.com/api/documentation/v1/)
## Information
In order to use the CoinMarketCap API, you will need an API key which you can sign up for one [here](https://pro.coinmarketcap.com/signup/).
Data from the API will be cached for 5 minutes at a time (that is how long it takes CoinMarketCap to refresh their data) this way you will not get rate limited.
The class has only 2 main functions, one for global data and one for ticker data.
## Example
```python
from coinmarketcap import CoinMarketCap
CMC = CoinMarketCap('API_KEY_HERE')
global_data = CMC._global() # Global data example
print('Cryptocurrencies : ' + str(global_data['cryptocurrencies']))
print('Exchanges : ' + str(global_data['exchanges']))
print('BTC Dominance : ' + str(global_data['btc_dominance']))
print('ETH Dominance : ' + str(global_data['eth_dominance']))
print('Market Cap : ' + str(global_data['market_cap']))
print('Volume : ' + str(global_data['volume']))
ticker_data = CMC._ticker() # Ticker data example
for item in ticker_data:
print('ID : ' + item)
print('Name : ' + ticker_data[item]['name'])
print('Symbol : ' + ticker_data[item]['symbol'])
print('Slug : ' + ticker_data[item]['slug'])
print('Rank : ' + str(ticker_data[item]['rank']))
print('Price : ' + str(ticker_data[item]['price']))
print('1h Percent : ' + str(ticker_data[item]['percent']['1h']))
print('24h Percent : ' + str(ticker_data[item]['percent']['24h']))
print('7d Percent : ' + str(ticker_data[item]['percent']['7d']))
print('Volume : ' + str(ticker_data[item]['volume']))
print('Market Cap : ' + str(ticker_data[item]['market_cap']))
input('') # Press enter to continue...
```
## Mirrors
- [acid.vegas](https://acid.vegas/coinmarketcap) *(main)*
- [GitHub](https://github.com/acidvegas/coinmarketcap)
- [GitLab](https://gitlab.com/acidvegas/coinmarketcap)

56
coinmarketcap.py Normal file
View File

@ -0,0 +1,56 @@
#!/usr/bin/env python
# CoinMarketCap API Class - Developed by acidvegas in Python (https://acid.vegas/coinmarketcap)
import http.client
import json
import time
import zlib
class CoinMarketCap(object):
def __init__(self, api_key):
self.api_key = api_key
self.cache = {'global':dict(), 'ticker':dict()}
self.last = {'global':0 , 'ticker':0 }
def _api(self, _endpoint):
conn = http.client.HTTPSConnection('pro-api.coinmarketcap.com', timeout=15)
conn.request('GET', '/v1/' + _endpoint, headers={'Accept':'application/json', 'Accept-Encoding':'deflate, gzip', 'X-CMC_PRO_API_KEY':self.api_key})
response = zlib.decompress(conn.getresponse().read(), 16+zlib.MAX_WBITS).decode('utf-8').replace(': null', ': "0"')
conn.close()
return json.loads(response)['data']
def _global(self):
if time.time() - self.last['global'] < 300:
return self.cache['global']
else:
data = self._api('global-metrics/quotes/latest')
self.cache['global'] = {
'cryptocurrencies' : data['active_cryptocurrencies'],
'exchanges' : data['active_exchanges'],
'btc_dominance' : int(data['btc_dominance']),
'eth_dominance' : int(data['eth_dominance']),
'market_cap' : int(data['quote']['USD']['total_market_cap']),
'volume' : int(data['quote']['USD']['total_volume_24h'])
}
self.last['global'] = time.time()
return self.cache['global']
def _ticker(self):
if time.time() - self.last['ticker'] < 300:
return self.cache['ticker']
else:
data = self._api('cryptocurrency/listings/latest?limit=5000')
self.cache['ticker'] = dict()
for item in data:
self.cache['ticker'][item['id']] = {
'name' : item['name'],
'symbol' : item['symbol'],
'slug' : item['slug'],
'rank' : item['cmc_rank'],
'price' : float(item['quote']['USD']['price']),
'percent' : {'1h':float(item['quote']['USD']['percent_change_1h']), '24h':float(item['quote']['USD']['percent_change_24h']), '7d':float(item['quote']['USD']['percent_change_7d'])},
'volume' : int(float(item['quote']['USD']['volume_24h'])),
'market_cap' : int(float(item['quote']['USD']['market_cap']))
}
self.last['ticker'] = time.time()
return self.cache['ticker']