-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmint_api.py
317 lines (245 loc) · 9.88 KB
/
mint_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
"""
Coppied from Kartik Talwar's mint screenscarping API
commit 28802d5d41
https://github.com/KartikTalwar/mint
"""
import os
import sys
import json
import time
import requests
import HTMLParser
class Mint:
def __init__(self, email, password):
self.email = email
self.password = password
self.token = None
self.session = requests.Session()
self.accounts = []
self.login()
def login(self):
payload = {
"username" : self.email,
"password" : self.password,
"task" : "L",
"nextPage" : "overview.event"
}
response = self.session.post("https://wwws.mint.com/loginUserSubmit.xevent", data=payload).text
js_token = response.split('javascript-token')[1].split('>')[0]
js_token = js_token.split('value="')[1].split('"')[0]
self.token = js_token
def get_accounts(self):
if not self.token:
return 'Not logged in'
payload = json.dumps(
[
{
"args":
{
"types":
[
"BANK",
"CREDIT",
"INVESTMENT",
"LOAN",
"MORTGAGE",
"OTHER_PROPERTY",
"REAL_ESTATE",
"VEHICLE",
"UNCLASSIFIED"
]
},
"id": "115485",
"service": "MintAccountService",
"task": "getAccountsSorted"
}
]
)
post_url = "https://wwws.mint.com/bundledServiceController.xevent?token="+self.token
response = self.session.post(post_url, data={"input": payload})
response = json.loads(response.text)["response"]
accounts = response["115485"]["response"]
self.accounts = accounts
return_keys = ['id', 'accountName', 'currency', 'currentBalance', 'isActive', 'lastUpdated']
return [dict(((k,account[k]) for k in return_keys) ) for account in self.accounts]
def get_account_details(self, account_id):
for account in self.accounts:
if account['id'] == account_id:
return account
return {}
def update_accounts(self):
post_url = 'https://wwws.mint.com/refreshFILogins.xevent'
payload = {"token" : self.token}
send_req = self.session.post(post_url, data=payload)
status = False
counter = time.time()
while status is False:
check_url = 'https://wwws.mint.com/userStatus.xevent?rnd=%s' % int(time.time())
check_req = self.session.get(check_url).json()['isRefreshing']
time.sleep(0.5)
if not check_req:
status = True
if time.time() - counter > 60:
break
return status
def get_transactions(self, **kwargs):
payload = {
'queryNew' : '',
'offset' : 0,
'filterType' : 'cash',
'comparableType' : 8,
'acctChanged' : 'T',
'task' :'transactions,txnfilters',
'rnd' : int(time.time())
}
if 'account_id' in kwargs:
payload['accountId'] = kwargs['account_id']
if 'reimbursable' in kwargs:
if kwargs['reimbursable']:
payload['query'] = 'tag:"Reimbursable"'
if 'tax_related' in kwargs:
if kwargs['tax_related']:
payload['query'] = 'tag:"Tax Related"'
if 'vacation' in kwargs:
if kwargs['vacation']:
payload['query'] = 'tag:"Vacation"'
if 'investment' in kwargs:
if kwargs['investment']:
payload.update({'filterType' : 'investment'})
if 'loan' in kwargs:
if kwargs['loan']:
payload.update({'filterType' : 'loan'})
request = self.session.get('https://wwws.mint.com/app/getJsonData.xevent', params=payload).json()
return request['set'][0]['data']
def search_transactions(self, query, **kwargs):
payload = {
'queryNew' : '',
'query' : query,
'offset' : 0,
'filterType' : 'cash',
'comparableType' : 8,
'acctChanged' : 'T',
'task' :'transactions,txnfilters',
'rnd' : int(time.time())
}
if 'start_date' in kwargs:
payload['startDate'] = kwargs['start_date']
if 'end_date' in kwargs:
payload['endDate'] = kwargs['end_date']
if 'account_id' in kwargs:
payload['accountId'] = kwargs['account_id']
if 'reimbursable' in kwargs:
if kwargs['reimbursable']:
payload['query'] += ', tag:"Reimbursable"'
if 'tax_related' in kwargs:
if kwargs['tax_related']:
payload['query'] += ', tag:"Tax Related"'
if 'vacation' in kwargs:
if kwargs['vacation']:
payload['query'] += ', tag:"Vacation"'
if 'investment' in kwargs:
if kwargs['investment']:
payload.update({'filterType' : 'investment'})
if 'loan' in kwargs:
if kwargs['loan']:
payload.update({'filterType' : 'loan'})
request = self.session.get('https://wwws.mint.com/app/getJsonData.xevent', params=payload).json()
return request['set'][0]['data']
def get_categories(self):
get_url = 'https://wwws.mint.com/app/getJsonData.xevent?task=categories&rnd=%s' % int(time.time())
request = self.session.get(get_url).json()
return request['set'][0]['data']
def get_goals(self):
get_url = 'https://wwws.mint.com/app/getJsonData.xevent?task=goals&rnd=%s' % int(time.time())
request = self.session.get(get_url).json()
return request['set'][0]['data']['current']
def get_budget(self, start_date=None, end_date=None):
get_url = 'https://wwws.mint.com/getBudget.xevent?startDate=%s&endDate=%s&rnd=%s' % (start_date, end_date, int(time.time()))
request = self.session.get(get_url).json()
data = {}
for month,values in request['data']['spending'].iteritems():
data[month] = {}
data[month]['budgeted'] = []
data[month]['unbudgeted'] = []
data[month]['summary'] = {
'total_spending' : values['tot']['amt'],
'budgeted' : values['tot']['bu'],
'unbudgeted' : values['tot']['ub']
}
for i in values['ub']:
if 'pid' in i and i['cat'] != 0:
data[month]['unbudgeted'].append({
'amount' : i['amt'],
'category_id' : i['cat'],
'category_name' : self.get_category_from_id(i['cat'])
})
for j in values['bu']:
data[month]['budgeted'].append({
'is_transfer' : j['isTransfer'],
'category_id' : j['cat'],
'category_name' : self.get_category_from_id(j['cat']),
'remaining_balance' : j['rbal'],
'remaining_amount' : j['ramt'],
'is_income' : j['isIncome'],
'budget_amount' : j['bgt'],
'budget_id' : j['id'],
'total_spending' : j['amt']
})
return data
def get_category_from_id(self, cid):
if cid == 0:
return 'Uncategorized'
for i in self.get_categories():
if i['id'] == cid:
return i['value']
if 'children' in i:
for j in i['children']:
if j['id'] == cid:
return j['value']
return 'Unknown'
def get_properties(self):
check = self.session.get('https://wwws.mint.com/htmlFragment.xevent?task=as-nav-content-pr&rnd=%s' % int(time.time())).json()
if "<div class='hide' id='prlogins'>" in check['xmlContent']:
check = check['xmlContent'].split("<div class='hide' id='prlogins'>")[1].split('</div>')[0]
html = HTMLParser.HTMLParser()
return json.loads(html.unescape(check))
return []
def add_new_property(self, name):
payload = {
'types' : 'pr',
'accountName' : name,
'accountValue' : 0.00,
'associatedLoanRadio' : 'F',
'isAdd' : 'T',
'accountType' : 'a',
'token' : self.token
}
others = self.get_properties()
for i in others:
if i['name'] == name:
return i
request = self.session.post('https://wwws.mint.com/updateAccount.xevent', data=payload)
return request.json()['response']
def update_property(self, account_id, value):
payload = {
"accountId": account_id,
"types": "ot",
"accountValue": value,
"associatedLoanRadio": "No",
"accountType": "3",
"accountStatus": "1",
"token": self.token
}
response = self.session.post("https://wwws.mint.com/updateAccount.xevent", data=payload)
return response.json()['response']
def update_bitcoins(self, coinbase_apikey):
balance = self.session.get('https://coinbase.com/api/v1/account/balance?api_key=%s' % coinbase_apikey).json()['amount']
bitvalue = self.session.get('https://coinbase.com/api/v1/prices/sell?qty=%s' % balance).json()['amount']
bitcoinid = self.add_new_property('Bitcoins')['accountId']
return self.update_property(bitcoinid, bitvalue)
def logout(self):
if self.token:
self.session.get('https://wwws.mint.com/logout.event?task=explicit')
return True
return False
mint = Mint('[email protected]','wjjk1120')