split by year received

This commit is contained in:
Antonio J. Delgado 2025-03-28 10:20:14 +02:00
parent 8d3ccc573a
commit 79df8cbaa3

View file

@ -64,19 +64,6 @@ class BackupImap:
)
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):
self._log.debug("Searching for all mailboxes in mailbox '%s'...", mailbox)
list_response, list_data = self.imap.list(f"\"{mailbox}\"", '*')
@ -112,9 +99,11 @@ class BackupImap:
os.mkdir(os.path.dirname(mailbox_path))
if not os.path.exists(mailbox_path):
os.mkdir(mailbox_path)
search_result = self._better_imap_search('UTF-8', 'UNDELETED')
if search_result['result'] == 'OK':
all_msgs_uids = search_result['data'][0].split()
current = time.gmtime()
for year in range(current.tm_year-10, current.tm_year+1):
search_response, search_data = self.imap.search('UTF-8', 'UNDELETED', f'BEFORE={year}')
if search_response == 'OK':
all_msgs_uids = search_data[0].split()
self._log.debug(
"Found %s messages",
len(all_msgs_uids)