Add support for linebreaks, parse id's beforehand to check properly, add leading zeroes to timestamp

pull/42/head
Stijn De Clercq 2021-03-03 18:37:09 +01:00
parent 1372f0a996
commit db0e7b8f9b
2 changed files with 26 additions and 19 deletions

View File

@ -1,15 +1,16 @@
from datetime import datetime, timedelta from datetime import datetime, timedelta
from discord import Embed, Colour from discord import Embed, Colour
from functions.stringFormatters import leadingZero as lz
from functions.timeFormatters import intToWeekday from functions.timeFormatters import intToWeekday
import pytz import pytz
import re import re
class UforaNotification: class UforaNotification:
def __init__(self, content: dict, course): def __init__(self, content: dict, course, notif_id, course_id):
self._content: dict = content self._content: dict = content
self._course = course self._course = course
self._notif_id, self._course_id = self._find_ids(self._content["id"]) self._notif_id, self._course_id = notif_id, course_id
self._view_url = self._create_url() self._view_url = self._create_url()
self._title = self._clean_content(self._content["title"]) self._title = self._clean_content(self._content["title"])
self._description = self._get_description() self._description = self._get_description()
@ -35,16 +36,6 @@ class UforaNotification:
return "https://ufora.ugent.be/d2l/le/news/{0}/{1}/view?ou={0}".format(self._course_id, self._notif_id) return "https://ufora.ugent.be/d2l/le/news/{0}/{1}/view?ou={0}".format(self._course_id, self._notif_id)
def _find_ids(self, url: str):
match = re.search(r"[0-9]+-[0-9]+$", url)
if not match:
return None, None
spl = match[0].split("-")
return spl[0], spl[1]
def _get_description(self): def _get_description(self):
desc = self._clean_content(self._content["summary"]) desc = self._clean_content(self._content["summary"])
@ -76,7 +67,10 @@ class UforaNotification:
"<ins>": "__", "<ins>": "__",
"</ins>": "__", "</ins>": "__",
# Represent paragraphs with newlines # Represent paragraphs with newlines
"</p>": "\n" "</p>": "\n",
"<br>": "\n",
"<br/>": "\n",
"<br />": "\n"
} }
# Unescape HTML # Unescape HTML
@ -96,6 +90,6 @@ class UforaNotification:
return "{} {}/{}/{} om {}:{}:{}".format( return "{} {}/{}/{} om {}:{}:{}".format(
intToWeekday(dt.weekday()), intToWeekday(dt.weekday()),
dt.day, dt.month, dt.year, lz(dt.day), lz(dt.month), lz(dt.year),
dt.hour, dt.minute, dt.second lz(dt.hour), lz(dt.minute), lz(dt.second)
) )

View File

@ -1,3 +1,5 @@
import re
import feedparser import feedparser
from data.embeds import UforaNotification from data.embeds import UforaNotification
import json import json
@ -37,14 +39,14 @@ def run():
feed = feedparser.parse(url) feed = feedparser.parse(url)
# Filter out old notifications # Filter out old notifications
feed = list(filter(lambda f: f["id"] not in notifications[course], feed.entries)) feed = list(filter(lambda f: _parse_ids(f["id"])[0] not in notifications[course], feed.entries))
if feed: if feed:
for item in feed: for item in feed:
notification = UforaNotification(item, course) notif_id, course_id = _parse_ids(item["id"])
new_notifications.append(notification) new_notifications.append(UforaNotification(item, course, notif_id, course_id))
notifications[course].append(notification.get_id()) notifications[course].append(notif_id)
# Update list of notifications # Update list of notifications
if new_notifications: if new_notifications:
@ -52,3 +54,14 @@ def run():
json.dump(notifications, fp) json.dump(notifications, fp)
return new_notifications return new_notifications
def _parse_ids(url: str):
match = re.search(r"[0-9]+-[0-9]+$", url)
if not match:
return None, None
spl = match[0].split("-")
return spl[0], spl[1]