Skip to content

Commit

Permalink
Merge branch 'release/0.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
man-c committed May 30, 2019
2 parents e8f80ff + 5c820a8 commit 2548171
Show file tree
Hide file tree
Showing 5 changed files with 304 additions and 11 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# CoinGecko API wrapper
[![PyPi Version](https://img.shields.io/pypi/v/pycoingecko.svg)](https://pypi.python.org/pypi/pycoingecko/)

Python3 wrapper around the [CoinGecko](https://www.coingecko.com/) API (V3)

Expand All @@ -23,17 +24,23 @@ cg = CoinGeckoAPI()

### Examples
The required parameters for each endpoint are defined as required (mandatory) parameters for the coresponding functions.
Optional parameters can be also passed using same names, as defined in CoinGecko API doc (https://www.coingecko.com/api/docs/v3)
**Any optional parameters** can be also passed using same names, as defined in CoinGecko API doc (https://www.coingecko.com/api/docs/v3)

*Lists are also supported as input for multiple-valued comma-separated parameters (e.g. see /simple/price usage examples).*

Usage examples:
```python
# /simple/price endpoint with the required parameters
>>> cg.get_price(ids='bitcoin', vs_currencies='usd')
{'bitcoin': {'usd': 3462.04}}

>>> cg.get_price(ids='bitcoin,litecoin,ethereum', vs_currencies='usd')
# OR (lists can be used for multiple-valued arguments)
>>> cg.get_price(ids=['bitcoin', 'litecoin', 'ethereum'], vs_currencies='usd')
{'bitcoin': {'usd': 3461.27}, 'ethereum': {'usd': 106.92}, 'litecoin': {'usd': 32.72}}

>>> cg.get_price(ids='bitcoin,litecoin,ethereum', vs_currencies='usd,eur')
# OR (lists can be used for multiple-valued arguments)
>>> cg.get_price(ids=['bitcoin', 'litecoin', 'ethereum'], vs_currencies=['usd', 'eur'])
{'bitcoin': {'usd': 3459.39, 'eur': 3019.33}, 'ethereum': {'usd': 106.91, 'eur': 93.31}, 'litecoin': {'usd': 32.72, 'eur': 28.56}}

Expand Down
37 changes: 30 additions & 7 deletions pycoingecko/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry

from .utils import get_comma_separated_values

from .utils import list_args_to_comma_separated

class CoinGeckoAPI:

Expand All @@ -24,10 +23,14 @@ def __request(self, url):
#print(url)
try:
response = self.session.get(url, timeout = self.request_timeout)
response.raise_for_status()
content = json.loads(response.content.decode('utf-8'))
response.raise_for_status()
return content
except Exception as e:
try:
raise ValueError(content)
except UnboundLocalError as e:
pass
raise


Expand All @@ -49,22 +52,29 @@ def ping(self):


#---------- SIMPLE ----------#
@list_args_to_comma_separated
def get_price(self, ids, vs_currencies, **kwargs):
"""Get the current price of any cryptocurrencies in any other supported currencies that you need"""

kwargs['ids'] = get_comma_separated_values(ids)
kwargs['vs_currencies'] = get_comma_separated_values(vs_currencies)
ids=ids.replace(' ','')
kwargs['ids'] = ids
vs_currencies=vs_currencies.replace(' ','')
kwargs['vs_currencies'] = vs_currencies

api_url = '{0}simple/price'.format(self.api_base_url)
api_url = self.__api_url_params(api_url, kwargs)

return self.__request(api_url)


@list_args_to_comma_separated
def get_token_price(self, id, contract_addresses, vs_currencies, **kwargs):
"""Get the current price of any tokens on this coin (ETH only at this stage as per api docs) in any other supported currencies that you need"""

kwargs['contract_addresses'] = get_comma_separated_values(contract_addresses)
kwargs['vs_currencies'] = get_comma_separated_values(vs_currencies)
contract_addresses=contract_addresses.replace(' ','')
kwargs['contract_addresses'] = contract_addresses
vs_currencies=vs_currencies.replace(' ','')
kwargs['vs_currencies'] = vs_currencies

api_url = '{0}simple/token_price/{1}'.format(self.api_base_url, id)
api_url = self.__api_url_params(api_url, kwargs)
Expand All @@ -79,6 +89,7 @@ def get_supported_vs_currencies(self):


#---------- COINS ----------#
@list_args_to_comma_separated
def get_coins(self, **kwargs):
"""List all coins with data (name, price, market, developer, community, etc)"""

Expand All @@ -97,6 +108,7 @@ def get_coins_list(self):
return self.__request(api_url)


@list_args_to_comma_separated
def get_coins_markets(self, vs_currency, **kwargs):
"""List all supported coins price, market cap, volume, and market related data (no pagination required)"""

Expand All @@ -108,6 +120,7 @@ def get_coins_markets(self, vs_currency, **kwargs):
return self.__request(api_url)


@list_args_to_comma_separated
def get_coin_by_id(self, id, **kwargs):
"""Get current data (name, price, market, ... including exchange tickers) for a coin"""

Expand All @@ -117,6 +130,7 @@ def get_coin_by_id(self, id, **kwargs):
return self.__request(api_url)


@list_args_to_comma_separated
def get_coin_ticker_by_id(self, id, **kwargs):
"""Get coin tickers (paginated to 100 items)"""

Expand All @@ -126,6 +140,7 @@ def get_coin_ticker_by_id(self, id, **kwargs):
return self.__request(api_url)


@list_args_to_comma_separated
def get_coin_history_by_id(self, id, date, **kwargs):
"""Get historical data (name, price, market, stats) at a given date for a coin"""

Expand All @@ -137,6 +152,7 @@ def get_coin_history_by_id(self, id, date, **kwargs):
return self.__request(api_url)


@list_args_to_comma_separated
def get_coin_market_chart_by_id(self, id, vs_currency, days):
"""Get historical market data include price, market cap, and 24h volume (granularity auto)"""

Expand All @@ -145,6 +161,7 @@ def get_coin_market_chart_by_id(self, id, vs_currency, days):
return self.__request(api_url)


@list_args_to_comma_separated
def get_coin_status_updates_by_id(self, id, **kwargs):
"""Get status updates for a given coin"""

Expand All @@ -154,6 +171,7 @@ def get_coin_status_updates_by_id(self, id, **kwargs):
return self.__request(api_url)


@list_args_to_comma_separated
def get_coin_info_from_contract_address_by_id(self, id, contract_address):
"""Get coin info from contract address"""

Expand All @@ -179,6 +197,7 @@ def get_exchanges_id_name_list(self):
return self.__request(api_url)


@list_args_to_comma_separated
def get_exchanges_by_id(self, id):
"""Get exchange volume in BTC and tickers"""

Expand All @@ -187,6 +206,7 @@ def get_exchanges_by_id(self, id):
return self.__request(api_url)


@list_args_to_comma_separated
def get_exchanges_tickers_by_id(self, id, **kwargs):
"""Get exchange tickers (paginated)"""

Expand All @@ -196,6 +216,7 @@ def get_exchanges_tickers_by_id(self, id, **kwargs):
return self.__request(api_url)


@list_args_to_comma_separated
def get_exchanges_status_updates_by_id(self, id, **kwargs):
"""Get status updates for a given exchange"""

Expand All @@ -206,6 +227,7 @@ def get_exchanges_status_updates_by_id(self, id, **kwargs):


#---------- STATUS UPDATES ----------#
@list_args_to_comma_separated
def get_status_updates(self, **kwargs):
"""List all status_updates with data (description, category, created_at, user, user_title and pin)"""

Expand All @@ -216,6 +238,7 @@ def get_status_updates(self, **kwargs):


#---------- EVENTS ----------#
@list_args_to_comma_separated
def get_events(self, **kwargs):
"""Get events, paginated by 100"""

Expand Down
13 changes: 13 additions & 0 deletions pycoingecko/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
def list_args_to_comma_separated(func):
"""Return function that converts list input arguments to comma-separated strings"""
def input_args(*args,**kwargs):
for v in kwargs:
# check in **kwargs for lists and convert to comma-separated string
if isinstance(kwargs[v], list): kwargs[v]=','.join(kwargs[v])
# check in *args for lists and convert to comma-separated string
args=[','.join(v) if isinstance(v, list) else v for v in args]
return func(*args, **kwargs)
return input_args


def get_comma_separated_values(values):
"""Return the values as a comma-separated string"""

# Make sure values is a list or tuple
if not isinstance(values, list) and not isinstance(values, tuple):
values = [values]
return ','.join(values)

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setuptools.setup(
name='pycoingecko',
version='0.2.0',
version='0.3.0',
packages=['pycoingecko',],
license='MIT',
description = 'Python wrapper around the CoinGecko API',
Expand Down
Loading

0 comments on commit 2548171

Please sign in to comment.