-
Notifications
You must be signed in to change notification settings - Fork 278
/
api.py
571 lines (479 loc) · 18.3 KB
/
api.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
from datetime import date, datetime
from dateutil.relativedelta import relativedelta
import logging
import os
import random
import re
import requests
import time
import warnings
from mintapi.signIn import sign_in, _create_web_driver_at_mint_com
logger = logging.getLogger("mintapi")
def json_date_to_datetime(dateraw):
cy = date.today().year
try:
newdate = datetime.strptime(dateraw + str(cy), "%b %d%Y")
except ValueError:
newdate = convert_mmddyy_to_datetime(dateraw)
return newdate
def convert_mmddyy_to_datetime(date):
try:
newdate = datetime.strptime(date, "%m/%d/%y")
except (TypeError, ValueError):
newdate = None
return newdate
def convert_date_to_string(date):
date_string = None
if date:
date_string = date.strftime("%m/%d/%Y")
return date_string
def reverse_credit_amount(row):
amount = float(row["amount"][1:].replace(",", ""))
return amount if row["isDebit"] else -amount
IGNORE_FLOAT_REGEX = re.compile(r"[$,%]")
def parse_float(str_number):
try:
return float(IGNORE_FLOAT_REGEX.sub("", str_number))
except ValueError:
return None
MINT_ROOT_URL = "https://mint.intuit.com"
MINT_ACCOUNTS_URL = "https://accounts.intuit.com"
MINT_CREDIT_URL = "https://credit.finance.intuit.com"
JSON_HEADER = {"accept": "application/json"}
class MintException(Exception):
pass
class Mint(object):
request_id = 42 # magic number? random number?
driver = None
status_message = None
def __init__(
self,
email=None,
password=None,
mfa_method=None,
mfa_token=None,
mfa_input_callback=None,
intuit_account=None,
headless=False,
session_path=None,
imap_account=None,
imap_password=None,
imap_server=None,
imap_folder="INBOX",
wait_for_sync=True,
wait_for_sync_timeout=5 * 60,
use_chromedriver_on_path=False,
chromedriver_download_path=os.getcwd(),
):
self.driver = None
self.status_message = None
if email and password:
self.login_and_get_token(
email,
password,
mfa_method=mfa_method,
mfa_token=mfa_token,
mfa_input_callback=mfa_input_callback,
intuit_account=intuit_account,
headless=headless,
session_path=session_path,
imap_account=imap_account,
imap_password=imap_password,
imap_server=imap_server,
imap_folder=imap_folder,
wait_for_sync=wait_for_sync,
wait_for_sync_timeout=wait_for_sync_timeout,
use_chromedriver_on_path=use_chromedriver_on_path,
chromedriver_download_path=chromedriver_download_path,
)
@classmethod
def get_rnd(cls): # {{{
return str(int(time.mktime(datetime.now().timetuple()))) + str(
random.randrange(999)
).zfill(3)
def _get_api_key_header(self):
key_var = "window.__shellInternal.appExperience.appApiKey"
api_key = self.driver.execute_script("return " + key_var)
auth = "Intuit_APIKey intuit_apikey=" + api_key
auth += ", intuit_apikey_version=1.0"
header = {"authorization": auth}
header.update(JSON_HEADER)
return header
def close(self):
"""Logs out and quits the current web driver/selenium session."""
if not self.driver:
return
self.driver.quit()
self.driver = None
def request_and_check(
self, url, method="get", expected_content_type=None, **kwargs
):
"""Performs a request, and checks that the status is OK, and that the
content-type matches expectations.
Args:
url: URL to request
method: either 'get' or 'post'
expected_content_type: prefix to match response content-type against
**kwargs: passed to the request method directly.
Raises:
RuntimeError if status_code does not match.
"""
assert method in ["get", "post"]
result = self.driver.request(method, url, **kwargs)
if result.status_code != requests.codes.ok:
raise RuntimeError(
"Error requesting %r, status = %d" % (url, result.status_code)
)
if expected_content_type is not None:
content_type = result.headers.get("content-type", "")
if not re.match(expected_content_type, content_type):
raise RuntimeError(
"Error requesting %r, content type %r does not match %r"
% (url, content_type, expected_content_type)
)
return result
def get(self, url, **kwargs):
return self.driver.request("GET", url, **kwargs)
def post(self, url, **kwargs):
return self.driver.request("POST", url, **kwargs)
def make_post_request(self, url, data, convert_to_text=False):
response = self.post(url=url, data=data, headers=JSON_HEADER)
if convert_to_text:
response = response.text
return response
def build_bundledServiceController_url(self):
return "{}/bundledServiceController.xevent?legacy=false".format(MINT_ROOT_URL)
def login_and_get_token(
self,
email,
password,
mfa_method=None,
mfa_token=None,
mfa_input_callback=None,
intuit_account=None,
headless=False,
session_path=None,
imap_account=None,
imap_password=None,
imap_server=None,
imap_folder=None,
wait_for_sync=True,
wait_for_sync_timeout=5 * 60,
use_chromedriver_on_path=False,
chromedriver_download_path=os.getcwd(),
):
self.driver = _create_web_driver_at_mint_com(
headless, session_path, use_chromedriver_on_path, chromedriver_download_path
)
try:
self.status_message = sign_in(
email,
password,
self.driver,
mfa_method,
mfa_token,
mfa_input_callback,
intuit_account,
wait_for_sync,
wait_for_sync_timeout,
imap_account,
imap_password,
imap_server,
imap_folder,
)
except Exception as e:
logger.exception(e)
self.driver.quit()
def get_request_id_str(self):
req_id = self.request_id
self.request_id += 1
return str(req_id)
def get_attention(self):
attention = None
# noinspection PyBroadException
try:
if "complete" in self.status_message:
attention = self.status_message.split(".")[1].strip()
else:
attention = self.status_message
except Exception:
pass
return attention
def get_bills(self):
return self.get(
"{}/bps/v2/payer/bills".format(MINT_ROOT_URL),
headers=self._get_api_key_header(),
).json()["bills"]
def get_invests_json(self):
warnings.warn(
"We will deprecate get_invests_json method in the next major release due to an updated endpoint for"
"investment data. Transition to use the updated get_investment_data method, which is also now accessible via command-line.",
DeprecationWarning,
)
body = self.get(
"{}/investment.event".format(MINT_ROOT_URL),
).text
p = re.search(
r'<input name="json-import-node" type="hidden" value="json = ([^"]*);"',
body,
)
if p:
return p.group(1).replace(""", '"')
else:
logger.error("FAIL2")
def get_investment_data(self):
investments = self.__call_investments_endpoint()
if "Investment" in investments.keys():
for i in investments["Investment"]:
i["lastUpdatedDate"] = i["metaData"]["lastUpdatedDate"]
i.pop("metaData", None)
else:
raise MintException("Cannot find investment data")
return investments["Investment"]
def get_account_data(self):
accounts = self.__call_accounts_endpoint()
if "Account" in accounts.keys():
for i in accounts["Account"]:
i["createdDate"] = i["metaData"]["createdDate"]
i["lastUpdatedDate"] = i["metaData"]["lastUpdatedDate"]
i.pop("metaData", None)
else:
raise MintException("Cannot find account data")
return accounts["Account"]
def __call_accounts_endpoint(self):
return self.get(
"{}/pfm/v1/accounts".format(MINT_ROOT_URL),
headers=self._get_api_key_header(),
).json()
def get_transaction_data(
self,
include_investment=False,
start_date=None,
end_date=None,
remove_pending=True,
id=0,
):
"""
Note: start_date and end_date must be in format mm/dd/yy.
If pulls take too long, consider a narrower range of start and end
date. See json explanation of include_investment.
Also note: Mint includes pending transactions, however these sometimes
change dates/amounts after the transactions post. They have been
removed by default in this pull, but can be included by changing
remove_pending to False
"""
result = self.__call_transactions_endpoint(
include_investment, start_date, end_date, id
)
if "Transaction" in result.keys():
if remove_pending:
filtered = filter(
lambda transaction: transaction["isPending"] == False,
result["Transaction"],
)
transactions = list(filtered)
else:
transactions = result["Transaction"]
for i in transactions:
i["lastUpdatedDate"] = i["metaData"]["lastUpdatedDate"]
i.pop("metaData", None)
else:
raise MintException("Cannot find transaction data")
return transactions
def __call_transactions_endpoint(
self, include_investment=False, start_date=None, end_date=None, id=0
):
# Specifying accountId=0 causes Mint to return investment
# transactions as well. Otherwise they are skipped by
# default.
if include_investment:
id = 0
if start_date is None:
start_date = self.__x_months_ago(2)
else:
start_date = convert_mmddyy_to_datetime(start_date)
if end_date is None:
end_date = date.today()
else:
end_date = convert_mmddyy_to_datetime(end_date)
return self.get(
"{}/pfm/v1/transactions?id={}&fromDate={}&toDate={}".format(
MINT_ROOT_URL, id, start_date, end_date
),
headers=self._get_api_key_header(),
).json()
def __call_investments_endpoint(self):
return self.get(
"{}/pfm/v1/investments".format(MINT_ROOT_URL),
headers=self._get_api_key_header(),
).json()
def get_categories(self):
return self.get(
"{}/pfm/v1/categories".format(MINT_ROOT_URL),
headers=self._get_api_key_header(),
).json()["Category"]
def __call_accounts_endpoint(self):
return self.get(
"{}/pfm/v1/accounts".format(MINT_ROOT_URL),
headers=self._get_api_key_header(),
).json()
def get_net_worth(self, account_data=None):
if account_data is None:
account_data = self.get_account_data()
# account types in this list will be subtracted
invert = set(["LoanAccount", "CreditAccount"])
return sum(
[
-a["currentBalance"] if a["type"] in invert else a["currentBalance"]
for a in account_data
if a["isActive"]
]
)
def get_budgets(self):
budgets = self.__call_budgets_endpoint()
if "Budget" in budgets.keys():
for i in budgets["Budget"]:
i["lastUpdatedDate"] = i["metaData"]["lastUpdatedDate"]
i.pop("metaData", None)
else:
raise MintException("Cannot find budget data")
return budgets["Budget"]
def __call_budgets_endpoint(self):
return self.get(
"{}/pfm/v1/budgets?startDate={}&endDate={}".format(
MINT_ROOT_URL, self.__x_months_ago(11), self.__first_of_this_month()
),
headers=self._get_api_key_header(),
).json()
def initiate_account_refresh(self):
self.make_post_request(url="{}/refreshFILogins.xevent".format(MINT_ROOT_URL))
def get_credit_score(self):
# Request a single credit report, and extract the score
report = self.get_credit_report(
limit=1,
details=False,
exclude_inquiries=False,
exclude_accounts=False,
exclude_utilization=False,
)
try:
vendor = report["reports"]["vendorReports"][0]
return vendor["creditReportList"][0]["creditScore"]
except (KeyError, IndexError):
raise Exception("No Credit Score Found")
def get_credit_report(
self,
limit=2,
details=True,
exclude_inquiries=False,
exclude_accounts=False,
exclude_utilization=False,
):
# Get the browser API key, build auth header
credit_header = self._get_api_key_header()
# Get credit reports. The UI shows 2 by default, but more are available!
# At least 8, but could be all the TransUnion reports Mint has
# How the "bands" are defined, and other metadata, is available at a
# /v1/creditscoreproviders/3 endpoint (3 = TransUnion)
credit_report = dict()
self._load_mint_credit_url()
credit_report["reports"] = self._get_credit_reports(limit, credit_header)
# If we want details, request the detailed sub-reports
if details:
# Get full list of credit inquiries
if not exclude_inquiries:
credit_report["inquiries"] = self.get_credit_inquiries(credit_header)
# Get full list of credit accounts
if not exclude_accounts:
credit_report["accounts"] = self.get_credit_accounts(credit_header)
# Get credit utilization history (~3 months, by account)
if not exclude_utilization:
credit_report["utilization"] = self.get_credit_utilization(
credit_header
)
return credit_report
def _load_mint_credit_url(self):
# Because cookies are involved and you cannot add cookies for another
# domain, we have to first load up the MINT_CREDIT_URL. Once the new
# domain has loaded, we can proceed with the pull of credit data.
return self.driver.get(MINT_CREDIT_URL)
def _get_credit_reports(self, limit, credit_header):
return self.get(
"{}/v1/creditreports?limit={}".format(MINT_CREDIT_URL, limit),
headers=credit_header,
).json()
def _get_credit_details(self, url, credit_header):
return self.get(url.format(MINT_CREDIT_URL), headers=credit_header).json()
def get_credit_inquiries(self, credit_header):
return self._get_credit_details(
"{}/v1/creditreports/0/inquiries", credit_header
)
def get_credit_accounts(self, credit_header):
return self._get_credit_details(
"{}/v1/creditreports/0/tradelines", credit_header
)
def get_credit_utilization(self, credit_header):
return self._process_utilization(
self._get_credit_details(
"{}/v1/creditreports/creditutilizationhistory", credit_header
)
)
def _process_utilization(self, data):
# Function to clean up the credit utilization history data
utilization = []
utilization.extend(self._flatten_utilization(data["cumulative"]))
for trade in data["tradelines"]:
utilization.extend(self._flatten_utilization(trade))
return utilization
def _flatten_utilization(self, data):
# The utilization history data has a nested format, grouped by year
# and then by month. Let's flatten that into a list of dates.
utilization = []
name = data.get("creditorName", "Total")
for cu in data["creditUtilization"]:
year = cu["year"]
for cu_month in cu["months"]:
date = datetime.strptime(cu_month["name"], "%B").replace(
day=1, year=int(year)
)
utilization.append(
{
"name": name,
"date": date.strftime("%Y-%m-%d"),
"utilization": cu_month["creditUtilization"],
}
)
return utilization
def __first_of_this_month(self):
return date.today().replace(day=1)
def __x_months_ago(self, months=2):
return (self.__first_of_this_month() - relativedelta(months=months)).replace(
day=1
)
def get_accounts(email, password, get_detail=False):
mint = Mint(email, password)
return mint.get_account_data(get_detail=get_detail)
def get_net_worth(email, password):
mint = Mint(email, password)
account_data = mint.get_account_data()
return mint.get_net_worth(account_data)
def get_budgets(email, password):
mint = Mint(email, password)
return mint.get_budgets()
def get_credit_score(email, password):
mint = Mint(email, password)
return mint.get_credit_score()
def get_credit_report(email, password):
mint = Mint(email, password)
return mint.get_credit_report()
def initiate_account_refresh(email, password):
mint = Mint(email, password)
return mint.initiate_account_refresh()
if __name__ == "__main__":
warnings.warn(
"Calling command line code from api.py will be deprecated in a future release.\n"
"Please call mintapi directly. For examples, see the README.md",
DeprecationWarning,
)
from mintapi.cli import main
main()