diff --git a/main.py b/main.py index 3a603f6..d598d59 100644 --- a/main.py +++ b/main.py @@ -6,6 +6,7 @@ import os from datetime import datetime import logging +import time import requests import boto3 import nextcord @@ -19,6 +20,7 @@ S3_CLIENT = None SIGNING_KEY = None + class ConfigurationError(Exception): def __init__(self, message): super().__init__(message) @@ -102,13 +104,18 @@ def extract_message(message): # process any attachments/files for attach in message.attachments: # Download file - resp = requests.get(attach.url, timeout=30) - resp.raise_for_status() + content = bytearray() + try: + resp = requests.get(attach.url, timeout=30) + resp.raise_for_status() + content = resp.content + except requests.exceptions.HTTPError: + print('Unable to download attachment') attachments.append({ 'type': attach.content_type, 'origin_name': attach.filename, - 'content': base64.b64encode(resp.content).decode() + 'content': base64.b64encode(content).decode() }) backup_msg = { @@ -559,27 +566,35 @@ async def on_ready(): generate_directory_file(client.get_all_channels(), datetime.now()) # Backup channels + max_attempts = 3 for channel in target_channels: - # Only interested in text channels - if not isinstance(channel, nextcord.TextChannel): - continue - - print(f'Backing up Channel {channel.name} on {channel.guild.name}') - - # Backup channels - last_msg_id = await get_last_message_id(channel) - new_last_msg_id = await backup_channel(channel, last_msg_id) - if new_last_msg_id is not None: - await set_last_message_id(channel, new_last_msg_id) - - # Backup threads in channel - for thread in channel.threads: - print(f'Backing up Thread {thread.id} in Channel {channel.name} on {channel.guild.name}') - - last_msg_id = await get_last_message_id(thread) - new_last_msg_id = await backup_channel(thread, last_msg_id) - if new_last_msg_id is not None: - await set_last_message_id(thread, new_last_msg_id) + for attempt in range(1, max_attempts + 1): + try: + # Only interested in text channels + if not isinstance(channel, nextcord.TextChannel): + continue + + print(f'Backing up Channel {channel.name} on {channel.guild.name}') + + # Backup channels + last_msg_id = await get_last_message_id(channel) + new_last_msg_id = await backup_channel(channel, last_msg_id) + if new_last_msg_id is not None: + await set_last_message_id(channel, new_last_msg_id) + + # Backup threads in channel + for thread in channel.threads: + print(f'Backing up Thread {thread.id} in Channel {channel.name} on {channel.guild.name}') + + last_msg_id = await get_last_message_id(thread) + new_last_msg_id = await backup_channel(thread, last_msg_id) + if new_last_msg_id is not None: + await set_last_message_id(thread, new_last_msg_id) + except Exception as e: # pylint: disable=W0718 + wait_time = attempt * 5 + print(f'\tAttempt {attempt} failed ({e}), retrying in {wait_time} seconds...') + time.sleep(wait_time) + print("\tMax retries reached. Function execution failed.") # Quit when done print('Notifying the heartbeat check...')