-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgmail_archiver.py
More file actions
135 lines (109 loc) · 3.97 KB
/
gmail_archiver.py
File metadata and controls
135 lines (109 loc) · 3.97 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
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
#!/usr/bin/env python
from boto3.s3.transfer import S3Transfer
import boto3
import getpass
import os
import subprocess
import configparser
import sys
import threading
# Credentials required for getmail to grab emails from Gmail
print"Please enter the credentials for the mailbox you wish to archive"
username = raw_input("Username: ")
password = getpass.getpass("Password: ")
print"Please enter the S3 bucket you wish to upload the .mbox archive to"
bucket_name = raw_input("S3 Bucket name: ")
def archive_mailbox():
# Variables
base_dir = os.path.expanduser("~/getmail/")
config_path = "{base}getmail_config".format(base=base_dir)
global mbox
mbox = "{0}{1}.mbox".format(base_dir, username)
# Create config file for getmail
config = configparser.ConfigParser()
config['retriever'] = {'type': 'SimpleIMAPSSLRetriever',
'server': 'imap.gmail.com',
'mailboxes': '("Inbox", "[Gmail]/Sent Mail")',
'username': username,
'password': password}
config['destination'] = {'type': 'Mboxrd',
'path': mbox}
config['options'] = {'delete': 'true',
'message_log': '{0}getmail_log'.format(base_dir)}
# Create getmail and it's required directories
try:
if not os.path.exists(base_dir):
print "Creating the base dir for getmail '{0}'".format(base_dir)
os.makedirs(base_dir, 0775)
dirs = ["cur", "new", "tmp"]
for entry in dirs:
print "Creating the '{0}' dir for getmail '{1}'".format(
entry,
base_dir
)
os.makedirs("{0}{1}".format(base_dir, entry), 0775)
open(mbox, 'w').close()
with open(config_path, 'w') as configfile:
config.write(configfile)
# Download emails from Gmail using getmail
print "Downloading the mailbox for {0} to `{1}`".format(
username,
mbox
)
subprocess.call(["getmail", "--all",
"--getmaildir={base}".format(base=base_dir),
"--dont-delete",
"--rcfile={config}".format(config=config_path)
])
print "The gmail archive has been successful for {0} to '{1}'".format(
username,
mbox
)
except Exception as e:
print(e)
finally:
# Remove config file as it contains sensitive information
print "Removing '{0}' as it contains secrets".format(config_path)
os.remove(config_path)
# Required for the progress bar when uploading to S3
class ProgressPercentage(object):
def __init__(self, filename):
self._filename = filename
self._size = float(os.path.getsize(filename))
self._seen_so_far = 0
self._lock = threading.Lock()
def __call__(self, bytes_amount):
# To simplify we'll assume this is hooked up
# to a single filename.
with self._lock:
self._seen_so_far += bytes_amount
percentage = (self._seen_so_far / self._size) * 100
sys.stdout.write(
"\r%s %s / %s (%.2f%%)" % (
self._filename, self._seen_so_far, self._size,
percentage))
sys.stdout.flush()
def s3_upload():
# Create an S3 client
s3 = boto3.client('s3')
transfer = S3Transfer(s3)
key = "{0}/{0}.mbox".format(username)
# Upload files to S3 bucket
try:
transfer.upload_file(
mbox,
bucket_name,
key,
callback=ProgressPercentage(mbox)
)
print "'{0}, has been successfully uploaded to '{1}'".format(
mbox,
bucket_name
)
except Exception as e:
print(e)
def main():
archive_mailbox()
s3_upload()
if __name__ == "__main__":
main()