diff options
author | David McMackins II <contact@mcmackins.org> | 2015-11-17 07:23:37 -0600 |
---|---|---|
committer | David McMackins II <contact@mcmackins.org> | 2015-11-17 07:23:37 -0600 |
commit | f75c20b984890540c715a1d106ccf901febf979f (patch) | |
tree | 67edee3d300600a3264a897cd08ff9835807e377 | |
parent | 8bfbb37b26f9c4a85ac27e04449d8f8ec1673d1b (diff) |
Add BitcoinAverage API
-rw-r--r-- | cfetch/__init__.py | 2 | ||||
-rw-r--r-- | cfetch/plugins/bitcoinaverage.py | 57 |
2 files changed, 58 insertions, 1 deletions
diff --git a/cfetch/__init__.py b/cfetch/__init__.py index 6247622..6e4f2de 100644 --- a/cfetch/__init__.py +++ b/cfetch/__init__.py @@ -21,7 +21,7 @@ from os.path import basename, dirname, exists, expanduser, isdir, join from os.path import realpath from requests import get -__version__ = '5.0.1' +__version__ = '5.1.0' ## A cryptocurrency exchange rate ticker. class Ticker(): diff --git a/cfetch/plugins/bitcoinaverage.py b/cfetch/plugins/bitcoinaverage.py new file mode 100644 index 0000000..02fb5d5 --- /dev/null +++ b/cfetch/plugins/bitcoinaverage.py @@ -0,0 +1,57 @@ +## +## coinfetch-api-bitcoinaverage - BitcoinAverage API plugin for coinfetch +## 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 cfetch import register_ticker, Ticker +from requests import get + +class BitcoinAverageTicker(Ticker): + def __init__(self, path, kind='24h_avg'): + super().__init__(path, kind) + + def get_pair_data(self, response): + return response.json() + + def get_rate(self, a, b, amt=1): + a = a.upper() + b = b.upper() + if 'BTC' not in (a, b): + self._fail(a, b) + + r = get(self.path) + res = self.get_pair_data(r) + + try: + if a == 'BTC': + if b not in res: + self._fail(a, b) + + res = res[b] + return float(res[self.kind]) * amt + + if a not in res: + self._fail(a, b) + + res = res[a] + return (float(res[self.kind]) ** -1) * amt + except (KeyError, TypeError) as e: + raise ValueError(str(e)) + + def _fail(self, a, b): + raise ValueError('{}/{}'.format(a, b)) + +register_ticker('ba', 'The BitcoinAverage ticker (built-in)', + BitcoinAverageTicker('https://api.bitcoinaverage.com/ticker/all')) |