-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgk_conv.py
201 lines (178 loc) · 5.63 KB
/
cgk_conv.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"""Simple commandline tool to get actual price of any coin on Coingecko and its multiples"""
import sys
import requests
from tabulate import tabulate
import numpy as np
def get_pycoingecko_ids():
url = 'https://api.coingecko.com/api/v3/coins/list'
headers = {"id": "accept: application/json"}
r = requests.get(url, headers)
# print(f"Status code: {r.status_code}")
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
# Commented redundant
#def display_pycoingecko_ids():
# response_dicts = get_pycoingecko_ids()
# for dict in response_dicts:
# for x in dict:
# print(dict["id"])
#def display_pycoingecko_vs_currencies():
# response_list = get_pycoingecko_symbols()
# for x in response_list:
# print(x)
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(id,vs,amount,x_price,switch):
"""Main Function printing the price of chosen crypto and amount."""
if switch == True:
msg = f"\n{amount} {vs} = {x_price} {id}\n"
else:
msg = f"\n{amount} {id} = {x_price} {vs}\n"
print(msg)
def display_help():
"""Frontend display menu."""
msg_0 = "\nCOINGECKO CMD CONVERTOR"
line = "========================"
msg_1 = "Usage: \npython3 cgk_conv.py <id> <vs_currency> <amount>"\
"\n* Example: How much USD is 100 Monero:"\
"\n$ python3 cgk_conv.py monero usd 100"
msg_2 = "Optional argument <switch>:"\
"\n* Switch nominator and denominator."\
"\n$ python3 cgk_conv.py <id> <vs_currency> <amount> <switch>"\
"\n* Example: How much Monero is 100 USD?, type:"\
"\n$ python3 cgk_conv.py monero usd 100 switch"
msg_3 = "------------------------\nOptional Commands:"
tab = [
["$ python3 cgk_conv.py id_list","Prints all IDs (more than 13000 items!!)"],
["$ python3 cgk_conv.py id_less","Prints a short list of IDs"],
["$ python3 cgk_conv.py vs_list", "Prints a list of vs_currencies (60 items)"],
]
print(f"{msg_0}\n{line}\n{msg_1}\n{msg_2}\n{msg_3}")
print(tabulate(tab))
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 _create_sorted_table(lst):
# array = []
# new_list = []
# sorted_list = sorted(lst)
# n = 0
# for x in sorted_list:
# if n%3 == 0:
# array.append(new_list)
# new_list = []
# new_list.append(x)
#
# else:
# new_list.append(x)
# n += 1
# array.append(new_list)
# return 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 main_app():
"""Runs the main program based on passed options"""
#print(sys.argv)
if len(sys.argv) < 3:
if sys.argv[-1] == "id_list":
display_id_list()
elif sys.argv[-1] == "id_less":
display_id_less()
elif sys.argv[-1] == "vs_list":
display_vs_currencies()
else:
display_help()
else:
id = sys.argv[1].lower()
vs = sys.argv [2].lower()
if len(sys.argv) == 3 or len(sys.argv) == 4 and sys.argv[-1] == "switch":
amount = 1
else:
amount = float(sys.argv[3])
if sys.argv[-1].lower() == 'switch':
switch = True
else:
switch = False
x_price = calc_x_price(id,vs,amount,switch)
display_result(id,vs,amount,x_price,switch)
if __name__ == '__main__':
main_app()