-
Notifications
You must be signed in to change notification settings - Fork 0
/
balance.py
81 lines (60 loc) · 2.29 KB
/
balance.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
import sys
import time
import logging
from argparse import ArgumentParser
from traceback import format_exc
from os.path import expanduser, isfile
from config import CONFIG
from sources import source_classes
from graphs import Graphs
def main():
try:
# Initialization
args = handle_args()
init_logging(args)
# Getting data from external resources
balances = []
for s in args.sources:
src = source_classes[s]()
balance = src.get_balance()
balances.append((s, balance))
logging.info(u'%s: %s', s, balance)
# Write received data (and only if all sources returned data successfully)
now_str = time.strftime(u'%d.%m.%Y %H:%M')
for source, balance in balances:
log_fname = u'logs/%s.log' % source
with open(log_fname, u'a') as f:
f.write(u'%s, %.2f\n' % (now_str, balance.value))
logging.info(u'Updated file %s' % log_fname)
# Generate html file with fancy graphs
Graphs().generate_html()
except Exception as e:
# We want to notify user that error happened
write_exception(e)
raise
def init_logging(args):
if args.verbose:
level = logging.DEBUG
else:
level = logging.INFO
logging.basicConfig(format=u'%(message)s', level=level)
def handle_args():
parser = ArgumentParser(description=u'Get data from sites (like phone balance) using given modules')
parser.add_argument(u'sources', nargs=u'+', choices=source_classes.keys(), help=u'Sources to get data from')
parser.add_argument(u'--verbose', action=u'store_true', help=u'Be verbose when getting data')
return parser.parse_args()
def write_exception(exc):
"""Create file on desktop with traceback to notify user about error"""
desktop_folder = u'%s/Desktop' % (expanduser(u'~%s' % CONFIG[u'desktop_user']))
msg = u'Command line arguments: %s\n' % u' '.join(sys.argv)
msg += format_exc()
# Create new file for each error
i = 1
fname = u'%s/BALANCE-HISTORY-FAILED.txt' % desktop_folder
while isfile(fname):
fname = u'%s/BALANCE-HISTORY-FAILED-%d.txt' % (desktop_folder, i)
i += 1
with open(fname, u'w') as f:
f.write(msg)
if __name__ == u'__main__':
main()