Skip to content

Instantly share code, notes, and snippets.

@ParthBarot-BoTreeConsulting
Last active September 25, 2017 07:49
Show Gist options
  • Save ParthBarot-BoTreeConsulting/ce4c569247e3dc65e507f04f06752f34 to your computer and use it in GitHub Desktop.
Save ParthBarot-BoTreeConsulting/ce4c569247e3dc65e507f04f06752f34 to your computer and use it in GitHub Desktop.
Python client for CryptoCompare
class Client:
def coin_list(self):
return False
def coin_snapshot_full_by_id(self, coin_id):
return False
def coin_snapshot(self, fsym, tsym):
return False
import json
import urllib2
class Client:
def coin_list(self):
url = 'https://www.cryptocompare.com/api/data/coinlist/'
print('Calling URL - ' + url)
json_response = json.load(urllib2.urlopen(url))
if 'Response' in json_response and json_response["Response"] == 'Error':
raise ValueError("API Error - {} !".format(json_response['Message']))
else:
return json_response
def coin_snapshot_full_by_id(self, coin_id):
url = 'https://www.cryptocompare.com/api/data/coinsnapshotfullbyid/?id=' + coin_id
print('Calling URL - ' + url)
json_response = json.load(urllib2.urlopen(url))
if 'Response' in json_response and json_response["Response"] == 'Error':
raise ValueError("API Error - {} !".format(json_response['Message']))
else:
return json_response
def coin_snapshot(self, fsym, tsym):
url = 'https://www.cryptocompare.com/api/data/coinsnapshot/' + '?fsym='+fsym+'&tsym='+tsym
print('Calling URL - ' + url)
json_response = json.load(urllib2.urlopen(url))
if 'Response' in json_response and json_response["Response"] == 'Error':
raise ValueError("API Error - {} !".format(json_response['Message']))
else:
return json_response
import json
import urllib2
class Client:
COIN_LIST_URL = 'https://www.cryptocompare.com/api/data/coinlist/'
COIN_SNAPSHOT_FULL_BY_ID_URL = 'https://www.cryptocompare.com/api/data/coinsnapshotfullbyid/?id='
COIN_SNAPSHOT_URL = 'https://www.cryptocompare.com/api/data/coinsnapshot/'
def coin_list(self):
return self._fetch_data(self.COIN_LIST_URL)
def coin_snapshot_full_by_id(self, coin_id):
if(coin_id == None or coin_id == ''):
raise ValueError('coin_id cannot be empty!')
return self._fetch_data(self.COIN_SNAPSHOT_FULL_BY_ID_URL+str(coin_id))
def coin_snapshot(self, fsym, tsym):
if(fsym == None or fsym == ''):
raise ValueError('fsym cannot be empty!')
if(tsym == None or tsym == ''):
raise ValueError('tsym cannot be empty!')
return self._fetch_data(self.COIN_SNAPSHOT_URL+'?fsym='+fsym+'&tsym='+tsym)
def _fetch_data(self, url):
print('Calling URL - ' + url)
json_response = json.load(urllib2.urlopen(url))
if 'Response' in json_response and json_response["Response"] == 'Error':
raise ValueError("API Error - {} !".format(json_response['Message']))
else:
return json_response
class Client:
COIN_LIST_URL = 'https://www.cryptocompare.com/api/data/coinlist/'
COIN_SNAPSHOT_FULL_BY_ID_URL = 'https://www.cryptocompare.com/api/data/coinsnapshotfullbyid/?id='
COIN_SNAPSHOT_URL = 'https://www.cryptocompare.com/api/data/coinsnapshot/'
PRICE_URL = 'https://min-api.cryptocompare.com/data/price'
PRICE_MULTI_URL = 'https://min-api.cryptocompare.com/data/pricemulti'
PRICE_MULTI_FULL_URL = 'https://min-api.cryptocompare.com/data/pricemultifull'
PRICE_HISTORICAL_URL = 'https://min-api.cryptocompare.com/data/pricehistorical'
GENERATE_AVG_URL = 'https://min-api.cryptocompare.com/data/generateAvg'
DAY_AVG_URL = 'https://min-api.cryptocompare.com/data/dayAvg'
SUBS_WATCH_LIST_URL = 'https://min-api.cryptocompare.com/data/subsWatchlist'
SUBS_URL = 'https://min-api.cryptocompare.com/data/subs'
ALL_EXCHANGES_URL = 'https://min-api.cryptocompare.com/data/all/exchanges'
TOP_EXCHANGES_URL = 'https://min-api.cryptocompare.com/data/top/exchanges'
TOP_VOLUMES_URL = 'https://min-api.cryptocompare.com/data/top/volumes'
TOP_PAIRS_URL = 'https://min-api.cryptocompare.com/data/top/pairs'
HISTO_DAY_URL = 'https://min-api.cryptocompare.com/data/histoday'
HISTO_HOUR_URL = 'https://min-api.cryptocompare.com/data/histohour'
HISTO_MINUTE_URL = 'https://min-api.cryptocompare.com/data/histominute'
SOCIAL_STATS_URL = 'https://www.cryptocompare.com/api/data/socialstats?id='
MINING_CONTRACTS_URL = 'https://www.cryptocompare.com/api/data/miningcontracts/'
MINING_EQUIPMENT_URL = 'https://www.cryptocompare.com/api/data/miningequipment/'
#1. Grouped related API methods together, and moved them into a module.
#2. Include all the module methods inside the class so that the module methods becomes class instance methods.
# This way, methods are available to use at runtime, but the code is separated and more readable/maintainable.
from .apis.coin import coin_list, coin_snapshot_full_by_id, coin_snapshot
from .apis.price import price, price_multi, price_multifull, price_historical
from .apis.average import generate_avg, day_avg
from .apis.subs import subs_watchlist, subs
from .apis.top import top_exchanges, top_volumes, top_pairs
from .apis.histo import histo_day, histo_hour, histo_minute
from .apis.mining import mining_contracts, mining_equipment
from .apis.uncategorized import all_exchanges, social_stats
from .apis.helper import _is_params_valid, _fetch_data, _get_querystring
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment