trying a simple shell script and fixing archives
This commit is contained in:
@@ -116,8 +116,15 @@ async def archive_mail_async(maildir_path, headers, progress, task_id, dry_run=F
|
||||
Returns:
|
||||
None
|
||||
"""
|
||||
archive_dir = os.path.join(maildir_path, ".Archives")
|
||||
archive_files = glob.glob(os.path.join(archive_dir, "**", "*.eml*"), recursive=True)
|
||||
# Check both possible archive folder names locally
|
||||
archive_files = []
|
||||
for archive_folder_name in [".Archives", ".Archive"]:
|
||||
archive_dir = os.path.join(maildir_path, archive_folder_name)
|
||||
if os.path.exists(archive_dir):
|
||||
archive_files.extend(
|
||||
glob.glob(os.path.join(archive_dir, "**", "*.eml*"), recursive=True)
|
||||
)
|
||||
|
||||
progress.update(task_id, total=len(archive_files))
|
||||
|
||||
folder_response = await fetch_with_aiohttp(
|
||||
@@ -128,13 +135,13 @@ async def archive_mail_async(maildir_path, headers, progress, task_id, dry_run=F
|
||||
(
|
||||
folder.get("id")
|
||||
for folder in folders
|
||||
if folder.get("displayName", "").lower() == "archive"
|
||||
if folder.get("displayName", "").lower() in ["archive", "archives"]
|
||||
),
|
||||
None,
|
||||
)
|
||||
|
||||
if not archive_folder_id:
|
||||
raise Exception("No folder named 'Archive' found on the server.")
|
||||
raise Exception("No folder named 'Archive' or 'Archives' found on the server.")
|
||||
|
||||
for filepath in archive_files:
|
||||
message_id = os.path.basename(filepath).split(".")[
|
||||
@@ -147,17 +154,22 @@ async def archive_mail_async(maildir_path, headers, progress, task_id, dry_run=F
|
||||
headers,
|
||||
{"destinationId": archive_folder_id},
|
||||
)
|
||||
if status != 201: # 201 Created indicates success
|
||||
progress.console.print(
|
||||
f"Failed to move message to 'Archive': {message_id}, {status}"
|
||||
)
|
||||
if status == 404:
|
||||
os.remove(filepath) # Remove the file from local archive if not found
|
||||
if status == 201: # 201 Created indicates successful move
|
||||
os.remove(
|
||||
filepath
|
||||
) # Remove the local file since it's now archived on server
|
||||
progress.console.print(f"Moved message to 'Archive': {message_id}")
|
||||
elif status == 404:
|
||||
os.remove(
|
||||
filepath
|
||||
) # Remove the file from local archive if not found on server
|
||||
progress.console.print(
|
||||
f"Message not found on server, removed local copy: {message_id}"
|
||||
)
|
||||
elif status == 204:
|
||||
progress.console.print(f"Moved message to 'Archive': {message_id}")
|
||||
else:
|
||||
progress.console.print(
|
||||
f"Failed to move message to 'Archive': {message_id}, status: {status}"
|
||||
)
|
||||
else:
|
||||
progress.console.print(
|
||||
f"[DRY-RUN] Would move message to 'Archive' folder: {message_id}"
|
||||
@@ -200,6 +212,21 @@ async def delete_mail_async(maildir_path, headers, progress, task_id, dry_run=Fa
|
||||
progress.advance(task_id)
|
||||
|
||||
|
||||
async def get_inbox_count_async(headers):
|
||||
"""
|
||||
Get the number of messages in the inbox.
|
||||
|
||||
Args:
|
||||
headers (dict): Headers including authentication.
|
||||
|
||||
Returns:
|
||||
int: The number of messages in the inbox.
|
||||
"""
|
||||
inbox_url = "https://graph.microsoft.com/v1.0/me/mailFolders/inbox"
|
||||
response = await fetch_with_aiohttp(inbox_url, headers)
|
||||
return response.get("totalItemCount", 0)
|
||||
|
||||
|
||||
async def synchronize_maildir_async(
|
||||
maildir_path, headers, progress, task_id, dry_run=False
|
||||
):
|
||||
@@ -217,10 +244,10 @@ async def synchronize_maildir_async(
|
||||
None
|
||||
"""
|
||||
from src.utils.mail_utils.helpers import (
|
||||
load_last_sync_timestamp,
|
||||
save_sync_timestamp,
|
||||
truncate_id,
|
||||
)
|
||||
load_last_sync_timestamp,
|
||||
save_sync_timestamp,
|
||||
truncate_id,
|
||||
)
|
||||
|
||||
last_sync = load_last_sync_timestamp()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user