-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
57 lines (48 loc) · 1.93 KB
/
main.py
File metadata and controls
57 lines (48 loc) · 1.93 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
#!/usr/bin/python
import os
import sys
import csv
import traceback
from constants import GMAIL_LABELS
from helper import GmailHelper
def main():
file = os.path.join(os.getcwd(), sys.argv[1])
with open(file, 'rt') as csvfile:
rows = [row for row in csv.reader(csvfile)]
column_names = [name.strip() for name in rows[0]]
email_index = column_names.index('email')
output_file = 'message-count-users-output.csv'
with open(output_file, 'wb') as output_file:
writer = csv.writer(output_file, delimiter=',', quotechar='|',
quoting=csv.QUOTE_NONE)
headers = ['email',
'source_message_count',
]
writer.writerow(headers)
for row in rows[1:]:
email = ''
count = 0
count_pages = 0
try:
email = row[email_index].strip()
# Connect to the source to the all message folder
gmail_helper = GmailHelper(user_email=email)
page_token = None
while True:
count_pages += 1
messages, page_token = gmail_helper.list_messages(
q="", page_token=page_token)
count += len(messages)
if not page_token:
break
print count_pages
new_row = []
new_row.append(str(email))
new_row.append(str(count))
writer.writerow(new_row)
except Exception as e:
print 'Error while connecting to get message count for ' \
'account[%s]: %s -> %s' % (email, e,
traceback.format_exc())
if __name__ == "__main__":
main()