mirror of
git://git.acid.vegas/coinmarketcap.git
synced 2024-11-21 14:26:36 +00:00
Mirrors updated and code cleaned up
This commit is contained in:
parent
e5f75a8235
commit
46592c3bb7
2
LICENSE
2
LICENSE
@ -1,6 +1,6 @@
|
|||||||
ISC License
|
ISC License
|
||||||
|
|
||||||
Copyright (c) 2020, acidvegas <acid.vegas@acid.vegas>
|
Copyright (c) 2021, acidvegas <acid.vegas@acid.vegas>
|
||||||
|
|
||||||
Permission to use, copy, modify, and/or distribute this software for any
|
Permission to use, copy, modify, and/or distribute this software for any
|
||||||
purpose with or without fee is hereby granted, provided that the above
|
purpose with or without fee is hereby granted, provided that the above
|
||||||
|
10
README.md
10
README.md
@ -2,7 +2,7 @@
|
|||||||
> A Python class for the API on [CoinMarketCap](https://coinmarketcap.com)
|
> A Python class for the API on [CoinMarketCap](https://coinmarketcap.com)
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
* [Python](https://www.python.org/downloads/) *(**Note:** This script was developed to be used with the latest version of Python)*
|
* [Python](https://www.python.org/downloads/)
|
||||||
|
|
||||||
## API Documentation
|
## API Documentation
|
||||||
- [CoinMarketCap API Documentation](https://coinmarketcap.com/api/documentation/v1/)
|
- [CoinMarketCap API Documentation](https://coinmarketcap.com/api/documentation/v1/)
|
||||||
@ -44,7 +44,7 @@ for item in ticker_data:
|
|||||||
input('') # Press enter to continue...
|
input('') # Press enter to continue...
|
||||||
```
|
```
|
||||||
|
|
||||||
## Mirrors
|
___
|
||||||
- [acid.vegas](https://acid.vegas/coinmarketcap) *(main)*
|
|
||||||
- [GitHub](https://github.com/acidvegas/coinmarketcap)
|
###### Mirrors
|
||||||
- [GitLab](https://gitlab.com/acidvegas/coinmarketcap)
|
[acid.vegas](https://git.acid.vegas/coinmarketcap) • [GitHub](https://github.com/acidvegas/coinmarketcap) • [GitLab](https://gitlab.com/acidvegas/coinmarketcap) • [SuperNETs](https://git.supernets.org/acidvegas/coinmarketcap)
|
||||||
|
@ -6,14 +6,7 @@ import json
|
|||||||
import time
|
import time
|
||||||
import zlib
|
import zlib
|
||||||
|
|
||||||
# Find a better way to do this...
|
CACHE_EXPIRY_TIME = 300
|
||||||
def replace_nulls(json_elem):
|
|
||||||
if isinstance(json_elem, list):
|
|
||||||
return [replace_nulls(elem) for elem in json_elem]
|
|
||||||
elif isinstance(json_elem, dict):
|
|
||||||
return {key: replace_nulls(value) for key, value in json_elem.items()}
|
|
||||||
else:
|
|
||||||
return '0' if json_elem is None else json_elem
|
|
||||||
|
|
||||||
class CoinMarketCap(object):
|
class CoinMarketCap(object):
|
||||||
def __init__(self, api_key):
|
def __init__(self, api_key):
|
||||||
@ -22,14 +15,19 @@ class CoinMarketCap(object):
|
|||||||
self.last = {'global':0 , 'ticker':0 }
|
self.last = {'global':0 , 'ticker':0 }
|
||||||
|
|
||||||
def _api(self, _endpoint):
|
def _api(self, _endpoint):
|
||||||
conn = http.client.HTTPSConnection('pro-api.coinmarketcap.com', timeout=15)
|
'''Make a request to the CoinMarketCap API.'''
|
||||||
conn.request('GET', '/v1/' + _endpoint, headers={'Accept':'application/json', 'Accept-Encoding':'deflate, gzip', 'X-CMC_PRO_API_KEY':self.api_key})
|
with http.client.HTTPSConnection('pro-api.coinmarketcap.com', timeout=15) as conn:
|
||||||
response = zlib.decompress(conn.getresponse().read(), 16+zlib.MAX_WBITS).decode('utf-8').replace(': null', ': "0"')
|
conn.request('GET', f'/v1/{_endpoint}', headers={'Accept':'application/json', 'Accept-Encoding':'deflate, gzip', 'X-CMC_PRO_API_KEY':self.api_key})
|
||||||
conn.close()
|
response = conn.getresponse()
|
||||||
return json.loads(response)['data']
|
if response.getheader('Content-Encoding') == 'gzip':
|
||||||
|
content = zlib.decompress(response.read(), 16+zlib.MAX_WBITS).decode('utf-8')
|
||||||
|
else:
|
||||||
|
content = response.read().decode('utf-8')
|
||||||
|
return json.loads(content.replace(': null', ': "0"'))['data']
|
||||||
|
|
||||||
def _global(self):
|
def _global(self):
|
||||||
if time.time() - self.last['global'] < 300:
|
'''Get global market data.'''
|
||||||
|
if time.time() - self.last['global'] < CACHE_EXPIRY_TIME:
|
||||||
return self.cache['global']
|
return self.cache['global']
|
||||||
else:
|
else:
|
||||||
data = self._api('global-metrics/quotes/latest')
|
data = self._api('global-metrics/quotes/latest')
|
||||||
@ -45,10 +43,11 @@ class CoinMarketCap(object):
|
|||||||
return self.cache['global']
|
return self.cache['global']
|
||||||
|
|
||||||
def _ticker(self):
|
def _ticker(self):
|
||||||
if time.time() - self.last['ticker'] < 300:
|
'''Get ticker data.'''
|
||||||
|
if time.time() - self.last['ticker'] < CACHE_EXPIRY_TIME:
|
||||||
return self.cache['ticker']
|
return self.cache['ticker']
|
||||||
else:
|
else:
|
||||||
data = replace_nulls(self._api('cryptocurrency/listings/latest?limit=5000'))
|
data = self._api('cryptocurrency/listings/latest?limit=5000')
|
||||||
self.cache['ticker'] = dict()
|
self.cache['ticker'] = dict()
|
||||||
for item in data:
|
for item in data:
|
||||||
self.cache['ticker'][item['id']] = {
|
self.cache['ticker'][item['id']] = {
|
||||||
|
Loading…
Reference in New Issue
Block a user