-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickStart.py
More file actions
136 lines (111 loc) · 4.24 KB
/
quickStart.py
File metadata and controls
136 lines (111 loc) · 4.24 KB
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
#!/usr/bin/python
from __future__ import print_function
import httplib2
import os
import re
import pickle
from googleapiclient import discovery
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from pprint import pprint
import base64
from transaction import *
import calculations
import argparse
SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'ExpenseManager'
def get_credentials():
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credentials_path_pickle = os.path.join(credential_dir,
'gmail-python-quickstart.pickle')
credentials = None
if os.path.exists(credentials_path_pickle):
with open(credentials_path_pickle, "rb") as token:
credentials = pickle.load(token)
if not credentials or not credentials.valid:
if credentials and credentials.expired and credentials.refresh_token:
credentials.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
credentials = flow.run_local_server()
with open(credentials_path_pickle, 'wb') as token:
pickle.dump(credentials, token)
return credentials
def get_messageIds(service, Bank):
""" Gets the text of all the messages matching the particular query"""
request = service.users().messages().list(userId='me', q=Bank.query.value)
response = request.execute()
ids = []
print(response)
#Return is no messages found for query
if "messages" not in response:
return ids
for obj in response['messages']:
#print(obj['id'])
print(obj)
ids.append(obj['id'])
while 'nextPageToken' in response:
request = service.users().messages().list_next(request, response)
response = request.execute()
for obj in response['messages']:
#print(obj['id'])
print(obj)
ids.append(obj['id'])
print("There are "+str(len(ids))+" messages for "+Bank.name)
return ids
def get_TransactionsFromIds(service, ids, Bank):
transactions = []
count = 500
for mId in ids:
count = count - 1
if count == 0:
break
msg_req = service.users().messages().get(userId='me', id=mId)
msg_res = msg_req.execute()
#print(msg_res['payload'])
file_data = base64.urlsafe_b64decode(msg_res['payload']['body']['data']).decode()
parsed = parseHTMLforTransactionDetails(file_data, Bank.regexPattern.value)
if parsed == None:
continue
trans = Transaction(parsed, Bank)
print(count, trans)
transactions.append(trans)
return transactions
def parseHTMLforTransactionDetails(htmlBody, pattern):
""" Returns a regex matcher object for the matched pattern"""
pattern = re.compile(pattern)
#print(htmlBody)
result = re.search(pattern, htmlBody)
if not result:
print("No Pattern Match Found")
#print(htmlBody)
return None
#print(result.groups())
#print(result.group("merchant").strip()[:-1])
return result
def main():
credentials = get_credentials()
service = discovery.build('gmail', 'v1', credentials=credentials)
transactions = []
for BankEnum in BankDetails:
print("Serching for "+BankEnum.name)
message_ids = get_messageIds(service, BankEnum.value)
transactions+=get_TransactionsFromIds(service, message_ids, BankEnum.value)
print(len(transactions))
bucket = calculations.getTotalSpendsByMonth(transactions)
print(bucket)
print("Total Spends: "+str(calculations.getTotalSpends(transactions)))
#message_list = get_messageText(service, 'airtel')
#pprint(message_list)
if __name__ == '__main__':
main()