didier/data/embeds/ufora.py

67 lines
2.2 KiB
Python
Raw Normal View History

from datetime import datetime
2021-03-03 18:04:31 +01:00
from discord import Embed, Colour
from functions.stringFormatters import leadingZero as lz
2021-03-03 18:04:31 +01:00
from functions.timeFormatters import intToWeekday
from markdownify import markdownify as md
2021-03-03 18:04:31 +01:00
import pytz
class UforaNotification:
def __init__(self, content: dict, course, notif_id, course_id):
2021-03-03 18:04:31 +01:00
self._content: dict = content
self._course = course
self._notif_id = notif_id
self._course_id = course_id
2021-03-03 18:04:31 +01:00
self._view_url = self._create_url()
self._title = self._clean_content(self._content["title"])
self._description = self._get_description()
self._published = self._get_published()
def to_embed(self):
embed = Embed(colour=Colour.from_rgb(30, 100, 200))
embed.set_author(name=self._course)
embed.title = self._title
embed.url = self._view_url
embed.description = self._description
embed.set_footer(text=self._published)
return embed
def get_id(self):
return int(self._notif_id) if self._notif_id is not None else self._content["id"]
2021-03-03 18:04:31 +01:00
def _create_url(self):
if self._notif_id is None or self._course_id is None:
return self._content["link"]
2021-03-03 18:16:23 +01:00
return "https://ufora.ugent.be/d2l/le/news/{0}/{1}/view?ou={0}".format(self._course_id, self._notif_id)
2021-03-03 18:04:31 +01:00
def _get_description(self):
desc = self._clean_content(self._content["summary"])
if len(desc) > 4096:
return desc[:4093] + "..."
2021-03-03 18:04:31 +01:00
return desc
def _clean_content(self, text: str):
return md(text)
2021-03-03 18:04:31 +01:00
def _get_published(self):
# Datetime is unable to parse the timezone because it's useless
# We will hereby cut it out and pray the timezone will always be UTC+0
published = self._content["published"].rsplit(" ", 1)[0]
time_string = "%a, %d %b %Y %H:%M:%S"
dt = datetime.strptime(published, time_string)\
2021-03-03 18:04:31 +01:00
.astimezone(pytz.timezone("Europe/Brussels"))
# Apply timezone offset in a hacky way
dt = dt + dt.utcoffset()
2021-03-03 18:04:31 +01:00
return "{} {}/{}/{} om {}:{}:{}".format(
intToWeekday(dt.weekday()),
lz(dt.day), lz(dt.month), lz(dt.year),
lz(dt.hour), lz(dt.minute), lz(dt.second)
2021-03-03 18:04:31 +01:00
)