Merge migrations, pull schedules daily

This commit is contained in:
stijndcl 2022-09-17 19:22:27 +02:00
parent ac24688a73
commit 8fea65e4ad
23 changed files with 337 additions and 566 deletions

View file

@ -41,6 +41,7 @@ class Tasks(commands.Cog):
self._tasks = {
"birthdays": self.check_birthdays,
"schedules": self.pull_schedules,
"ufora": self.pull_ufora_announcements,
"remove_ufora": self.remove_old_ufora_announcements,
"wordle": self.reset_wordle_word,
@ -59,6 +60,7 @@ class Tasks(commands.Cog):
# Start other tasks
self.reset_wordle_word.start()
self.pull_schedules.start()
@overrides
def cog_unload(self) -> None:
@ -110,6 +112,32 @@ class Tasks(commands.Cog):
async def _before_check_birthdays(self):
await self.client.wait_until_ready()
@tasks.loop(time=DAILY_RESET_TIME)
@timed_task(enums.TaskType.SCHEDULES)
async def pull_schedules(self, **kwargs):
"""Task that pulls the schedules & saves the files locally
Schedules are then parsed & cached in memory
"""
_ = kwargs
for data in settings.SCHEDULE_DATA:
if data.schedule_url is None:
return
async with self.client.http_session.get(data.schedule_url) as response:
# If a schedule couldn't be fetched, log it and move on
if response.status != 200:
await self.client.log_warning(
f"Unable to fetch schedule {data.name} (status {response.status}).", log_to_discord=False
)
continue
# Write the content to a file
content = await response.text()
with open(f"files/schedules/{data.name}.ics", "w+") as fp:
fp.write(content)
@tasks.loop(minutes=10)
@timed_task(enums.TaskType.UFORA_ANNOUNCEMENTS)
async def pull_ufora_announcements(self, **kwargs):
@ -166,3 +194,4 @@ async def setup(client: Didier):
cog = Tasks(client)
await client.add_cog(cog)
await cog.reset_wordle_word()
await cog.pull_schedules()

View file

@ -1,6 +1,6 @@
# The year in which we were in 1Ba
import settings
# The year in which we were in 1Ba
FIRST_YEAR = 2019
# Year to use when adding the current year of our education
# to find the academic year

View file

@ -1,5 +1,6 @@
import logging
import os
import pathlib
import discord
from aiohttp import ClientSession
@ -59,6 +60,9 @@ class Didier(commands.Bot):
This hook is called once the bot is initialised
"""
# Create directories that are ignored on GitHub
self._create_ignored_directories()
# Load the Wordle dictionary
self._load_wordle_words()
@ -67,19 +71,26 @@ class Didier(commands.Bot):
async with self.postgres_session as session:
await self.database_caches.initialize_caches(session)
# Create aiohttp session
self.http_session = ClientSession()
# Load extensions
await self._load_initial_extensions()
await self._load_directory_extensions("didier/cogs")
# Create aiohttp session
self.http_session = ClientSession()
# Configure channel to send errors to
if settings.ERRORS_CHANNEL is not None:
self.error_channel = self.get_channel(settings.ERRORS_CHANNEL)
else:
self.error_channel = self.get_user(self.owner_id)
def _create_ignored_directories(self):
"""Create directories that store ignored data"""
ignored = ["files/schedules"]
for directory in ignored:
pathlib.Path(directory).mkdir(exist_ok=True, parents=True)
async def _load_initial_extensions(self):
"""Load all extensions that should be loaded before the others"""
for extension in self.initial_extensions:
@ -138,13 +149,27 @@ class Didier(commands.Bot):
"""Add an X to a message"""
await message.add_reaction("")
async def log_error(self, message: str, log_to_discord: bool = True):
"""Send an error message to the logs, and optionally the configured channel"""
logger.error(message)
async def _log(self, level: int, message: str, log_to_discord: bool = True):
"""Log a message to the logging file, and optionally to the configured channel"""
methods = {
logging.ERROR: logger.error,
logging.WARNING: logger.warning,
}
methods.get(level, logger.error)(message)
if log_to_discord:
# TODO pretty embed
# different colours per level?
await self.error_channel.send(message)
async def log_error(self, message: str, log_to_discord: bool = True):
"""Log an error message"""
await self._log(logging.ERROR, message, log_to_discord)
async def log_warning(self, message: str, log_to_discord: bool = True):
"""Log a warning message"""
await self._log(logging.WARNING, message, log_to_discord)
async def on_ready(self):
"""Event triggered when the bot is ready"""
print(settings.DISCORD_READY_MESSAGE)