-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_balances_display.py
More file actions
66 lines (64 loc) · 1.74 KB
/
format_balances_display.py
File metadata and controls
66 lines (64 loc) · 1.74 KB
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
#!/usr/bin/env python3
from pathlib import Path
import re
src = Path('/home/felix/youtubestream/balances.txt')
out_dir = Path('/tmp/youtube_stream')
out_dir.mkdir(parents=True, exist_ok=True)
if not src.exists():
print('balances.txt missing')
raise SystemExit(1)
lines = src.read_text().strip().splitlines()
bal_lines = []
pos_lines = []
for line in lines:
if not line.strip():
continue
if line.upper().startswith('TOTAL'):
# Will handle later
total_line = line
continue
# Example: ETH: 0.06698957 - 117.86EUR
m = re.match(r"^([^:]+):\s*(.+)$", line)
if not m:
continue
asset = m.group(1).strip()
rest = m.group(2).strip()
if ' - ' in rest:
parts = rest.split(' - ')
qty = parts[0].strip()
eur = parts[1].replace('EUR','').strip()
try:
qtyf = float(qty)
except:
qtyf = 0.0
try:
eurf = float(eur)
except:
eurf = 0.0
bal_lines.append(f"{asset}: {qtyf:.2f} - {eurf:.2f}EUR")
pos_lines.append(f"{asset} EUR: {qtyf:.2f}")
else:
# e.g. EUR: 88.45
try:
valf = float(rest.replace('EUR','').strip())
except:
valf = 0.0
bal_lines.append(f"{asset}: {valf:.2f}")
# total
if 'total_line' in locals():
m = re.search(r"([0-9]+\.?[0-9]*)", total_line)
if m:
total = float(m.group(1))
else:
total = 0.0
else:
total = 0.0
# write outputs
with open(out_dir / 'data_balances.txt', 'w') as f:
for l in bal_lines:
f.write(l + "\n")
f.write('\n')
f.write(f"TOTAL: {total:.2f} EUR\n")
with open(out_dir / 'data_positions.txt', 'w') as f:
for l in pos_lines:
f.write(l + "\n")