forked from mintapi/mintapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
464 lines (431 loc) · 14.3 KB
/
cli.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import atexit
import logging
import os
import sys
import json
import getpass
import keyring
import configargparse
from mintapi.api import Mint
from mintapi.signIn import get_email_code
from pandas import json_normalize
logger = logging.getLogger("mintapi")
def parse_arguments(args):
ARGUMENTS = [
(
("email",),
{
"nargs": "?",
"default": None,
"help": "The e-mail address for your Mint.com account",
},
),
(
("password",),
{
"nargs": "?",
"default": None,
"help": "The password for your Mint.com account",
},
),
(
("--accounts",),
{
"action": "store_true",
"dest": "accounts",
"default": False,
"help": "Retrieve account information (default if nothing else is specified)",
},
),
(
("--attention",),
{
"action": "store_true",
"help": "Display accounts that need attention (None if none).",
},
),
(
("--budgets",),
{
"action": "store_true",
"dest": "budgets",
"default": False,
"help": "Retrieve budget information",
},
),
(
("--budget_hist",),
{
"action": "store_true",
"dest": "budget_hist",
"default": None,
"help": "Retrieve 12-month budget history information",
},
),
(
("--categories",),
{
"action": "store_true",
"default": False,
"help": "Retrieve category definitions as configured in Mint",
},
),
(
("--chromedriver-download-path",),
{
"default": os.getcwd(),
"help": "The directory to download chromedrive to.",
},
),
(
("--config-file", "-c"),
{
"required": False,
"is_config_file": True,
"help": "The path to the config file used.",
},
),
(
("--credit-report",),
{
"action": "store_true",
"dest": "credit_report",
"default": False,
"help": "Retrieve full credit report",
},
),
(
("--credit-score",),
{
"action": "store_true",
"dest": "credit_score",
"default": False,
"help": "Retrieve current credit score",
},
),
(
("--end-date",),
{
"nargs": "?",
"default": None,
"help": "Latest date for transactions to be retrieved from. Used with --transactions. Format: mm/dd/yy",
},
),
(
("--exclude-accounts",),
{
"action": "store_true",
"default": False,
"help": "When accessing credit report details, exclude data related to credit accounts. Used with --credit-report.",
},
),
(
("--exclude-inquiries",),
{
"action": "store_true",
"default": False,
"help": "When accessing credit report details, exclude data related to credit inquiries. Used with --credit-report.",
},
),
(
("--exclude-utilization",),
{
"action": "store_true",
"default": False,
"help": "When accessing credit report details, exclude data related to credit utilization. Used with --credit-report.",
},
),
(
("--filename", "-f"),
{
"help": "write results to file. can be {csv,json} format. default is to write to stdout."
},
),
(
("--headless",),
{
"action": "store_true",
"help": "Whether to execute chromedriver with no visible window.",
},
),
(("--imap-account",), {"default": None, "help": "IMAP login account"}),
(("--imap-folder",), {"default": "INBOX", "help": "IMAP folder"}),
(("--imap-password",), {"default": None, "help": "IMAP login password"}),
(("--imap-server",), {"default": None, "help": "IMAP server"}),
(
("--imap-test",),
{"action": "store_true", "help": "Test imap login and retrieval."},
),
(
("--investments",),
{
"action": "store_true",
"default": False,
"help": "Retrieve data related to your investments, whether they be retirement or personal stock purchases",
},
),
(
("--include-investment",),
{
"action": "store_true",
"default": False,
"help": "Used with --transactions",
},
),
(
("--keyring",),
{
"action": "store_true",
"help": "Use OS keyring for storing password information",
},
),
(
("--mfa-method",),
{
"choices": ["sms", "email", "soft-token"],
"default": "sms",
"help": "The MFA method to automate.",
},
),
(
("--mfa-token",),
{"default": None, "help": "The MFA soft-token to pass to oathtool."},
),
(
("--net-worth",),
{
"action": "store_true",
"dest": "net_worth",
"default": False,
"help": "Retrieve net worth information",
},
),
(
("--no_wait_for_sync",),
{
"action": "store_true",
"default": False,
"help": "By default, mint api will wait for accounts to sync with the backing financial institutions. If this flag is present, do not wait for them to sync.",
},
),
(
("--session-path",),
{
"nargs": "?",
"default": os.path.join(os.path.expanduser("~"), ".mintapi", "session"),
"help": "Directory to save browser session, including cookies. Used to prevent repeated MFA prompts. Defaults to $HOME/.mintapi/session. Set to None to use a temporary profile.",
},
),
# Displayed to the user as a postive switch, but processed back here as a negative
(
("--show-pending",),
{
"action": "store_false",
"default": True,
"help": "Exclude pending transactions from being retrieved. Used with --transactions",
},
),
(
("--start-date",),
{
"nargs": "?",
"default": None,
"help": "Earliest date for transactions to be retrieved from. Used with --transactions. Format: mm/dd/yy",
},
),
(
("--transactions", "-t"),
{"action": "store_true", "default": False, "help": "Retrieve transactions"},
),
(
("--use-chromedriver-on-path",),
{
"action": "store_true",
"help": "Whether to use the chromedriver on PATH, instead of downloading a local copy.",
},
),
(
("--wait_for_sync_timeout",),
{
"type": int,
"default": 5 * 60,
"help": "Number of seconds to wait for sync. Default is 5 minutes",
},
),
]
# Parse command-line arguments {{{
cmdline = configargparse.ArgumentParser()
for argument_commands, argument_options in ARGUMENTS:
cmdline.add_argument(*argument_commands, **argument_options)
return cmdline.parse_args(args)
def handle_password(type, prompt, email, password, use_keyring=False):
if use_keyring and not password:
# If we don't yet have a password, try prompting for it
password = keyring.get_password(type, email)
if not password:
# If we still don't have a password, prompt for it
password = getpass.getpass("Mint password: ")
if use_keyring:
# If keyring option is specified, save the password in the keyring
keyring.set_password(type, email, password)
return password
def validate_file_extensions(options):
if any(
[
options.transactions,
options.investments,
]
):
if not (
options.filename is None
or options.filename.endswith(".csv")
or options.filename.endswith(".json")
):
raise ValueError(
"File extension must be either .csv or .json for transaction data"
)
else:
if not (options.filename is None or options.filename.endswith(".json")):
raise ValueError("File extension must be .json for non-transaction data")
def output_data(options, data, attention_msg=None):
if options.filename is None:
print(json.dumps(data, indent=2))
# NOTE: While this logic is here, unless validate_file_extensions
# allows for other data types to export to CSV, this will
# only include investment data.
elif options.filename.endswith(".csv"):
# NOTE: Currently, investment_data, which is a flat JSON, is the only
# type of data that uses this section. So, if we open this up to
# other non-flat JSON data, we will need to revisit this.
json_normalize(data).to_csv(options.filename, index=False)
elif options.filename.endswith(".json"):
with open(options.filename, "w+") as f:
json.dump(data, f, indent=2)
if options.attention:
if attention_msg is None or attention_msg == "":
attention_msg = "no messages"
if options.filename is None:
print(attention_msg)
else:
with open(options.filename, "w+") as f:
f.write(attention_msg)
def main():
options = parse_arguments(sys.argv[1:])
# Try to get the e-mail and password from the arguments
email = options.email
password = options.password
imap_account = options.imap_account
imap_password = options.imap_password
mfa_method = options.mfa_method
validate_file_extensions(options)
if not email:
# If the user did not provide an e-mail, prompt for it
email = input("Mint e-mail: ")
password = handle_password(
"mintapi", "Mint password: ", email, password, options.keyring
)
if mfa_method == "email" and imap_account:
imap_password = handle_password(
"mintapi_imap",
"IMAP password: ",
imap_account,
imap_password,
options.keyring,
)
if not any(
[
options.accounts,
options.budgets,
options.transactions,
options.net_worth,
options.credit_score,
options.credit_report,
options.investments,
options.attention,
options.categories,
]
):
options.accounts = True
if options.session_path == "None":
session_path = None
else:
session_path = options.session_path
mint = Mint(
email,
password,
mfa_method=mfa_method,
mfa_token=options.mfa_token,
session_path=session_path,
headless=options.headless,
imap_account=imap_account,
imap_password=imap_password,
imap_server=options.imap_server,
imap_folder=options.imap_folder,
wait_for_sync=not options.no_wait_for_sync,
wait_for_sync_timeout=options.wait_for_sync_timeout,
use_chromedriver_on_path=options.use_chromedriver_on_path,
chromedriver_download_path=options.chromedriver_download_path,
)
atexit.register(mint.close) # Ensure everything is torn down.
if options.imap_test:
mfa_code = get_email_code(
imap_account,
imap_password,
options.imap_server,
imap_folder=options.imap_folder,
delete=False,
)
print("MFA CODE:", mfa_code)
sys.exit()
data = None
if options.accounts and options.budgets:
try:
data = mint.get_account_data()
except Exception:
accounts = None
try:
budgets = mint.get_budgets()
except Exception:
budgets = None
data = {"accounts": accounts, "budgets": budgets}
elif options.budgets:
try:
data = mint.get_budgets()
except Exception:
data = None
elif options.budget_hist:
try:
data = mint.get_budgets(hist=12)
except Exception:
data = None
elif options.accounts:
try:
data = mint.get_account_data()
except Exception:
data = None
elif options.transactions:
data = mint.get_transaction_data(
start_date=options.start_date,
end_date=options.end_date,
include_investment=options.include_investment,
remove_pending=options.show_pending,
)
elif options.categories:
data = mint.get_categories()
elif options.investments:
data = mint.get_investment_data()
elif options.net_worth:
data = mint.get_net_worth()
elif options.credit_score:
data = mint.get_credit_score()
elif options.credit_report:
data = mint.get_credit_report(
details=True,
exclude_inquiries=options.exclude_inquiries,
exclude_accounts=options.exclude_accounts,
exclude_utilization=options.exclude_utilization,
)
attention_msg = None
if options.attention:
attention_msg = mint.get_attention()
output_data(options, data, attention_msg)