Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: delay tasks #15

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/discord/cogs/tasks.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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):
Expand All @@ -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))
12 changes: 5 additions & 7 deletions src/trakt/functions.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
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

# Function to process recent ratings and send them to a specified Discord channel as embeds
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 = {
Expand Down Expand Up @@ -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 = {
Expand Down
24 changes: 20 additions & 4 deletions utils/datetime.py
Original file line number Diff line number Diff line change
@@ -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
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
Loading