-
Notifications
You must be signed in to change notification settings - Fork 0
/
DownloadAttachments_OATH_performance.py
195 lines (166 loc) · 7.67 KB
/
DownloadAttachments_OATH_performance.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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from __future__ import print_function
from oauth2client.service_account import ServiceAccountCredentials
from xml.dom.minidom import parseString
import xmlrpclib
import os
import base64
import ConfigParser
import oauth2client
import httplib2
import pprint
import json
import re
import zipfile
from zipfile import ZipFile
from apiclient import discovery
from apiclient import errors
from oauth2client import client
from oauth2client import tools
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/gmail-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Gmail API Python Quickstart'
EMAIL_MESSAGE_DUMP_FILE = "emails.txt"
def main():
config = ConfigParser.RawConfigParser()
config.read('config.properties')
attachment_directory = config.get('general', 'attachment_directory')
suffixes = config.get('general', 'emails_suffixes_to_ignore').split(",")
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('gmail', 'v1', http=http)
messages = ListMessagesWithLabel(service, 'me', 'INBOX')
label = config.get('general', 'label')
if not messages:
print('No messages found.')
else:
for message in messages:
current_files = os.listdir(attachment_directory)
message_details = service.users().messages().get(userId='me', id=message['id']).execute()
messageString = json.dumps(message_details).replace('\\','')
#print('message string', messageString)
pp = pprint.PrettyPrinter(indent=4)
#pp.pprint(message_details)
attachmentIdMatch = re.match(r"(.*)attachmentId\": \"(.+?)\"(.*)", messageString)
fromMatch = re.match(r"(.*)From\", \"value\": \"(.+?)\"(.*)", messageString)
subjectMatch = re.match(r"(.*)Subject\", \"value\": \"(.+?)\"(.*)", messageString)
returnPathMatch = re.match(r"(.*)Return-Path\", \"value\": \"(.+?)\"(.*)", messageString)
#if label not in messageString:
# print('not correct label - ignoring')
# continue
# if not returnPathMatch:
# print('WARNING no return path - ignoring')
# pp.pprint(message_details)
# continue
if not subjectMatch:
print('WARNING no subject - ignoring')
pp.pprint(message_details)
continue
fromFound = fromMatch.group(2)
subjectFound = subjectMatch.group(2)
if returnPathMatch:
returnPathFound = returnPathMatch.group(2).replace('<', '').replace('>', '')
if '@' in returnPathFound:
emailSuffixToCheck = re.match(r"(.*)@(.*)", returnPathFound).group(2)
if emailSuffixToCheck in suffixes:
print('skipping message as email suffix is to be ignored', emailSuffixToCheck)
continue
else:
returnPathFound="NONE"
else:
returnPathFound = "NONE"
# print('skipping message as no valid return path', messageSummary)
# continue
messageSummary = "FROM " + fromFound + " RETURN PATH " + returnPathFound + " SUBJECT " +subjectFound
fileNameMatch = re.match(r"(.*)filename=\"(.+?)\"(.*)", messageString)
if not fileNameMatch or not attachmentIdMatch:
print('skipping message as no attachment present', messageSummary)
pp.pprint(message_details)
continue
fileNameFound = fileNameMatch.group(2)
attachmentIdFound = attachmentIdMatch.group(2)
if fileNameFound in current_files:
print('skipping ', fileNameFound, " as already stored")
continue
att = service.users().messages().attachments().get(userId='me', messageId=message['id'],id=attachmentIdFound).execute()
data = att['data']
file_data = base64.urlsafe_b64decode(data.encode('UTF-8'))
path = os.path.join(attachment_directory, fileNameFound)
# PUT BACK
if not path.lower().endswith(".jpg") and not path.lower().endswith(".png") \
and not path.lower().endswith(".gif") and not path.lower().endswith(".ics"):
# if path.endswith(".zip"):
with open(path, 'w') as f:
f.write(file_data)
print('saving file:', f)
if path.endswith(".zip"):
try:
fh = open(path, 'rb')
z = zipfile.ZipFile(fh)
#zippedFile = ZipFile(path)
z.extractall(attachment_directory)
os.remove(path)
fh.close()
except zipfile.BadZipfile, error:
print('unzip error occurred: ', error)
continue
def get_credentials():
# type: () -> object
"""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,
'Google Drive/G/IT/Development/ETL/.credentials') # JLD - customized path to working directory
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir, 'client_secret_anchorpath.json')
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run_flow(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def ListMessagesWithLabel(service, user_id, label_name):
"""List all Messages of the user's mailbox with label_ids applied.
Args:
service: Authorized Gmail API service instance.
user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
label_ids: Only return Messages with these labelIds applied.
Returns:
List of Messages that have all required Labels applied. Note that the
returned list contains Message IDs, you must use get with the
appropriate id to get the details of a Message.
"""
try:
config = ConfigParser.RawConfigParser()
config.read('config.properties')
query = config.get('general', 'query')
response = service.users().messages().list(userId=user_id, 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=user_id, pageToken=page_token, q=query).execute()
messages.extend(response['messages'])
return messages
except errors.HttpError, error:
print('An error occurred: ', error)
if __name__ == '__main__':
main()