Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor md functions into utils #67

Merged
merged 6 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")

Expand All @@ -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(
Expand Down
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")

Expand Down Expand Up @@ -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>

Expand Down
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
Expand Down Expand Up @@ -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)
Loading