Skip to content

Commit

Permalink
refactor md functions into utils
Browse files Browse the repository at this point in the history
  • Loading branch information
wizzdom committed Jan 16, 2025
1 parent 5829bcb commit b66a386
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 58 deletions.
26 changes: 5 additions & 21 deletions src/extensions/action_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
51 changes: 14 additions & 37 deletions src/extensions/agenda.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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>
Expand Down
42 changes: 42 additions & 0 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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

0 comments on commit b66a386

Please sign in to comment.