-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgk.py
executable file
·183 lines (164 loc) · 5.84 KB
/
cgk.py
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/python3
"""Simple commandline tool to get a price of any coin on Coingecko and its multiples"""
import requests
from tabulate import tabulate
import numpy as np
import argparse
from colorama import Fore, Back, Style
import sys
def get_pycoingecko_ids():
"""Gets a list of all coin ids from coingecko API."""
url = 'https://api.coingecko.com/api/v3/coins/list'
r = requests.get(url)
response_dicts = r.json()
return response_dicts
def get_pycoingecko_symbols():
url = 'https://api.coingecko.com/api/v3/simple/supported_vs_currencies'
r = requests.get(url)
response_list = r.json()
return response_list
def display_id_list():
"""Downloads long list of IDs and prints them as a table."""
response_dicts = get_pycoingecko_ids()
lst = []
for dict in response_dicts:
lst.append(dict["id"])
array = get_sorted_array(lst)
print(tabulate(array))
def display_id_less():
"""A users custom choice of id currencies."""
coins = [
"bitcoin",
"ethereum",
"polkadot",
"solana",
"zcash",
"monero",
"mobilecoin",
"avalanche-2",
"ripple",
"cardano",
"terra-luna",
"matic-network",
"cosmos",
"chainlink",
"bitcoin-cash",
"bitcoin-cash-sv",
"ftx-token",
"algorand",
"ethereum-classic",
"stellar",
"tezos",
"internet-computer",
"fantom",
"dash",
"uniswap",
"zencash",
"komodo",
"gmx",
"ethereum-pow-iou",
"dopex",
"dopex-rebate-token",
"looksrare",
"mina-protocol",
"thorchain-erc20",
"thorswap",
"binancecoin",
"usd-coin",
"tether",
]
array = get_sorted_array(coins)
print(tabulate(array))
def display_vs_currencies():
"""Downloads vs_currencies and prints them as a table."""
lst = get_pycoingecko_symbols()
array = get_sorted_array(lst)
print(tabulate(array))
def get_sorted_array(lst,columns=3):
"""Convert a list of strings into a sorted array with desired n of colummns."""
lst = sorted(lst)
x = columns - len(lst)%columns
[lst.append("") for i in range(x) if x < columns]
rows = int(len(lst)/columns)
arr = np.array(lst).reshape(rows,columns)
return arr
def display_list(args):
"""Function called L subcommand in parser_main"""
if args.id_all:
display_id_list()
elif args.id_less:
display_id_less()
elif args.vs_list:
display_vs_currencies()
def panic(msg):
"""Error message print"""
print(f"error: {msg}", file=sys.stderr)
sys.exit(-1)
def get_simple_price(id,vs):
"""Gets a basic pair price from the API."""
url = \
f"https://api.coingecko.com/api/v3/simple/price?ids={id}&vs_currencies={vs}"
r = requests.get(url)
response_dict = r.json()
price = response_dict[f"{id}"][f"{vs}"]
return price
def calc_x_price(id,vs,n,switch):
"""Calculates the price based on pair and amount."""
price = get_simple_price(id,vs)
if switch == True:
x_price=1/price*n
else:
x_price = price * n
return x_price
def display_result(args):
"""Main Function printing the price of chosen crypto and amount."""
id = args.id
vs = args.vs_currency
amount = args.amount
switch = args.switch
x_price = calc_x_price(id,vs,amount,switch)
if switch == True:
msg = f"\n{amount} {vs} = {x_price} {id}\n"
else:
msg = f"\n{amount} {id} = {x_price} {vs}\n"
print(Fore.CYAN + Style.BRIGHT + msg)
def parser_main():
"""Main function initializing ArgumentParser, storing arguments and executing commands."""
# Top level parser
parser = argparse.ArgumentParser(
prog= Style.BRIGHT + 'Coingecko Commandline Convertor' + Style.RESET_ALL,
description='''Convert any asset of any amount in terminal.''',
epilog=Style.DIM + '''Let there be dark!''' + Style.RESET_ALL
)
parser.add_argument("-V","--version", action="version", version='%(prog)s 1.0.0')
# Sub-command parsers
subparsers = parser.add_subparsers(help="{subcommand}[-h] shows all the options")
parser_convert = subparsers.add_parser('convert',help='{C}[id][vs_currency]([--amount][--switch]) - example: ./cgk.py C monero btc 10', aliases=['C'])
parser_list = subparsers.add_parser('list', help='displays list of convertable coins {L}[--argument]',aliases=['L'])
# Convert - arguments
parser_convert.add_argument("id",
help="add an id of an asset to convert")
parser_convert.add_argument("vs_currency",
help="add a symbol of a currency to convert to")
parser_convert.add_argument("amount", type=float, nargs='?', default=1,
help="price multiplier (default = 1)")
parser_convert.add_argument("-s", "--switch",
action="store_true",
help="id <--> vs_currency (monero to usd --> usd to monero)")
parser_convert.set_defaults(func=display_result)
# List - arguments
parser_list.add_argument("-a","--id_all",
help="displays all convertable coin ids (over 13000 items!)", action="store_true")
parser_list.add_argument("-l","--id_less",
help="displays a shortened list of convertable coin ids", action="store_true")
parser_list.add_argument("-v","--vs_list",
help="displays all vs currencies (~60items)", action="store_true")
parser_list.set_defaults(func=display_list)
args = parser.parse_args()
try:
args.func(args)
except AttributeError as e:
msg = f"{e}.\n{Style.BRIGHT}Please run: {Fore.YELLOW}cgk --help{Style.RESET_ALL}"
panic(msg)
if __name__ == '__main__':
parser_main()