-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_emails.py
63 lines (52 loc) · 2.32 KB
/
fetch_emails.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
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
import json
import email
import monzo
import base64
import email_scraper
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
def main(creds, monzo_creds):
"""Shows basic usage of the Gmail API.
Lists the user's Gmail labels.
"""
service = build('gmail', 'v1', credentials=creds)
subject_keywords = ['order', 'receipt', 'booking', 'confirmation', 'invoice', 'payment', 'successful', 'debit']
query = 'subject:' + ' OR subject:'.join(subject_keywords)
print(query)
response = service.users().messages().list(userId='me', q=query).execute()
messages = []
if 'messages' in response:
messages.extend(response['messages'])
while 'nextPageToken' in response:
page_token = response['nextPageToken']
response = service.users().messages().list(userId='me',
pageToken=page_token, q=query).execute()
messages.extend(response['messages'])
print("Gmail emails downloaded: " + str(len(messages)))
transactions = monzo.get_transactions( monzo_creds['first_account_id'], monzo_creds['access_token']);
print("Monzo transactions downloaded: " + str(len(transactions)))
print("Munching receipts")
count = 0;
for message in messages:
message = service.users().messages().get(userId='me', id=message['id'], format='raw').execute()
msg_str = base64.urlsafe_b64decode(message['raw'])
mime_msg = email.message_from_bytes(msg_str)
#print("Munched: "+mime_msg['subject'])
try:
money, time, subject, email_link = email_scraper.scrape(message['id'], mime_msg)
#print("Found money: " + str(money))
if money != 1:
if 1 == monzo.match_and_upload_receipt(money, time, subject, email_link, transactions, monzo_creds['access_token']) :
count = count + 1
print("Receipt munched successfully"+str(mime_msg['subject']))
except:
continue
print("Receipts munched: "+ str(count))
if __name__ == '__main__':
main()