-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattachment_archiever.py
151 lines (121 loc) · 5.3 KB
/
attachment_archiever.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
#! /usr/bin/python
import datetime as dt
import email
import imaplib
import json
import logging
import os
from tqdm import tqdm
class EmailAttachmentDownloader(object):
def __init__(self, server: str, email_address: str, password: str) -> None:
super().__init__()
self.server = server
self.email_address = email_address
self.password = password
self.m = None
self.logged_in = False
def login(self) -> None:
self.m = imaplib.IMAP4_SSL(self.server)
status, login_msg = self.m.login(self.email_address, self.password)
if status.lower() == 'ok':
logging.info(f'{self.email_address} login Successfully!')
self.logged_in = True
self.m.select()
else:
logging.error(f'Login ERROR! Error message: {login_msg}')
def logout(self) -> None:
self.m.close()
self.m.logout()
self.logged_in = False
logging.info(f'{self.email_address} logged out!')
def delete_mail(self) -> None:
self.m.expunge()
def __enter__(self):
self.login()
return self
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
if self.logged_in:
self.logout()
@staticmethod
def parse_sender(sender: str) -> str:
if ' ' in sender:
sender = sender.split(' ')[-1]
sender = sender.replace('<', '')
sender = sender.replace('>', '')
if '?' in sender:
sender = email.header.decode_header(sender)[-1][0].decode()
return sender
@staticmethod
def parse_datetime(email_date: str) -> dt.datetime:
raw_date_str = email_date[:31]
if raw_date_str[-1] == ' ':
raw_date_str = raw_date_str[:-1]
if raw_date_str[:3].isalpha():
raw_date_str = raw_date_str[5:]
time = dt.datetime.strptime(raw_date_str, '%d %b %Y %H:%M:%S %z')
return time
@staticmethod
def parse_file_name(file_name: str) -> str:
if '?' in file_name:
content, charset = email.header.decode_header(file_name)[0]
file_name = content.decode(charset)
return file_name
def download_all_attachments(self, output_dir: str) -> None:
if not self.logged_in:
logging.error('You MUST login first!')
return
resp, items = self.m.search(None, "(ALL)")
emails = items[0].split()
logging.info(f'{len(emails)} emails found in the inbox')
with tqdm(emails) as progress_bar:
for email_id in emails:
resp, data = self.m.fetch(email_id, "(RFC822)")
email_body = data[0][1]
# determine encoding the hard way
charset_beg = email_body.find(b'charset=')
if charset_beg > 0:
charset_end = email_body[charset_beg:].find(b';')
if charset_end < 0:
charset_end = 9999999999
charset_end2 = email_body[charset_beg:].find(b'\r')
charset_end3 = email_body[charset_beg:].find(b'\n')
charset = email_body[charset_beg+8:charset_beg + min(charset_end, charset_end2, charset_end3)]
charset = charset.decode().replace('"', '')
else:
charset = 'gb2312'
mail = email.message_from_string(email_body.decode(charset))
sent_datetime = self.parse_datetime(mail['Date'])
if mail['FROM']:
sender = self.parse_sender(mail['FROM'])
else:
continue
logging.debug(f'Dealing with email sent by {sender} on {sent_datetime}')
folder_name = os.path.join(output_dir, sender)
for part in mail.walk():
if part.get_content_maintype() == 'multipart':
continue
file_name = part.get_filename()
if file_name is None:
continue
file_name = self.parse_file_name(file_name)
os.makedirs(folder_name, exist_ok=True)
full_path = os.path.join(folder_name, file_name)
if os.path.isfile(full_path):
time_str = sent_datetime.strftime('%Y%m%d%H%M%S')
file_name = '_'.join([time_str, file_name])
full_path = os.path.join(folder_name, file_name)
logging.debug(f'Downloading attachment {file_name}')
progress_bar.set_description(f'{file_name}')
with open(full_path, 'wb') as f:
f.write(part.get_payload(decode=True))
self.m.store(email_id, '+FLAGS', r'\Seen')
self.m.store(email_id, '+FLAGS', r'\Deleted')
progress_bar.update()
if __name__ == '__main__':
logging.getLogger().setLevel(logging.INFO)
with open('config.json', 'r', encoding='utf-8') as f:
config = json.load(f)
# attachment_downloader = EmailAttachmentDownloader(**config['login_info'])
# attachment_downloader.login()
with EmailAttachmentDownloader(**config['login_info']) as attachment_downloader:
attachment_downloader.download_all_attachments(config['storage_root'])