#! /usr/bin/env python3 ## ## coinfetch - Cryptocurrency price fetcher ## Copyright (C) 2015-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 argparse import Action, ArgumentParser from configparser import ConfigParser from cfetch import __version__, get_ticker, get_registered_tickers from cfetch import load_default_plugins, NoSuchKindException from cfetch import NoSuchPairException from os.path import expanduser, join, exists from os import makedirs __title__ = 'coinfetch' __author__ = 'David McMackins II' __version_info__ = '''{} {} 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. Written by {}'''.format(__title__, __version__, __author__) CONFIG_DIR = join(expanduser('~'), '.coinfetch') CONFIG_PATH = join(CONFIG_DIR, 'config.ini') config = ConfigParser() if not exists(CONFIG_PATH): if not exists(CONFIG_DIR): makedirs(CONFIG_DIR) config['coinfetch'] = {} config['coinfetch']['api'] = 'cb' with open(CONFIG_PATH, 'w') as f: config.write(f) else: config.read(CONFIG_PATH) default_api = config['coinfetch']['api'] load_default_plugins() tickers = get_registered_tickers() tickers.sort() apis = [api for api, desc in tickers] class DefaultApiAction(Action): def __call__(self, parser, values, namespace, option_string): config['coinfetch']['api'] = namespace with open(CONFIG_PATH, 'w') as f: config.write(f) print('Default API set to', namespace) exit(0) class ListAction(Action): def __call__(self, parser, values, namespace, option_string): for api, desc in tickers: if api == default_api: desc += ' (default)' print(api + '\t- ' + desc) exit(0) class VersionAction(Action): def __call__(self, parser, values, namespace, option_string): print(__version_info__) exit(0) cli = ArgumentParser(__title__) cli.add_argument('-a', '--api', default=default_api, choices=apis, help='uses an API by name') cli.add_argument('-k', '--kind', help='specifies which kind of rate to get') cli.add_argument('-l', '--list-apis', action=ListAction, help='list available APIs and exit', nargs=0) cli.add_argument('--set-default', action=DefaultApiAction, choices=apis, help='sets the default API and exits') cli.add_argument('-v', '--version', action=VersionAction, help='show version information and exit', nargs=0) cli.add_argument('amount', default=1, help='amount of the original currency', nargs='?', type=float) cli.add_argument('src', help='currency from which to convert') cli.add_argument('dest', help='currency to which to convert') args = cli.parse_args() try: ticker = get_ticker(args.api) 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 NoSuchKindException: print('The', args.kind, 'rate is not available with the', args.api, 'API.') exit(10) except NoSuchPairException: pair = '/'.join([args.src, args.dest]) print('The pair', pair, 'was not found using the', args.api, 'API.') exit(11)