Skip to content

Instantly share code, notes, and snippets.

@markuskont
Last active October 18, 2019 06:18
Show Gist options
  • Save markuskont/f6e210a1b9b25441d4cbdf838915fa8f to your computer and use it in GitHub Desktop.
Save markuskont/f6e210a1b9b25441d4cbdf838915fa8f to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#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 base64
import dateutil.parser as parser
from bs4 import BeautifulSoup
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
def main():
"""Shows basic usage of the Gmail API.
Lists the user's Gmail labels.
"""
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 available, 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)
service = build('gmail', 'v1', credentials=creds)
# Call the Gmail API
results = service.users().labels().list(userId='me').execute()
labels = results.get('labels', [])
mails = service.users().messages().list(userId='me').execute()
for m in mails["messages"]:
message = service.users().messages().get(userId="me", id=m["id"]).execute() # fetch the message using API
payld = message['payload'] # get payload of the message
headr = payld['headers'] # get header of the payload
temp_dict = {}
for one in headr: # getting the Subject
if one['name'] == 'Subject':
msg_subject = one['value']
temp_dict['Subject'] = msg_subject
else:
pass
for two in headr: # getting the date
if two['name'] == 'Date':
msg_date = two['value']
date_parse = (parser.parse(msg_date))
m_date = (date_parse.date())
temp_dict['Date'] = str(m_date)
else:
pass
for three in headr: # getting the Sender
if three['name'] == 'From':
msg_from = three['value']
temp_dict['Sender'] = msg_from
else:
pass
# Fetching message body
if "data" in payld["body"]:
data = payld['body']['data'] # fetching the message parts
data = data.replace("-","+")
data = data.replace("_","/")
clean = base64.b64decode(data) # decoding from Base64 to UTF-8
soup = BeautifulSoup(clean, "lxml" )
#mssg_body = soup.body()
# mssg_body is a readible form of message body
# depending on the end user's requirements, it can be further cleaned
# using regex, beautiful soup, or any other method
#temp_dict['Message_body'] = mssg_body
print(soup.body())
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment