summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid McMackins II <contact@mcmackins.org>2016-05-25 15:21:23 -0500
committerDavid McMackins II <contact@mcmackins.org>2016-05-25 15:21:23 -0500
commit5b062e44e9023d36cddcf0ab3d8a3d87e7330178 (patch)
tree84609318ab491c8266ab3526de1f732987059790
parent66fdebe8e8c70ca3ab08f406337e8292d5f34b36 (diff)
Fix 'kind' option to actually work
-rw-r--r--cfetch/__init__.py15
-rw-r--r--cfetch/plugins/bitcoinaverage.py12
-rw-r--r--cfetch/plugins/bitstamp.py21
-rw-r--r--cfetch/plugins/ccc.py9
-rwxr-xr-xcoinfetch14
5 files changed, 42 insertions, 29 deletions
diff --git a/cfetch/__init__.py b/cfetch/__init__.py
index 40dea8d..db65f7d 100644
--- a/cfetch/__init__.py
+++ b/cfetch/__init__.py
@@ -1,6 +1,6 @@
##
## coinfetch - plugin-based cryptocurrency price converter
-## Copyright (C) 2015 Delwink, LLC
+## Copyright (C) 2015-2016 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
@@ -28,9 +28,8 @@ class Ticker():
## Constructor for this class.
# @param path The URL prefix for this ticker.
# @param kind Which type of exchange rate to fetch.
- def __init__(self, path, kind='avg'):
+ def __init__(self, path):
self.path = path
- self.kind = kind
## Defines how a currency pair is defined in the request URL.
# @param a The first currency.
@@ -49,22 +48,22 @@ class Ticker():
else:
raise TypeError('pair cannot be {}'.format(type(pair)))
- def _get_single_rate(self, a, b, amt, power):
+ def _get_single_rate(self, a, b, amt, power, kind):
r = get(self.path + self.get_pair(a, b))
res = self.get_pair_data(r, (a, b))
- return (float(res[self.kind]) ** power) * amt
+ return (float(res[kind]) ** power) * amt
## Calculates the exchange rate between two currencies.
# @param a The first currency.
# @param b The second currency.
# @param amt The number quantity of 'a' currency.
# @return The exchange rate between 'a' and 'b' currencies.
- def get_rate(self, a, b, amt=1):
+ def get_rate(self, a, b, amt=1, kind='avg'):
try:
- return self._get_single_rate(a, b, amt, 1)
+ return self._get_single_rate(a, b, amt, 1, kind)
except (KeyError, TypeError):
try:
- return self._get_single_rate(b, a, amt, -1)
+ return self._get_single_rate(b, a, amt, -1, kind)
except (KeyError, TypeError) as e:
raise ValueError(str(e)) # currency pair not found
diff --git a/cfetch/plugins/bitcoinaverage.py b/cfetch/plugins/bitcoinaverage.py
index 02fb5d5..df1cebd 100644
--- a/cfetch/plugins/bitcoinaverage.py
+++ b/cfetch/plugins/bitcoinaverage.py
@@ -1,6 +1,6 @@
##
## coinfetch-api-bitcoinaverage - BitcoinAverage API plugin for coinfetch
-## Copyright (C) 2015 Delwink, LLC
+## Copyright (C) 2015-2016 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
@@ -19,13 +19,13 @@ 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 __init__(self, path):
+ super().__init__(path)
def get_pair_data(self, response):
return response.json()
- def get_rate(self, a, b, amt=1):
+ def get_rate(self, a, b, amt=1, kind='24h_avg'):
a = a.upper()
b = b.upper()
if 'BTC' not in (a, b):
@@ -40,13 +40,13 @@ class BitcoinAverageTicker(Ticker):
self._fail(a, b)
res = res[b]
- return float(res[self.kind]) * amt
+ return float(res[kind]) * amt
if a not in res:
self._fail(a, b)
res = res[a]
- return (float(res[self.kind]) ** -1) * amt
+ return (float(res[kind]) ** -1) * amt
except (KeyError, TypeError) as e:
raise ValueError(str(e))
diff --git a/cfetch/plugins/bitstamp.py b/cfetch/plugins/bitstamp.py
index 50f81c3..7af3883 100644
--- a/cfetch/plugins/bitstamp.py
+++ b/cfetch/plugins/bitstamp.py
@@ -1,6 +1,6 @@
##
## coinfetch-api-bitstamp - Bitstamp API plugin for coinfetch
-## Copyright (C) 2015 Delwink, LLC
+## Copyright (C) 2015-2016 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
@@ -19,21 +19,22 @@ from cfetch import register_ticker, Ticker
from requests import get
class BitstampTicker(Ticker):
- def __init__(self, path, kind='vwap'):
- super().__init__(path, kind)
+ def __init__(self, path):
+ super().__init__(path)
def get_pair_data(self, response):
return response.json()
- def get_rate(self, a, b, amt=1):
+ def get_rate(self, a, b, amt=1, kind='vwap'):
+ r = get(self.path)
+ res = self.get_pair_data(r)
+ if kind not in res:
+ raise ValueError('Kind {} not available'.format(kind))
+
if a == 'btc' and b == 'usd':
- r = get(self.path)
- res = self.get_pair_data(r)
- return float(res[self.kind]) * amt
+ return float(res[kind]) * amt
elif a == 'usd' and b == 'btc':
- r = get(self.path)
- res = self.get_pair_data(r)
- return (float(res[self.kind]) ** -1) * amt
+ return (float(res[kind]) ** -1) * amt
else:
raise ValueError('{}/{}'.format(a, b))
diff --git a/cfetch/plugins/ccc.py b/cfetch/plugins/ccc.py
index 309b07a..07bdb99 100644
--- a/cfetch/plugins/ccc.py
+++ b/cfetch/plugins/ccc.py
@@ -1,6 +1,6 @@
##
## coinfetch-api-ccc - CryptoCoin Charts API plugin for coinfetch
-## Copyright (C) 2015 Delwink, LLC
+## Copyright (C) 2015-2016 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
@@ -18,11 +18,14 @@
from cfetch import register_ticker, Ticker
class CccTicker(Ticker):
- def __init__(self, path, kind='price'):
- super().__init__(path, kind)
+ def __init__(self, path):
+ super().__init__(path)
def get_pair_data(self, response, pair=None):
return response.json()
+ def get_rate(self, a, b, amt=1, kind='price'):
+ return super().get_rate(a, b, amt, kind)
+
register_ticker('ccc', 'The CryptoCoin Charts ticker (built-in)',
CccTicker('http://api.cryptocoincharts.info/tradingPair/'))
diff --git a/coinfetch b/coinfetch
index a8df456..6618ed6 100755
--- a/coinfetch
+++ b/coinfetch
@@ -98,9 +98,19 @@ args = cli.parse_args()
try:
ticker = get_ticker(args.api)
- rate = ticker.get_rate(args.src, args.dest, args.amount)
+
+ rate_args = [args.src, args.dest, args.amount]
+ if args.kind:
+ rate_args.append(args.kind)
+
+ rate = ticker.get_rate(*rate_args)
print(format(rate, '.8f'))
except ValueError:
+ if args.kind:
+ print('The', args.kind, 'rate is not available with the', args.api,
+ 'API.')
+ exit(10)
+
pair = '/'.join([args.src, args.dest])
- print('The pair {} was not found using the {} API.'.format(pair, args.api))
+ print('The pair', pair, 'was not found using the', args.api, 'API.')
exit(11)