diff --git a/src/extensions/action_items.py b/src/extensions/action_items.py index a972ca5..8a4b535 100644 --- a/src/extensions/action_items.py +++ b/src/extensions/action_items.py @@ -2,9 +2,8 @@ import hikari import re import aiohttp -from urllib.parse import urlparse -from src.utils import role_mention, hedgedoc_login +from src.utils import role_mention, get_md_content from src.hooks import restrict_to_channels, restrict_to_roles from src.config import CHANNEL_IDS, ROLE_IDS, UID_MAPS @@ -28,30 +27,15 @@ async def get_action_items( ) -> None: """Display the action items from the MD!""" - if "https://md.redbrick.dcu.ie" not in url: + try: + content = await get_md_content(url, aiohttp_client) + except Exception as e: await ctx.respond( - f"❌ `{url}` is not a valid MD URL. Please provide a valid URL.", + f"❌ {e}", flags=hikari.MessageFlag.EPHEMERAL, ) return - await hedgedoc_login(aiohttp_client) - - parsed_url = urlparse(url) - request_url = ( - f"{parsed_url.scheme}://{parsed_url.hostname}{parsed_url.path}/download" - ) - - async with aiohttp_client.get(request_url) as response: - if response.status != 200: - await ctx.respond( - f"❌ Failed to fetch the minutes. Status code: `{response.status}`", - flags=hikari.MessageFlag.EPHEMERAL, - ) - return - - content = await response.text() - # extract the action items section from the minutes action_items_section = re.search( r"# Action Items:?\n(.*?)(\n# |\n---|$)", content, re.DOTALL diff --git a/src/extensions/agenda.py b/src/extensions/agenda.py index 3b6f066..8dc809a 100644 --- a/src/extensions/agenda.py +++ b/src/extensions/agenda.py @@ -1,10 +1,9 @@ import arc import hikari import aiohttp -from urllib.parse import urlparse import datetime -from src.utils import role_mention, hedgedoc_login +from src.utils import role_mention, get_md_content, post_new_md_content from src.hooks import restrict_to_channels, restrict_to_roles from src.config import CHANNEL_IDS, ROLE_IDS, UID_MAPS, AGENDA_TEMPLATE_URL @@ -90,50 +89,28 @@ async def gen_agenda( formatted_time = parsed_datetime.strftime("%H:%M") formatted_datetime = parsed_datetime.strftime("%A, %Y-%m-%d %H:%M") - if "https://md.redbrick.dcu.ie" not in url: + try: + content = await get_md_content(url, aiohttp_client) + except Exception as e: await ctx.respond( - f"❌ `{url}` is not a valid MD URL. Please provide a valid URL.", + f"❌ {e}", flags=hikari.MessageFlag.EPHEMERAL, ) return - await hedgedoc_login(aiohttp_client) - - parsed_url = urlparse(url) - request_url = ( - f"{parsed_url.scheme}://{parsed_url.hostname}{parsed_url.path}/download" - ) - - async with aiohttp_client.get(request_url) as response: - if response.status != 200: - await ctx.respond( - f"❌ Failed to fetch the agenda template. Status code: `{response.status}`", - flags=hikari.MessageFlag.EPHEMERAL, - ) - return - - content = await response.text() - modified_content = content.format( DATE=formatted_date, TIME=formatted_time, ROOM=room ) - post_url = f"{parsed_url.scheme}://{parsed_url.hostname}/new" - post_headers = {"Content-Type": "text/markdown"} - - async with aiohttp_client.post( - url=post_url, - headers=post_headers, - data=modified_content, - ) as response: - if response.status != 200: - await ctx.respond( - f"❌ Failed to generate the agenda. Status code: `{response.status}`", - flags=hikari.MessageFlag.EPHEMERAL, - ) - return - - new_agenda_url = response.url + try: + new_agenda_url = await post_new_md_content(modified_content, aiohttp_client) + except Exception as e: + await ctx.respond( + f"❌ {e}", + flags=hikari.MessageFlag.EPHEMERAL, + ) + return + announce_text = f""" ## 📣 Agenda for this week's meeting | {formatted_datetime} | {room} <:bigRed:634311607039819776> diff --git a/src/utils.py b/src/utils.py index b014819..3dcc090 100644 --- a/src/utils.py +++ b/src/utils.py @@ -2,6 +2,7 @@ from arc import GatewayClient import aiohttp from src.config import LDAP_USERNAME, LDAP_PASSWORD +from urllib.parse import urlparse async def get_guild( @@ -21,3 +22,44 @@ async def hedgedoc_login(aiohttp_client: aiohttp.ClientSession) -> None: } await aiohttp_client.post("https://md.redbrick.dcu.ie/auth/ldap", data=data) + + +async def get_md_content( + url: str, aiohttp_client: aiohttp.ClientSession +) -> str | tuple[str, urlparse]: + """ + Get the content of a note at a HedgeDoc URL. + """ + if "https://md.redbrick.dcu.ie" not in url: + raise ValueError(f"`{url}` is not a valid MD URL. Please provide a valid URL.") + await hedgedoc_login(aiohttp_client) + parsed_url = urlparse(url) + request_url = ( + f"{parsed_url.scheme}://{parsed_url.hostname}{parsed_url.path}/download" + ) + async with aiohttp_client.get(request_url) as response: + if response.status != 200: + raise Exception( + f"Failed to fetch the minutes. Status code: `{response.status}`" + ) + return await response.text() + + +async def post_new_md_content( + content: str, aiohttp_client: aiohttp.ClientSession +) -> str: + post_url = "https://md.redbrick.dcu.ie/new" + post_headers = {"Content-Type": "text/markdown"} + + async with aiohttp_client.post( + url=post_url, + headers=post_headers, + data=content, + ) as response: + if response.status != 200: + raise Exception( + f"Failed to generate the agenda. Status code: `{response.status}`" + ) + return + + return await response.url