PK-u>Ky4|GmailAPIHelper.py""" Module to help navigate the Gmail API responses """ __version__ = '0.0.0' import os import re from datetime import datetime from base64 import urlsafe_b64decode import httplib2 from pytz import utc from apiclient import discovery from oauth2client import client, tools, file class GmailHelper: """ Helper class """ def __init__(self, app_name): """ Creates/loads credentials for the Gmail API connection Args: : app_name: Application name as defined during pre-setup: https://developers.google.com/gmail/api/quickstart/python """ self.app_name = app_name self.creds = self.setup() self.service = discovery.build('gmail', 'v1', http=self.creds.authorize(httplib2.Http())) def setup(self): """ Sets up credentials to access the Gmail API if they don't exist Returns: : credentials """ home_dir = os.path.expanduser('~') secret_file = os.path.join(home_dir, "client_secret.json") scopes_url = "https://mail.google.com/" credential_path = os.path.join(home_dir, "gmail-credentials.json") store = file.Storage(credential_path) credentials = store.get() if not credentials or credentials.invalid: flow = client.flow_from_clientsecrets(secret_file, scopes_url) flow.user_agent = self.app_name credentials = tools.run_flow(flow, store) return credentials def _message_contents(self, msg_id): """ Retrieves the contents of a message, by ID Args: : msg_id: Gmail message ID Returns: : Contents of the message """ messages = self.service.users().messages() message_contents = messages.get(userId="me", id=msg_id).execute() return message_contents def _walk(self, payload): """ Warning: This is a recursive function. It calls on itself until it can get a result, may hang if can't complete Walks the contents of a Gmail message payload to find the 'text/plain' or 'text/html' MimeType message Args: : payload: GmailMessage['payload'] Returns: : Base64-encoded plain text or html message """ for part in payload['parts']: if 'text/' in part['mimeType']: data = part['body']['data'] return data return self._walk(part) def _retrieve(self, message): """ Retrieves the pieces of content from a Gmail message Exceptions: : AttributeError if a piece of content couldn't be retrieved Returns: : Dictionary with ID, Datetime, From and Message """ content = {} content['id'] = message['id'] _date = int(message['internalDate']) _date = utc.localize(datetime.fromtimestamp(_date/1000)) content['date'] = utc.normalize(_date) payload = message['payload'] for header in payload['headers']: if header['name'].lower() == 'from': _from = header['value'] email_encoded = re.search('<(.*?)>', _from) if email_encoded: _from = email_encoded.group(1) content['from'] = _from if 'parts' in payload: data = self._walk(payload) else: data = payload['body']['data'] data = urlsafe_b64decode(data.encode("ASCII")) content['message'] = data.decode('utf-8') expected_content = set(['id', 'date', 'from', 'message']) found_content = set([c for c in content]) missing_content = expected_content.difference(found_content) if missing_content: raise AttributeError(f'{missing_content} was not retrieved') return content def get_emails(self, search, max_results=None): """ Retrieve latest e-mails (up to 100) found by a search Args: : search: Search to send to Gmail. Accepts same special strings such as 'from:*****' or 'in:sent' : max_results: Max number of results (up to 100). Recommended Returns: : List of e-mail dictionaries with: 'id' : message id 'date' : UTC datetime of the e-mail 'from' : Sender e-mail 'message' : Plain text or HTML message """ search = self.service.users().messages().list(userId="me", q=search, maxResults=max_results) results = search.execute() msg_ids = [msg['id'] for msg in results['messages']] messages = list(map(self._message_contents, msg_ids)) emails = list(map(self._retrieve, messages)) return emails PKys>K=]j,,)GmailAPIHelper-0.0.0.dist-info/LICENSE.md The MIT License (MIT) Copyright (c) 2017 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!H١Wd$GmailAPIHelper-0.0.0.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,Q0343 /, (-JLR()*M ILR(4KM̫#DPK!H'GmailAPIHelper-0.0.0.dist-info/METADATA]n0EY ɲ 1?P'z*M*I9p>C)s7Ä/ UV )au_ߒD]*1N 65Ar0cYx@蝍[gK!%bsU̢0nYחp-v-BS'\4c\Phj|[R˝k13hoHyC6!a#(+(alpƯm"Q1ٖ2~6f'2;>HY?g`b6S>U duX,fB.* kvQ@:ѹک{=~qg;( =7L-4+تgZdCott;mYA/4PK-u>Ky4|GmailAPIHelper.pyPKys>K=]j,,)GmailAPIHelper-0.0.0.dist-info/LICENSE.mdPK!H١Wd$AGmailAPIHelper-0.0.0.dist-info/WHEELPK!H'GmailAPIHelper-0.0.0.dist-info/METADATAPK!HH%GmailAPIHelper-0.0.0.dist-info/RECORDPK