-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
116 lines (95 loc) · 3.83 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
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
import tabula
# from tabulate import tabulate
import io
import logging
import argparse
import datetime
#For SCB
import scb_parser
#For MoneyWiz
import webbrowser
import moneywiz_url_parser
import csv
# Logger Setup
logging.basicConfig(format='%(levelname)s %(filename)s:%(lineno)s : %(message)s', level=logging.WARNING)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# Arg Setup
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description="Generate cost and usage report for the last 3 month grouped by service")
parser.add_argument('--infile', type=str, default=f'./data/AcctSt.pdf', help="Input file name")
parser.add_argument('--outfile', type=str, default=f'./data/acc_bnk_pst-{datetime.date.today()}.csv', help="Output file name")
parser.add_argument('--password', type=str, default="XXXXXXX", help="PDF File Password")
parser.add_argument('--account', type=str, default="TEST", help="MoneyWiz Account Name")
parser.add_argument('--currency', type=str, default="THB", help="MoneyWiz Currency")
parser.add_argument('--save' , action="store_true", help="Save MoneyWiz Transaction")
parser.add_argument('--csv', action="store_true", help="Generate CSV, skip MoneyWiz URLs")
parser.add_argument('--debug', action="store_true", help="Print debug info")
args = parser.parse_args()
if args.debug:
logger.setLevel(logging.DEBUG)
report_file_name = args.outfile
# Read PDF file
source = tabula.read_pdf(args.infile, pages="all" , password=args.password, guess=False, stream=True, multiple_tables=True, pandas_options={'header': None})
# Parse SCB Transactions
scb_parsed_transaction_data = scb_parser.parse_transations(source=source)
total = scb_parser.get_total(source=source)
is_amounts_verified = scb_parser.verify_amounts(scb_parsed_transaction_data, total)
if not is_amounts_verified:
logger.error("Total amount is not equal to sum of transactions")
exit(1)
# After we have to export it to CSV or try to execute MoneyWiz import URLs
# We have some succcess with MoneyWiz URLs. Code below just works. We need another attemt to make it done.
# Preprocess moneywiz_data using a list comprehension
preprocessed_data_for_moneywiz = [
{
"account": args.account,
"amount": row["Amount"],
"currency": args.currency,
"date": row["Date"] + " " + row["Time"],
"payee": row["Description"],
"memo": row["Notes"] + " Code:" + row["Code/Channel"],
"save": args.save
}
for _, row in scb_parsed_transaction_data.iterrows()
]
# Preprocess csv_data using a list comprehension
preprocessed_data_for_csv = [
{
"Account": args.account,
"Amount": row["Amount"],
"Date": row["Date"],
"Payee": row["Description"],
"Memo": row["Notes"] + " Code: " + row["Code/Channel"] + " Time: " + row["Time"],
}
for _, row in scb_parsed_transaction_data.iterrows()
]
# Process the preprocessed_data using get_moneywiz_url
moneywiz_urls = [
moneywiz_url_parser.get_moneywiz_url(
data["account"],
data["amount"],
data["currency"],
data["date"],
payee=data["payee"],
memo=data["memo"],
save=data["save"]
)
for data in preprocessed_data_for_moneywiz
]
# Write transacations directly to MoneyWiz or create CSV file
if args.csv:
logger.debug("Generate CSV file")
with open(args.outfile, 'w', newline='') as csvfile:
fieldnames = ["Account", "Amount", "Date", "Payee", "Memo"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
# Write the header
writer.writeheader()
# Write the rows
for data in preprocessed_data_for_csv:
writer.writerow(data)
else:
logger.debug("Open MoneyWiz URLs")
for url in moneywiz_urls:
logger.debug(url)
webbrowser.open(url)