-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail_service.py
More file actions
74 lines (67 loc) · 3.06 KB
/
Copy pathemail_service.py
File metadata and controls
74 lines (67 loc) · 3.06 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
import os
import pickle
# Gmail API utils
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# for encoding/decoding messages in base64
from mimetypes import guess_type as guess_mime_type
from bs4 import BeautifulSoup
import base64
# Request all access (permission to read/send/receive emails, manage the inbox, and more)
SCOPES = ['https://mail.google.com/']
def gmail_authenticate():
creds = None
# the file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first time
if os.path.exists("token.pickle"):
with open("token.pickle", "rb") as token:
creds = pickle.load(token)
# if there are no (valid) credentials availablle, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file('credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# save the credentials for the next run
with open("token.pickle", "wb") as token:
pickle.dump(creds, token)
return build('gmail', 'v1', credentials=creds)
# get the Gmail API service "from:yashmeh98@gmail.com"
def get_gmail_service():
service = gmail_authenticate()
return service
def get_latest_email(service, q):
results = service.users().messages().list(userId='me', labelIds=['INBOX'], q=q).execute()
messages = results.get('messages',[])
for msg in messages:
# Get the message from its id
txt = service.users().messages().get(userId='me', id=msg['id']).execute()
# Mark the message as read
service.users().messages().modify(userId='me', id=msg['id'], body={'removeLabelIds': ['UNREAD']}).execute()
# Use try-except to avoid any Errors
try:
# Get value of 'payload' from dictionary 'txt'
payload = txt['payload']
headers = payload['headers']
# Look for Subject and Sender Email in the headers
for d in headers:
if d['name'] == 'Subject':
subject = d['value']
if d['name'] == 'From':
sender = d['value']
# The Body of the message is in Encrypted format. So, we have to decode it.
# Get the data and decode it with base 64 decoder.
parts = payload.get('parts')[0]
data = parts['body']['data']
data = data.replace("-","+").replace("_","/")
decoded_data = base64.b64decode(data)
# Now, the data obtained is in lxml. So, we will parse
# it with BeautifulSoup library
soup = BeautifulSoup(decoded_data , "lxml")
body = soup.body()
except Exception as e:
print("Error retrieving email ", e)
#return the last body to ensure that only latest email body is sent back
return body