Skip to content

Commit

Permalink
Refactor MD functions into utils (#67)
Browse files Browse the repository at this point in the history
Co-authored-by: nova <[email protected]>
wizzdom and novanai authored Jan 20, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent df37984 commit 4025e0d
Showing 3 changed files with 67 additions and 58 deletions.
32 changes: 11 additions & 21 deletions src/extensions/action_items.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import re
from urllib.parse import urlparse

import aiohttp
import arc
import hikari

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(
57 changes: 20 additions & 37 deletions src/extensions/agenda.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import datetime
from urllib.parse import urlparse

import aiohttp
import arc
import hikari

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,52 +99,36 @@ 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,
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 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>
36 changes: 36 additions & 0 deletions src/utils.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 4025e0d

Please sign in to comment.