diff options
author | David McMackins II <contact@mcmackins.org> | 2015-04-05 14:49:13 -0500 |
---|---|---|
committer | David McMackins II <contact@mcmackins.org> | 2015-04-05 14:49:13 -0500 |
commit | 34bd35e60ab2deb9c401a801e2aaea467df43877 (patch) | |
tree | e4680655a51680b5a450cffa63514f9f4e953c11 | |
parent | 39060c3978199354e7ee29f8a02564df3773e21b (diff) |
Added CryptoCoin Charts API
-rw-r--r-- | README | 17 | ||||
-rwxr-xr-x | cccfetch | 25 | ||||
-rw-r--r-- | coinfetchapi.py | 30 |
3 files changed, 63 insertions, 9 deletions
@@ -36,6 +36,22 @@ Example: `$ coinfetch -a bter usd doge` The above example will be the same as calling `coinfetch usd doge` in version 2.x, which used the BTer API. The default API is BTC-E. +Note +---- + +BTer's ticker (as of this update) does not seem to be updating often enough to +have accurate data. Version 4.1.x adds the CryptoCoin Charts (ccc) API which +appears to have accurate data. This API is slightly more limited (it doesn't +have as many values, and the formatting does not follow the one used by BTer +and BTC-E; however, it does have support for Dogecoin which is favored by +Delwink. If you want a more accurate calculation for Dogecoin and other +altcoins, we recommend the ccc API. + +Note that the ccc API is new to coinfetch and does not match the formula +expected by the original code. Expect some bugs when using it: the numbers +should be accurate, but it may error out where the other APIs do not. Please +report bugs to [Delwink's bug tracking system][2]. + Licensing --------- @@ -44,3 +60,4 @@ licensed under the terms of version 3 of the GNU Affero General Public License. See [COPYING](COPYING) for more information. [1]: mailto:contribute@delwink.com +[2]: http://bugs.delwink.com/index.php?project=4&do=index diff --git a/cccfetch b/cccfetch new file mode 100755 index 0000000..d0bf3ce --- /dev/null +++ b/cccfetch @@ -0,0 +1,25 @@ +#! /usr/bin/env python3 +## +## coinfetch - Cryptocurrency price fetcher +## Copyright (C) 2015 Delwink, LLC +## +## This program is free software: you can redistribute it and/or modify +## it under the terms of the GNU Affero General Public License as published by +## the Free Software Foundation, version 3 only. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU Affero General Public License for more details. +## +## You should have received a copy of the GNU Affero General Public License +## along with this program. If not, see <http://www.gnu.org/licenses/>. +## + +from sys import argv +from coinfetchapi import * + +try: + coinfetch(['--api=ccc'] + argv[1:]) +except UsageException as e: + print_usage(e) diff --git a/coinfetchapi.py b/coinfetchapi.py index f2084f9..c4871fc 100644 --- a/coinfetchapi.py +++ b/coinfetchapi.py @@ -36,6 +36,7 @@ OPTIONS: Supported APIs: btce BTC-E, the default API. bter BTer, the legacy API. + ccc CryptoCoin Charts. Kinds: avg Average rate, the default. @@ -60,6 +61,8 @@ def _keypair(api, r, pair): return r.json()[pair] elif api == 'bter': return r.json() + elif api == 'ccc': + return r.json() raise ValueError('API %s not supported.' %api) @@ -68,6 +71,12 @@ def get_rate(coin_a, coin_b, amt, api, kind): url = 'https://btc-e.com/api/3/ticker/' elif api == 'bter': url = 'http://data.bter.com/api/1/ticker/' + elif api == 'ccc': + url = 'http://api.cryptocoincharts.info/tradingPair/' + if kind == 'avg': + kind = 'price' + else: + raise ValueError('kind %s not supported with API %s' %(kind, api)) else: raise ValueError('API %s not supported.' %api) @@ -78,12 +87,15 @@ def get_rate(coin_a, coin_b, amt, api, kind): try: res = _keypair(api, r, pair) return float(res[kind]) * amt - except KeyError: - pair = '%s_%s' %(coin_b, coin_a) - r = get(url + pair) + except (KeyError, TypeError): + try: + pair = '%s_%s' %(coin_b, coin_a) + r = get(url + pair) - res = _keypair(api, r, pair) - return (float(res[kind]) ** -1) * amt + res = _keypair(api, r, pair) + return (float(res[kind]) ** -1) * amt + except TypeError as e: + raise ValueError('currency pair not found: %s' %str(e)) def coinfetch(args): api = 'btce' @@ -114,9 +126,9 @@ Written by David McMackins II''') print('%.8f' %get_rate(args[bump], args[1+bump], amt, api, kind)) except IndexError: raise UsageException(1) - except KeyError: - print('coinfetch: currency pair not found', file=stderr) + except KeyError as e: + print('coinfetch: currency pair not found: %s' %str(e), file=stderr) exit(2) - except ValueError: - print('coinfetch: "%s" is not a valid number' %args[0], file=stderr) + except ValueError as e: + print('coinfetch: %s' %str(e), file=stderr) exit(3) |