-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCryptoConverter.py
305 lines (236 loc) · 9.98 KB
/
CryptoConverter.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import datetime
import time
import gi
import json
import threading
import numpy as np
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GLib
from pycoingecko import CoinGeckoAPI
class ConfigUpdater(threading.Thread):
def __init__(self, handler):
threading.Thread.__init__(self, target=update_conf_files,
args=(handler,))
self.start()
class Handler:
def __init__(self, builder):
config = load_config()
self.cg = CoinGeckoAPI()
self.source_unit = builder.get_object('source_unit')
self.conv_unit = builder.get_object('conv_unit')
self.source_amount = builder.get_object('source_amount')
self.conv_result = builder.get_object('conv_result')
self.time_update = builder.get_object('time_update')
self.crypto_completion = builder.get_object('crypto_completion')
self.currency_completion = builder.get_object('currency_completion')
self.radio_api = builder.get_object('radio_api')
self.api_rate = builder.get_object('api_rate')
self.custom_rate = builder.get_object('custom_rate')
self.current_crypto = config['cryptocurrency']
self.current_currency = config['vs_currency']
self.source = 'api'
self.current_rate = None
self.auto_update = True
self.inverted_rates = False
self.crypto_ids = load_supported_cryptos()
self.currencies = load_supported_vs_currencies()
self.source_unit.set_text(self.current_crypto)
self.conv_unit.set_text(self.current_currency)
if self.crypto_ids is None or self.currencies is None:
# Download the config files directly from the API
update_conf_files(self)
else:
# Updating the UI with the config values
populate_completion(self.crypto_completion, self.crypto_ids)
populate_completion(self.currency_completion, self.currencies)
# Update the config files in a background thread
ConfigUpdater(self)
# Get and display the data received from the API
self.updateValues()
self.updateRate()
# Start the auto-updater in the background with 10s interval
GLib.timeout_add(interval=10000, function=self.updateValues)
def onDestroy(self, *args):
# Saves the active crypto and currency in the config file
config = {'vs_currency': self.current_currency.upper(),
'cryptocurrency': self.current_crypto.upper()}
with open('conf/config.json', 'w') as json_file:
json.dump(config, json_file)
Gtk.main_quit()
def toggleAutoUpdate(self, *args):
self.auto_update = not self.auto_update
def toggleInvertRates(self, *args):
self.inverted_rates = not self.inverted_rates
self.updateRate()
if self.source == 'custom':
self.convertValue()
def toggleAPI(self, *args):
self.source = 'api'
self.updateAPIValues()
def toggleCustom(self, *args):
self.source = 'custom'
self.convertValue()
def updateValues(self, *args):
if self.auto_update:
source = self.source_unit.get_text().lower()
conv = self.conv_unit.get_text().lower()
try:
amount = float(self.source_amount.get_text())
except ValueError:
return True
if source in self.crypto_ids:
cid = self.crypto_ids[source]
data = self.cg.get_price(ids=cid, vs_currencies=conv)
self.current_rate = data[cid][conv]
else:
return True
if self.source == 'api':
result = convert(self.current_rate, self.current_rate, amount,
self.source, self.inverted_rates)
self.conv_result.set_text(result)
self.updateRate()
update_time_label(self.time_update)
return True
else:
return True
def updateAPIValues(self, *args):
source = self.source_unit.get_text().lower()
conv = self.conv_unit.get_text().lower()
try:
amount = float(self.source_amount.get_text())
except ValueError:
return True
if source in self.crypto_ids:
cid = self.crypto_ids[source]
data = self.cg.get_price(ids=cid, vs_currencies=conv)
self.current_rate = data[cid][conv]
else:
return True
result = convert(self.current_rate, self.current_rate, amount,
self.source, self.inverted_rates)
self.conv_result.set_text(result)
self.updateRate()
update_time_label(self.time_update)
def convertValue(self, *args):
source = self.source_unit.get_text().lower()
conv = self.conv_unit.get_text().lower()
try:
amount = float(self.source_amount.get_text())
except ValueError:
amount = None
if self.source == 'api':
# Get the price from CG API only if crypto and/or currency changed
if self.current_crypto != source or self.current_currency != conv:
if source in self.crypto_ids:
cid = self.crypto_ids[source]
data = self.cg.get_price(ids=cid, vs_currencies=conv)
self.current_rate = data[cid][conv]
self.current_crypto = source
self.current_currency = conv
result = convert(self.current_rate, self.current_rate,
amount, self.source, self.inverted_rates)
self.conv_result.set_text(result)
self.updateRate()
else:
self.conv_result.set_text("N/A")
else:
try:
custom_rate = float(self.custom_rate.get_text())
result = convert(self.current_rate, custom_rate, amount,
self.source, self.inverted_rates)
self.conv_result.set_text(result)
except ValueError:
self.conv_result.set_text("N/A")
update_time_label(self.time_update)
def updateRate(self):
source = self.source_unit.get_text()
conv = self.conv_unit.get_text()
str_rate = np.format_float_positional(self.current_rate, trim='-')
if self.inverted_rates:
precision = len(str_rate.split(".")[1])
str_rate = round(1 / self.current_rate, precision)
str_rate = np.format_float_positional(str_rate, trim='-')
str_rate = f'{str_rate} {source}/{conv}'
else:
str_rate = f'{str_rate} {conv}/{source}'
self.api_rate.set_text(str_rate)
def load_config():
# Load the config file or the defaults if it fails
try:
with open('conf/config.json', 'r') as json_file:
config = json.load(json_file)
except FileNotFoundError:
config = {'cryptocurrency': 'BTC', 'vs_currency': 'USD'}
return config
def convert(api_rate, rate, amount, source, inverted):
# Remove the scientific notation and get the precision returned by the API
str_rate = np.format_float_positional(api_rate, trim='-')
if '.' in str_rate:
precision = len(str_rate.split('.')[1])
else:
precision = 0
if source == 'api':
result = round(amount * rate, precision)
elif inverted:
try:
result = round(amount / rate, precision)
except ZeroDivisionError:
return 'N/A'
else:
result = round(amount * rate, precision)
return np.format_float_positional(result, trim='-')
def update_time_label(label):
if time.localtime().tm_isdst:
utc_offset_sec = time.altzone
else:
utc_offset_sec = time.timezone
utc_offset = datetime.timedelta(seconds=-utc_offset_sec)
now = datetime.datetime.now()
now = now.replace(tzinfo=datetime.timezone(offset=utc_offset)).isoformat()
now = now.split('.')[0].replace('T', ' ')
label.set_text(f"Powered by CoinGecko\nLast updated: {now}")
def update_conf_files(handler):
# Fetch all the supported cryptos from CoinGecko and saves them using JSON
cg = CoinGeckoAPI()
data = cg.get_coins_list()
vs_currencies = cg.get_supported_vs_currencies()
supported_cryptos = {d['symbol']: d['id'] for d in data}
with open('conf/supported_cryptos.json', 'w') as json_file:
json.dump(supported_cryptos, json_file)
with open('conf/supported_vs_currencies.json', 'w') as json_file:
json.dump(vs_currencies, json_file)
# Update the values loaded from previous config file
handler.crypto_ids = supported_cryptos
handler.currencies = sorted([vsc.upper() for vsc in vs_currencies])
populate_completion(handler.crypto_completion, handler.crypto_ids)
populate_completion(handler.currency_completion, handler.currencies)
def load_supported_cryptos():
# Load the supported cryptocurrencies from a local JSON file (faster)
try:
with open('conf/supported_cryptos.json', 'r') as json_file:
data = json.load(json_file)
return data
except FileNotFoundError:
return None
def load_supported_vs_currencies():
try:
with open('conf/supported_vs_currencies.json', 'r') as json_file:
data = json.load(json_file)
return sorted([d.upper() for d in data])
except FileNotFoundError:
return None
def populate_completion(completion, values):
values_store = Gtk.ListStore(str)
for value in values:
values_store.append([value.upper()])
completion.set_model(values_store)
completion.set_text_column(0)
def main():
builder = Gtk.Builder()
builder.add_from_file('layout.glade')
window = builder.get_object('main_window')
builder.connect_signals(Handler(builder))
window.show_all()
Gtk.main()
if __name__ == "__main__":
main()