-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·69 lines (54 loc) · 1.91 KB
/
main.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
#!./venv/bin/python3
import argparse
import json
import os
import pprint
from collections import defaultdict
from parse_history_page import parse_history_page
CURRENCY_FORMATTERS = {
"AUD": lambda a: f"${a:.2f}",
"USD": lambda a: f"${a:.2f}"
}
def is_valid_file(parser, arg):
if not os.path.exists(arg):
parser.error(f"The file {arg} does not exist!")
else:
return open(arg)
DESCRIPTION = """
Reads a Steam transaction history page and sums up how much you've spent over your account's lifetime.
You will need to download/save the page as an HTML file so this script can read it.
Load all the transactions on the page by using the "Load More Transactions" button.
Then, press Ctrl/Cmd + S to save the page.
"""
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description=DESCRIPTION)
parser.add_argument(
"--dump-json",
help="dump transaction events to given json file",
metavar="FILE",
type=lambda s: open(s, "w")
)
parser.add_argument(
"filename",
help="filename of account history page's HTML",
type=lambda s: is_valid_file(parser, s)
)
args = parser.parse_args()
content = args.filename.read()
args.filename.close()
events = parse_history_page(content)
if args.dump_json is not None:
json.dump(events, args.dump_json)
args.dump_json.close()
totals = defaultdict(lambda: 0)
for event in events:
if event["type"] == "refund":
for amount in event["amounts"]:
totals[amount["currency"]] -= amount["amount"]
else:
for amount in event["amounts"]:
totals[amount["currency"]] += amount["amount"]
print("Over the lifetime of your Steam amount, you have spent the following:")
for currency, amount in totals.items():
print(f" - {currency}: {CURRENCY_FORMATTERS[currency](amount)}")