-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtitano.py
executable file
·68 lines (57 loc) · 2.54 KB
/
titano.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
#!/usr/bin/env python3
import configparser
import sys
import requests
from decimal import Decimal
from os.path import expanduser
config = configparser.ConfigParser()
# File must be opened with utf-8 explicitly
with open(expanduser('~/.config/polybar/titano-bar/titano-config'), 'r', encoding='utf-8') as f:
config.read_file(f)
# Everything except the general section
currencies = [x for x in config.sections() if x != 'general']
base_currency = config['general']['base_currency']
params = {'convert': base_currency}
for currency in currencies:
try:
icon = config[currency]['icon']
json = requests.get(f'https://api.coingecko.com/api/v3/coins/{currency}').json()["market_data"]
local_price = round(Decimal(json["current_price"][f'{base_currency.lower()}']), 4)
# remove decimals if decimals contain four zeros
if str(local_price).split('.')[1] == "0000":
local_price = int(local_price)
print('local_price_int:',local)
# remove decimals if currency is wbnb.
if 'wbnb' in str(currency):
local_price = round(Decimal(json["current_price"][f'{base_currency.lower()}']), 0)
# remove decimals if currency is ethereum.
if 'ethereum' in str(currency):
local_price = round(Decimal(json["current_price"][f'{base_currency.lower()}']), 0)
# Set 24h % change to two decimals.
change_24 = round(float(json['price_change_percentage_24h']), 2)
display_opt = config['general']['display']
if display_opt == 'both' or display_opt == None:
sys.stdout.write(f'{icon}${local_price}|{change_24:+}%')
elif display_opt == 'small':
sys.stdout.write(f'{icon} ')
sys.stdout.write(f'{local_price}')
sys.stdout.write(f' | ')
if change_24 > 0:
# arrow up
sys.stdout.write('')
elif change_24 == 0:
# straight line
sys.stdout.write('')
else:
# arrow down
sys.stdout.write('')
sys.stdout.write(f'{change_24: }%')
elif display_opt == 'percentage':
sys.stdout.write(f'{icon} {change_24:+}%')
elif display_opt == 'price':
sys.stdout.write(f'{icon} {local_price}')
if currency != currencies[-1]:
sys.stdout.write(' ')
except requests.exceptions.ConnectionError as e:
sys.stdout.write('not connected')
break