Do search one year at a time
This commit is contained in:
parent
343daa4929
commit
8d3ccc573a
1 changed files with 17 additions and 16 deletions
|
@ -64,6 +64,19 @@ class BackupImap:
|
||||||
)
|
)
|
||||||
self._process_mailbox(self.config['mailbox'])
|
self._process_mailbox(self.config['mailbox'])
|
||||||
|
|
||||||
|
def _better_imap_search(self, charset='UTF-8', criterion='UNDELETED'):
|
||||||
|
current = time.gmtime()
|
||||||
|
result = {
|
||||||
|
"result": b"NO",
|
||||||
|
"data": b""
|
||||||
|
}
|
||||||
|
for year in range(current.tm_year-10, current.tm_year+1):
|
||||||
|
search_response, search_data = self.imap.search(charset, criterion, f'BEFORE={year}')
|
||||||
|
if search_response == 'OK':
|
||||||
|
result['result'] = 'OK'
|
||||||
|
result[search_data] += result[search_data]
|
||||||
|
return result
|
||||||
|
|
||||||
def _process_mailbox(self, mailbox):
|
def _process_mailbox(self, mailbox):
|
||||||
self._log.debug("Searching for all mailboxes in mailbox '%s'...", mailbox)
|
self._log.debug("Searching for all mailboxes in mailbox '%s'...", mailbox)
|
||||||
list_response, list_data = self.imap.list(f"\"{mailbox}\"", '*')
|
list_response, list_data = self.imap.list(f"\"{mailbox}\"", '*')
|
||||||
|
@ -99,22 +112,9 @@ class BackupImap:
|
||||||
os.mkdir(os.path.dirname(mailbox_path))
|
os.mkdir(os.path.dirname(mailbox_path))
|
||||||
if not os.path.exists(mailbox_path):
|
if not os.path.exists(mailbox_path):
|
||||||
os.mkdir(mailbox_path)
|
os.mkdir(mailbox_path)
|
||||||
search_succeeded = False
|
search_result = self._better_imap_search('UTF-8', 'UNDELETED')
|
||||||
while not search_succeeded:
|
if search_result['result'] == 'OK':
|
||||||
try:
|
all_msgs_uids = search_result['data'][0].split()
|
||||||
search_response, search_data = self.imap.search('UTF-8', 'UNDELETED')
|
|
||||||
search_succeeded = True
|
|
||||||
except imaplib.IMAP4.error as error:
|
|
||||||
error_str = f"{error.__str__}"
|
|
||||||
if 'got more than' in error_str:
|
|
||||||
imaplib._MAXLINE += 10000
|
|
||||||
self._log.debug(
|
|
||||||
"Error searching mailbox, increasing maxline to %s. %s",
|
|
||||||
imaplib._MAXLINE,
|
|
||||||
error
|
|
||||||
)
|
|
||||||
if search_response == 'OK':
|
|
||||||
all_msgs_uids = search_data[0].split()
|
|
||||||
self._log.debug(
|
self._log.debug(
|
||||||
"Found %s messages",
|
"Found %s messages",
|
||||||
len(all_msgs_uids)
|
len(all_msgs_uids)
|
||||||
|
@ -129,6 +129,7 @@ class BackupImap:
|
||||||
)
|
)
|
||||||
msg_counter += 1
|
msg_counter += 1
|
||||||
fetch_response, fetch_data = self.imap.fetch(message_uid, "(RFC822)")
|
fetch_response, fetch_data = self.imap.fetch(message_uid, "(RFC822)")
|
||||||
|
self._log.debug(fetch_data)
|
||||||
if fetch_response == 'OK':
|
if fetch_response == 'OK':
|
||||||
store_result, unseen_data = self.imap.store(message_uid, '-FLAGS', '\\Seen')
|
store_result, unseen_data = self.imap.store(message_uid, '-FLAGS', '\\Seen')
|
||||||
if store_result != 'OK':
|
if store_result != 'OK':
|
||||||
|
|
Loading…
Reference in a new issue