From 14b34d6cc0c3621efd4aae2ff59f21e6c865ea74 Mon Sep 17 00:00:00 2001 From: David McMackins II Date: Tue, 19 Sep 2017 08:09:26 -0500 Subject: Add Coinbase API and make it default --- README | 22 +++++++++------- cfetch/plugins/cb.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ coinfetch | 49 +++++++++++++++++++++++++---------- 3 files changed, 122 insertions(+), 22 deletions(-) create mode 100644 cfetch/plugins/cb.py diff --git a/README b/README index 1027783..8382397 100644 --- a/README +++ b/README @@ -34,7 +34,7 @@ with the `-a` option. 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. +2.x, which used the BTer API. The default API is Coinbase. For help using `coinfetch` and for a list of options, use `coinfetch --help`. @@ -80,17 +80,21 @@ 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]. +Version 5.3 changes the default API to Coinbase. Coinbase appears to be +well-established and unlikely to disappear. The other APIs previously added to +Coinfetch have been unreliable at best and broken at worst. The goal with this +project is to have Coinfetch work out of the box to give pricing data for the +most popular currencies (which Coinbase supports) and to optionally support +other currencies (as offered by the alternative APIs). + +Please report bugs to [Delwink's bug tracking system][2]. Licensing --------- -This script is [free software](http://gnu.org/philosophy/free-sw.html), -licensed under the terms of version 3 of the GNU Affero General Public -License. See [COPYING](COPYING) for more information. +This script is [free software](http://gnu.org/p/free-sw.html), licensed under +the terms of version 0.5 of the Copyfree Open Innovation License. See +[COPYING](COPYING) for more information. [1]: mailto:contribute@delwink.com -[2]: http://bugs.delwink.com/index.php?project=4&do=index +[2]: https://delwink.com/bugs diff --git a/cfetch/plugins/cb.py b/cfetch/plugins/cb.py new file mode 100644 index 0000000..fd531ed --- /dev/null +++ b/cfetch/plugins/cb.py @@ -0,0 +1,73 @@ +## +## coinfetch-api-cb - Coinbase API plugin for coinfetch +## Copyright (C) 2017 Delwink, LLC +## +## Redistributions, modified or unmodified, in whole or in part, must retain +## applicable copyright or other legal privilege notices, these conditions, and +## the following license terms and disclaimer. Subject to these conditions, +## the holder(s) of copyright or other legal privileges, author(s) or +## assembler(s), and contributors of this work hereby grant to any person who +## obtains a copy of this work in any form: +## +## 1. Permission to reproduce, modify, distribute, publish, sell, sublicense, +## use, and/or otherwise deal in the licensed material without restriction. +## +## 2. A perpetual, worldwide, non-exclusive, royalty-free, irrevocable patent +## license to reproduce, modify, distribute, publish, sell, use, and/or +## otherwise deal in the licensed material without restriction, for any and all +## patents: +## +## a. Held by each such holder of copyright or other legal privilege, +## author or assembler, or contributor, necessarily infringed by the +## contributions alone or by combination with the work, of that privilege +## holder, author or assembler, or contributor. +## +## b. Necessarily infringed by the work at the time that holder of +## copyright or other privilege, author or assembler, or contributor made +## any contribution to the work. +## +## NO WARRANTY OF ANY KIND IS IMPLIED BY, OR SHOULD BE INFERRED FROM, THIS +## LICENSE OR THE ACT OF DISTRIBUTION UNDER THE TERMS OF THIS LICENSE, +## INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR +## A PARTICULAR PURPOSE, AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, +## ASSEMBLERS, OR HOLDERS OF COPYRIGHT OR OTHER LEGAL PRIVILEGE BE LIABLE FOR +## ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN ACTION OF CONTRACT, TORT, +## OR OTHERWISE ARISING FROM, OUT OF, OR IN CONNECTION WITH THE WORK OR THE USE +## OF OR OTHER DEALINGS IN THE WORK. +## + +from cfetch import register_ticker, NoSuchKindException, NoSuchPairException +from cfetch import Ticker + +class CoinbaseTicker(Ticker): + def __init__(self, path): + super().__init__(path) + + def get_pair(self, a, b): + return '{}-{}'.format(a.upper(), b.upper()) + + def get_pair_data(self, response, pair=None): + return response.json() + + def get_rate_pow(self, a, b, amt, power, kind): + if kind not in ('spot', 'buy', 'sell'): + raise NoSuchKindException(kind) + + r = get(self.path + self.get_pair(a, b) + '/' + kind, + headers={'CB-VERSION': '2017-09-19'}) + + res = self.get_pair_data(r, (a, b)) + print(r.text) + if 'data' not in res: + raise NoSuchPairException('{}/{}'.format(a, b)) + + if 'errors' in res: + raise Exception(res['errors']) + + return (float(res['data']['amount']) ** power) * amt + + def get_rate(self, a, b, amt=1, kind='spot'): + return super().get_rate(a, b, amt, kind) + +register_ticker('cb', 'The Coinbase ticker (built-in)', + CoinbaseTicker('https://api.coinbase.com/v2/prices/')) diff --git a/coinfetch b/coinfetch index 8edd10b..279ce0e 100755 --- a/coinfetch +++ b/coinfetch @@ -1,19 +1,40 @@ #! /usr/bin/env python3 ## ## coinfetch - Cryptocurrency price fetcher -## Copyright (C) 2015-2016 Delwink, LLC +## Copyright (C) 2015-2017 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. +## Redistributions, modified or unmodified, in whole or in part, must retain +## applicable copyright or other legal privilege notices, these conditions, and +## the following license terms and disclaimer. Subject to these conditions, +## the holder(s) of copyright or other legal privileges, author(s) or +## assembler(s), and contributors of this work hereby grant to any person who +## obtains a copy of this work in any form: ## -## 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. +## 1. Permission to reproduce, modify, distribute, publish, sell, sublicense, +## use, and/or otherwise deal in the licensed material without restriction. ## -## You should have received a copy of the GNU Affero General Public License -## along with this program. If not, see . +## 2. A perpetual, worldwide, non-exclusive, royalty-free, irrevocable patent +## license to reproduce, modify, distribute, publish, sell, use, and/or +## otherwise deal in the licensed material without restriction, for any and all +## patents: +## +## a. Held by each such holder of copyright or other legal privilege, +## author or assembler, or contributor, necessarily infringed by the +## contributions alone or by combination with the work, of that privilege +## holder, author or assembler, or contributor. +## +## b. Necessarily infringed by the work at the time that holder of +## copyright or other privilege, author or assembler, or contributor made +## any contribution to the work. +## +## NO WARRANTY OF ANY KIND IS IMPLIED BY, OR SHOULD BE INFERRED FROM, THIS +## LICENSE OR THE ACT OF DISTRIBUTION UNDER THE TERMS OF THIS LICENSE, +## INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR +## A PARTICULAR PURPOSE, AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, +## ASSEMBLERS, OR HOLDERS OF COPYRIGHT OR OTHER LEGAL PRIVILEGE BE LIABLE FOR +## ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN ACTION OF CONTRACT, TORT, +## OR OTHERWISE ARISING FROM, OUT OF, OR IN CONNECTION WITH THE WORK OR THE USE +## OF OR OTHER DEALINGS IN THE WORK. ## from argparse import Action, ArgumentParser @@ -27,8 +48,10 @@ from os import makedirs __title__ = 'coinfetch' __author__ = 'David McMackins II' __version_info__ = '''{} {} -Copyright (C) 2015-2016 Delwink, LLC -License AGPLv3: GNU AGPL version 3 only . +Copyright (C) 2015-2017 Delwink, LLC +License COIL: Copyfree Open Innovation License 0.5 + + This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. @@ -43,7 +66,7 @@ if not exists(CONFIG_PATH): makedirs(CONFIG_DIR) config['coinfetch'] = {} - config['coinfetch']['api'] = 'btce' + config['coinfetch']['api'] = 'cb' with open(CONFIG_PATH, 'w') as f: config.write(f) else: -- cgit v1.2.3