From 4025e0dd510195133b90f462a647b55668c2ad12 Mon Sep 17 00:00:00 2001 From: wizzdom Date: Mon, 20 Jan 2025 10:12:32 +0000 Subject: [PATCH] Refactor MD functions into utils (#67) Co-authored-by: nova --- src/extensions/action_items.py | 32 +++++++------------ src/extensions/agenda.py | 57 ++++++++++++---------------------- src/utils.py | 36 +++++++++++++++++++++ 3 files changed, 67 insertions(+), 58 deletions(-) diff --git a/src/extensions/action_items.py b/src/extensions/action_items.py index eecb1ed..507eb02 100644 --- a/src/extensions/action_items.py +++ b/src/extensions/action_items.py @@ -1,5 +1,4 @@ import re -from urllib.parse import urlparse import aiohttp import arc @@ -7,7 +6,7 @@ from src.config import CHANNEL_IDS, ROLE_IDS, UID_MAPS from src.hooks import restrict_to_channels, restrict_to_roles -from src.utils import hedgedoc_login, role_mention +from src.utils import get_md_content, role_mention action_items = arc.GatewayPlugin(name="Action Items") @@ -28,29 +27,20 @@ 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 aiohttp.ClientResponseError as e: await ctx.respond( - f"❌ `{url}` is not a valid MD URL. Please provide a valid URL.", + f"❌ Failed to fetch the minutes. Status code: `{e.status}`", + flags=hikari.MessageFlag.EPHEMERAL, + ) + return + except ValueError as e: + await ctx.respond( + 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( diff --git a/src/extensions/agenda.py b/src/extensions/agenda.py index 5ae3c58..51bb387 100644 --- a/src/extensions/agenda.py +++ b/src/extensions/agenda.py @@ -1,5 +1,4 @@ import datetime -from urllib.parse import urlparse import aiohttp import arc @@ -7,7 +6,7 @@ from src.config import AGENDA_TEMPLATE_URL, CHANNEL_IDS, ROLE_IDS, UID_MAPS from src.hooks import restrict_to_channels, restrict_to_roles -from src.utils import hedgedoc_login, role_mention, utcnow +from src.utils import get_md_content, post_new_md_content, role_mention, utcnow plugin = arc.GatewayPlugin(name="Agenda") @@ -100,29 +99,20 @@ 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 aiohttp.ClientResponseError as e: await ctx.respond( - f"❌ `{url}` is not a valid MD URL. Please provide a valid URL.", + f"❌ Failed to fetch the agenda template. Status code: `{e.status}`", + flags=hikari.MessageFlag.EPHEMERAL, + ) + return + except ValueError as e: + await ctx.respond( + 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, @@ -130,22 +120,15 @@ async def gen_agenda( 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 aiohttp.ClientResponseError as e: + await ctx.respond( + f"❌ Failed to generate the agenda. Status code: `{e.status}`", + 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 7cb87d9..a01f4f9 100644 --- a/src/utils.py +++ b/src/utils.py @@ -1,4 +1,5 @@ import datetime +from urllib.parse import urlparse import aiohttp import hikari @@ -27,5 +28,40 @@ 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: + """ + 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: + response.raise_for_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: + response.raise_for_status() + + return str(response.url) + + def utcnow() -> datetime.datetime: return datetime.datetime.now(datetime.timezone.utc)