diff --git a/src/discord/cogs/tasks.py b/src/discord/cogs/tasks.py index 39b65a5..d97b1c9 100644 --- a/src/discord/cogs/tasks.py +++ b/src/discord/cogs/tasks.py @@ -1,6 +1,9 @@ from discord.ext import tasks, commands +import asyncio +from datetime import timedelta from utils.custom_logger import logger +from utils.datetime import TimeCalculator from src.trakt.functions import process_ratings, process_favorites from config.globals import TRAKT_CHANNEL, TRAKT_USERNAME @@ -18,7 +21,7 @@ async def trakt_ratings(self): await process_ratings(ratings_channel, TRAKT_USERNAME) except Exception as e: logger.error(f'Error processing recent Trakt ratings: {e}') - + # Task to process recent Trakt favorites @tasks.loop(hours=24) async def trakt_favorites(self): @@ -28,6 +31,18 @@ async def trakt_favorites(self): except Exception as e: logger.error(f'Error processing recent Trakt ratings: {e}') + @trakt_ratings.before_loop + async def before_trakt_ratings(self): + seconds = TimeCalculator.seconds_until_next_hour() + logger.info(f'Trakt ratings task will start in {str(timedelta(seconds=seconds))}') + await asyncio.sleep(seconds) + + @trakt_favorites.before_loop + async def before_trakt_favorites(self): + seconds = TimeCalculator.seconds_until_next_day() + logger.info(f'Trakt favorites task will start in {str(timedelta(seconds=seconds))}') + await asyncio.sleep(seconds) + async def setup(bot): logger.info('Cogs have been loaded') await bot.add_cog(TasksCog(bot)) \ No newline at end of file diff --git a/src/trakt/functions.py b/src/trakt/functions.py index d69a73a..17f6207 100644 --- a/src/trakt/functions.py +++ b/src/trakt/functions.py @@ -1,8 +1,6 @@ -from utils.datetime import get_times - from src.discord.embed import EmbedBuilder from api.trakt.client import TraktClient -from utils.datetime import get_times +from utils.datetime import TimeCalculator from config.globals import TRAKT_ICON @@ -10,8 +8,8 @@ async def process_ratings(ratings_channel, username): client = TraktClient() user = client.user(username) - now, one_hour_ago = get_times() - ratings = user.get_ratings(start_time=one_hour_ago, end_time=now) + now, then = TimeCalculator.get_time_ago(hours=1) + ratings = sorted(user.get_ratings(start_time=then, end_time=now), key=lambda rating: rating.date) async def process_rating(rating): description_formats = { @@ -47,8 +45,8 @@ async def process_rating(rating): async def process_favorites(favorites_channel, username): client = TraktClient() user = client.user(username) - now, one_hour_ago = get_times() - favorites = sorted(user.get_favorites(start_time=one_hour_ago, end_time=now), key=lambda favorite: favorite.date) + now, then = TimeCalculator.get_time_ago(hours=24) + favorites = sorted(user.get_favorites(start_time=then, end_time=now), key=lambda favorite: favorite.date) async def process_favorite(favorite): description_formats = { diff --git a/utils/datetime.py b/utils/datetime.py index fa8fb4d..51abb78 100644 --- a/utils/datetime.py +++ b/utils/datetime.py @@ -1,6 +1,22 @@ from datetime import datetime, timedelta -def get_times(): - now = datetime.now() - one_hour_ago = now - timedelta(hours=1) - return now, one_hour_ago \ No newline at end of file +class TimeCalculator: + @staticmethod + def get_time_ago(hours=0, minutes=0): + now = datetime.now() + time_ago = now - timedelta(hours=hours, minutes=minutes) + return now, time_ago + + @staticmethod + def seconds_until_next_hour(): + now = datetime.now() + next_hour = (now + timedelta(hours=1)).replace(minute=0, second=0, microsecond=0) + delta = next_hour - now + return delta.seconds + + @staticmethod + def seconds_until_next_day(): + now = datetime.now() + next_day = (now + timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0) + delta = next_day - now + return delta.seconds \ No newline at end of file