summaryrefslogtreecommitdiff
path: root/coinfetch
blob: 8edd10bac0bc00ef15f8109cf67334613626d678 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#! /usr/bin/env python3
##
##  coinfetch - Cryptocurrency price fetcher
##  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
##  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 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-2016 Delwink, LLC
License AGPLv3: GNU AGPL version 3 only <http://gnu.org/licenses/agpl.html>.
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'] = 'btce'
    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)