From 6568cfdcce562110d55d734e188372632bbe8e64 Mon Sep 17 00:00:00 2001
From: Stijn De Clercq
Date: Wed, 3 Mar 2021 15:36:18 +0100
Subject: [PATCH 01/11] Inspire command (fixes #40), disable Launch
---
cogs/launch.py | 5 +++--
cogs/oneliners.py | 17 ++++++++++++++---
files/help.json | 2 +-
3 files changed, 18 insertions(+), 6 deletions(-)
diff --git a/cogs/launch.py b/cogs/launch.py
index d0604ed..4950695 100644
--- a/cogs/launch.py
+++ b/cogs/launch.py
@@ -8,6 +8,7 @@ import pytz
import requests
+# Temporarily disabled because of API (setup @ bottom)
class Launch(commands.Cog):
def __init__(self, client):
self.client = client
@@ -50,5 +51,5 @@ class Launch(commands.Cog):
datetime.fromtimestamp(int(d) + 7200, pytz.timezone("Europe/Brussels")).strftime('%B %d %Y om %H:%M:%S'))
-def setup(client):
- client.add_cog(Launch(client))
+# def setup(client):
+# client.add_cog(Launch(client))
diff --git a/cogs/oneliners.py b/cogs/oneliners.py
index 0c67f42..20bc822 100644
--- a/cogs/oneliners.py
+++ b/cogs/oneliners.py
@@ -6,6 +6,7 @@ from discord.ext import commands
from enums.help_categories import Category
from functions import checks, clap, mock, sunrise, timeFormatters
import pytz
+from requests import get
import time
import urllib.parse
@@ -101,7 +102,7 @@ class Oneliners(commands.Cog):
@commands.command(name="Todo", aliases=["List", "Td"])
@help.Category(category=Category.Didier)
- async def todo(self, ctx, *args):
+ async def todo(self, ctx):
await ctx.send("https://trello.com/b/PdtsAJea/didier-to-do-list")
@commands.command(name="LMGTFY", aliases=["Dsfr"], usage="[Query]")
@@ -130,7 +131,7 @@ class Oneliners(commands.Cog):
await ctx.send("Shut, it already is.")
@commands.command()
- async def sc(self, ctx, *args):
+ async def sc(self, ctx):
await ctx.send("http://take-a-screenshot.org/")
@commands.command(aliases=["os", "sauce", "src"])
@@ -144,9 +145,19 @@ class Oneliners(commands.Cog):
await ctx.send(":sunny:: **{}**\n:crescent_moon:: **{}**".format(s.sunrise(), s.sunset()))
@commands.command(name="Tias", aliases=["TryIt"])
- async def tias(self, ctx, *args):
+ async def tias(self, ctx):
await ctx.send("***Try it and see***")
+ @commands.command(name="Inspire")
+ @help.Category(Category.Other)
+ async def inspire(self, ctx):
+ image = get("http://inspirobot.me/api?generate=true")
+
+ if image.status_code == 200:
+ await ctx.send(image.text)
+ else:
+ await ctx.send("Uh oh API down.")
+
def setup(client):
client.add_cog(Oneliners(client))
diff --git a/files/help.json b/files/help.json
index 09f640e..3fe70ee 100644
--- a/files/help.json
+++ b/files/help.json
@@ -46,12 +46,12 @@
"hangman start": "Start een nieuwe Hangman game indien er nog geen bezig is. Indien je geen woord opgeeft, wordt er een willekeurig woord gekozen.\n**Indien je wel een woord opgeeft, werkt dit enkel in DM.**",
"hangman guess": "Probeer het woord te raden.",
"claim": "Claim [Aantal] Didier Dinks uit je profit.\nIndien je geen aantal opgeeft (of \"all\"), claim je alles, inclusief je investering.",
+ "inspire": "Generate quotes via [InspiroBot](https://inspirobot.me/).",
"inventory": "Bekijk de items in jouw inventory.",
"invest": "Investeer [Aantal] Didier Dinks in jouw Didier Bank om rente te vergaren.",
"jpl": "Informatie over de Jupiler Pro League.",
"jpl matches": "Bekijk de wedstrijden die gespeeld worden op [Week]. Default naar de huidige speeldag.",
"jpl table": "De huidige stand van het klassement.",
- "launch": "Tijdstip van de volgende SpaceX lancering.",
"leaderboard": "Bekijk de Top 10 van [Categorie].\nIndien je geen categorie opgeeft krijg je een lijst van categorieën.",
"les": "Bekijk het lessenrooster voor [Dag] in het [Jaargang]-de jaar.\nIndien je geen dag opgeeft, is dit standaard vandaag. De jaargang is standaard 2.\nLes Morgen/Overmorgen werkt ook.",
"lmgtfy": "Stuur iemand een LMGTFY link wanneer ze je een domme vraag stellen in plaats van het zelf op te zoeken.\nQueries met spaties moeten **niet** tussen aanhalingstekens staan.",
From c374fce3f3245b2aa2588ffc38202c48bff95049 Mon Sep 17 00:00:00 2001
From: Stijn De Clercq
Date: Wed, 3 Mar 2021 15:44:18 +0100
Subject: [PATCH 02/11] Properly disable launch
---
cogs/launch.py | 5 +++--
functions/ufora_notifications.py | 1 +
2 files changed, 4 insertions(+), 2 deletions(-)
create mode 100644 functions/ufora_notifications.py
diff --git a/cogs/launch.py b/cogs/launch.py
index 4950695..14568c7 100644
--- a/cogs/launch.py
+++ b/cogs/launch.py
@@ -51,5 +51,6 @@ class Launch(commands.Cog):
datetime.fromtimestamp(int(d) + 7200, pytz.timezone("Europe/Brussels")).strftime('%B %d %Y om %H:%M:%S'))
-# def setup(client):
-# client.add_cog(Launch(client))
+def setup(client):
+ pass
+ # client.add_cog(Launch(client))
diff --git a/functions/ufora_notifications.py b/functions/ufora_notifications.py
new file mode 100644
index 0000000..464c436
--- /dev/null
+++ b/functions/ufora_notifications.py
@@ -0,0 +1 @@
+import feedparser
\ No newline at end of file
From ade7f8b72e2eea52e7fa1973de9d828fb214f74c Mon Sep 17 00:00:00 2001
From: Stijn De Clercq
Date: Wed, 3 Mar 2021 15:57:35 +0100
Subject: [PATCH 03/11] Start working on ufora notifications, add new course
id's to FAQ
---
data/constants.py | 8 +++++++-
data/embeds.py | 5 +++++
functions/ufora_notifications.py | 21 ++++++++++++++++++++-
3 files changed, 32 insertions(+), 2 deletions(-)
create mode 100644 data/embeds.py
diff --git a/data/constants.py b/data/constants.py
index 0d3facf..fa70a18 100644
--- a/data/constants.py
+++ b/data/constants.py
@@ -14,6 +14,7 @@ botIDs = [
"728361496874057812"
]
BugReports = "762668401960812554"
+BotTesting = "679701786189103106"
CallOfCode = "626699611192688641"
CoCGeneral = "626699611813314561"
DeZandbak = "728361030404538488"
@@ -53,7 +54,12 @@ faq_channels = {
727876797458284584: "funcprog",
727876819264733244: "statprob",
727876836587208714: "sysprog",
- 676713433567199232: "didier"
+ 676713433567199232: "didier",
+ 807566495550013460: "comparch",
+ 807567007355895838: "multimedia",
+ 807567261216538644: "sel1",
+ 807567345484169237: "webdev",
+ 807567387775336499: "wetrek"
}
diff --git a/data/embeds.py b/data/embeds.py
new file mode 100644
index 0000000..dba8123
--- /dev/null
+++ b/data/embeds.py
@@ -0,0 +1,5 @@
+from discord import embeds
+
+
+class UforaNotification:
+ pass
diff --git a/functions/ufora_notifications.py b/functions/ufora_notifications.py
index 464c436..1551b81 100644
--- a/functions/ufora_notifications.py
+++ b/functions/ufora_notifications.py
@@ -1 +1,20 @@
-import feedparser
\ No newline at end of file
+import feedparser
+from data.constants import BotTesting
+
+
+# TODO when it works, move to announcements channel
+notifications_channel = BotTesting
+
+
+course_urls = {
+ "Algoritmen en Datastructuren 2": "https://ufora.ugent.be/d2l/le/news/rss/222018/course?token=aehhv6utkf46t8cc102e0&ou=222018",
+ "Communicatienetwerken": "https://ufora.ugent.be/d2l/le/news/rss/221184/course?token=aehhv6utkf46t8cc102e0&ou=221184",
+ "Computerarchitectuur": "https://ufora.ugent.be/d2l/le/news/rss/228912/course?token=aehhv6utkf46t8cc102e0&ou=228912",
+ "Functioneel Programmeren": "https://ufora.ugent.be/d2l/le/news/rss/236396/course?token=aehhv6utkf46t8cc102e0&ou=236396",
+ "Multimedia": "https://ufora.ugent.be/d2l/le/news/rss/236949/course?token=aehhv6utkf46t8cc102e0&ou=236949",
+ "Software Engineering Lab 1": "https://ufora.ugent.be/d2l/le/news/rss/235800/course?token=aehhv6utkf46t8cc102e0&ou=235800",
+ "Statistiek en Probabiliteit": "https://ufora.ugent.be/d2l/le/news/rss/236398/course?token=aehhv6utkf46t8cc102e0&ou=236398",
+ "Systeemprogrammeren": "https://ufora.ugent.be/d2l/le/news/rss/222035/course?token=aehhv6utkf46t8cc102e0&ou=222035",
+ "Webdevelopment": "https://ufora.ugent.be/d2l/le/news/rss/223449/course?token=aehhv6utkf46t8cc102e0&ou=223449",
+ "Wetenschappelijk Rekenen": "https://ufora.ugent.be/d2l/le/news/rss/236404/course?token=aehhv6utkf46t8cc102e0&ou=236404"
+}
\ No newline at end of file
From 779a84828b828c1508a3ff54ddade5f09a34c953 Mon Sep 17 00:00:00 2001
From: Stijn De Clercq
Date: Wed, 3 Mar 2021 18:04:31 +0100
Subject: [PATCH 04/11] Announcements work + task
---
.gitignore | 1 +
cogs/tasks.py | 21 +++++++++
data/constants.py | 1 +
data/embeds.py | 5 --
data/embeds/__init__.py | 1 +
data/embeds/ufora.py | 81 ++++++++++++++++++++++++++++++++
functions/timeFormatters.py | 3 +-
functions/ufora_notifications.py | 45 +++++++++++++++---
8 files changed, 145 insertions(+), 13 deletions(-)
delete mode 100644 data/embeds.py
create mode 100644 data/embeds/__init__.py
create mode 100644 data/embeds/ufora.py
diff --git a/.gitignore b/.gitignore
index a2da7e1..df1d76e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,6 +8,7 @@ files/stats.json
files/lost.json
files/locked.json
files/database.json
+files/ufora_notifications.json
.idea/
__pycache__
.env
\ No newline at end of file
diff --git a/cogs/tasks.py b/cogs/tasks.py
index d38e14c..14c84ff 100644
--- a/cogs/tasks.py
+++ b/cogs/tasks.py
@@ -6,6 +6,7 @@ from functions import timeFormatters
from functions.config import config
from functions.database import currency, poke, prison, birthdays, stats
from functions.scraping import getMatchweek
+from functions import ufora_notifications
import json
import random
import requests
@@ -24,6 +25,7 @@ class Tasks(commands.Cog):
self.updateMessageCounts.start()
self.sendReminders.start()
self.updateMatchweek.start()
+ self.uforaAnnouncements.start()
@tasks.loop(hours=1.0)
async def bankInterest(self):
@@ -238,6 +240,25 @@ class Tasks(commands.Cog):
async def beforeUpdateMatchweek(self):
await self.client.wait_until_ready()
+ @tasks.loop(minutes=2)
+ async def uforaAnnouncements(self):
+ """
+ Task that checks for new Ufora announcements every few minutes
+ """
+ # Get new notifications
+ announcements = ufora_notifications.run()
+
+ if announcements:
+ # TODO change to COC Bot testing if it works
+ announcements_channel = self.client.get_channel(int(constants.ZandbakSpeeltuin))
+
+ for an in announcements:
+ await announcements_channel.send(embed=an.to_embed())
+
+ @uforaAnnouncements.before_loop
+ async def beforeUforaAnnouncements(self):
+ await self.client.wait_until_ready()
+
def getCurrentHour(self):
return timeFormatters.dateTimeNow().hour
diff --git a/data/constants.py b/data/constants.py
index fa70a18..8f8a9cb 100644
--- a/data/constants.py
+++ b/data/constants.py
@@ -20,6 +20,7 @@ CoCGeneral = "626699611813314561"
DeZandbak = "728361030404538488"
ErrorLogs = "762668505455132722"
FeatureRequests = "762668473313787964"
+ZandbakSpeeltuin = "769248992957038612"
mods = {
626699611192688641: [384457911377854467, 171671190631481345],
diff --git a/data/embeds.py b/data/embeds.py
deleted file mode 100644
index dba8123..0000000
--- a/data/embeds.py
+++ /dev/null
@@ -1,5 +0,0 @@
-from discord import embeds
-
-
-class UforaNotification:
- pass
diff --git a/data/embeds/__init__.py b/data/embeds/__init__.py
new file mode 100644
index 0000000..55d7068
--- /dev/null
+++ b/data/embeds/__init__.py
@@ -0,0 +1 @@
+from .ufora import UforaNotification
diff --git a/data/embeds/ufora.py b/data/embeds/ufora.py
new file mode 100644
index 0000000..9d1d13c
--- /dev/null
+++ b/data/embeds/ufora.py
@@ -0,0 +1,81 @@
+from datetime import datetime, timedelta
+from discord import Embed, Colour
+from functions.timeFormatters import intToWeekday
+import pytz
+import re
+
+
+class UforaNotification:
+ def __init__(self, content: dict, course):
+ self._content: dict = content
+ self._course = course
+ self._notif_id, self._course_id = self._find_ids(self._content["id"])
+ 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 _create_url(self):
+ if self._notif_id is None or self._course_id is None:
+ return self._content["link"]
+
+ return "https://ufora.ugent.be/d2l/le/news/{0}/{1}/view?ou={0}".format(self._notif_id, self._course_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):
+ desc = self._clean_content(self._content["summary"])
+
+ if len(desc) > 500:
+ return desc[:500]
+
+ return desc
+
+ def _clean_content(self, text: str):
+ html_table = {
+ "&": '&',
+ """: '"',
+ "apos;": "'",
+ ">": ">",
+ "<": "<"
+ }
+
+ # Unescape HTML
+ for key, value in html_table.items():
+ text = text.replace(key, value)
+
+ # Remove HTML tags
+ return re.sub(r"<[^>]*>", "", text)
+
+ def _get_published(self):
+ time_string = "%a, %d %b %Y %H:%M:%S %Z"
+ dt = datetime.strptime(self._content["published"], time_string)\
+ .astimezone(pytz.timezone("Europe/Brussels"))
+
+ # Apply timezone offset
+ dt = dt + timedelta(hours=dt.utcoffset().seconds//3600)
+
+ return "{} {}/{}/{} om {}:{}:{}".format(
+ intToWeekday(dt.weekday()),
+ dt.day, dt.month, dt.year,
+ dt.hour, dt.minute, dt.second
+ )
diff --git a/functions/timeFormatters.py b/functions/timeFormatters.py
index 54c4663..b79dd74 100644
--- a/functions/timeFormatters.py
+++ b/functions/timeFormatters.py
@@ -1,8 +1,7 @@
import datetime
-import time
-
import dateutil.relativedelta
import pytz
+import time
def epochToDate(epochTimeStamp, strFormat="%d/%m/%Y om %H:%M:%S"):
diff --git a/functions/ufora_notifications.py b/functions/ufora_notifications.py
index 1551b81..11b9f53 100644
--- a/functions/ufora_notifications.py
+++ b/functions/ufora_notifications.py
@@ -1,9 +1,6 @@
import feedparser
-from data.constants import BotTesting
-
-
-# TODO when it works, move to announcements channel
-notifications_channel = BotTesting
+from data.embeds import UforaNotification
+import json
course_urls = {
@@ -17,4 +14,40 @@ course_urls = {
"Systeemprogrammeren": "https://ufora.ugent.be/d2l/le/news/rss/222035/course?token=aehhv6utkf46t8cc102e0&ou=222035",
"Webdevelopment": "https://ufora.ugent.be/d2l/le/news/rss/223449/course?token=aehhv6utkf46t8cc102e0&ou=223449",
"Wetenschappelijk Rekenen": "https://ufora.ugent.be/d2l/le/news/rss/236404/course?token=aehhv6utkf46t8cc102e0&ou=236404"
-}
\ No newline at end of file
+}
+
+
+def run():
+ """
+ Check for new notifications
+ """
+ # List of new notifications
+ new_notifications = []
+
+ # List of old notifications
+ with open("files/ufora_notifications.json", "r") as fp:
+ notifications = json.load(fp)
+
+ for course, url in course_urls.items():
+ # Automatically append new/missing courses
+ if course not in notifications:
+ notifications[course] = []
+
+ # Get the updated feed
+ feed = feedparser.parse(url)
+
+ # Filter out old notifications
+ feed = list(filter(lambda f: f["id"] not in notifications[course], feed.entries))
+
+ if feed:
+ for item in feed:
+ notifications[course].append(item["id"])
+
+ new_notifications.append(UforaNotification(item, course))
+
+ # Update list of notifications
+ if new_notifications:
+ with open("files/ufora_notifications.json", "w") as fp:
+ json.dump(notifications, fp)
+
+ return new_notifications
From c9bef3b3008c721ab1c76d7430499eb585a21f6d Mon Sep 17 00:00:00 2001
From: Stijn De Clercq
Date: Wed, 3 Mar 2021 18:06:28 +0100
Subject: [PATCH 05/11] Add notifications to ignored.md
---
ignored.md | 21 ++++++++++++++++++++-
1 file changed, 20 insertions(+), 1 deletion(-)
diff --git a/ignored.md b/ignored.md
index 483e923..9ce1bdf 100644
--- a/ignored.md
+++ b/ignored.md
@@ -102,4 +102,23 @@ Contains the stats to track for gambling games. Weren't made as a PSQL table bec
Contains Didier's status message for when he logs in. Keep in mind that his activity is set to `Playing `. This was first used in Didier V1 to show whether or not he was in sandbox mode.
- with your Didier Dinks.
\ No newline at end of file
+ with your Didier Dinks.
+
+### files/ufora_notifications.json
+
+Stores ID's of all received Ufora notifications.
+
+```json
+{
+ "Algoritmen en Datastructuren 2": [],
+ "Communicatienetwerken": [],
+ "Computerarchitectuur": [],
+ "Functioneel Programmeren": [],
+ "Multimedia": [],
+ "Software Engineering Lab 1": [],
+ "Statistiek en Probabiliteit": [],
+ "Systeemprogrammeren": [],
+ "Webdevelopment": [],
+ "Wetenschappelijk Rekenen": []
+}
+```
\ No newline at end of file
From 268bb80bfde50267c3ce52585e89fdd39840dd2d Mon Sep 17 00:00:00 2001
From: Stijn De Clercq
Date: Wed, 3 Mar 2021 18:13:22 +0100
Subject: [PATCH 06/11] Add markdown support for ufora notifications, store
notifications using IDs
---
data/embeds/ufora.py | 21 ++++++++++++++++++++-
functions/ufora_notifications.py | 5 +++--
2 files changed, 23 insertions(+), 3 deletions(-)
diff --git a/data/embeds/ufora.py b/data/embeds/ufora.py
index 9d1d13c..c02cbff 100644
--- a/data/embeds/ufora.py
+++ b/data/embeds/ufora.py
@@ -26,6 +26,9 @@ class UforaNotification:
return embed
+ def get_id(self):
+ return int(self._notif_id) if self._notif_id is not None else self._content["id"]
+
def _create_url(self):
if self._notif_id is None or self._course_id is None:
return self._content["link"]
@@ -52,11 +55,27 @@ class UforaNotification:
def _clean_content(self, text: str):
html_table = {
+ # CHARACTERS:
"&": '&',
""": '"',
"apos;": "'",
">": ">",
- "<": "<"
+ "<": "<",
+ # MARKDOWN SUPPORT:
+ "": "**",
+ "": "**",
+ "": "**",
+ "": "**",
+ "": "*",
+ "": "*",
+ "": "*",
+ "": "*",
+ "": "~~",
+ "": "~~",
+ "": "__",
+ "": "__",
+ # Represent paragraphs with newlines
+ "
": "\n"
}
# Unescape HTML
diff --git a/functions/ufora_notifications.py b/functions/ufora_notifications.py
index 11b9f53..ecb8a1c 100644
--- a/functions/ufora_notifications.py
+++ b/functions/ufora_notifications.py
@@ -41,9 +41,10 @@ def run():
if feed:
for item in feed:
- notifications[course].append(item["id"])
+ notification = UforaNotification(item, course)
+ new_notifications.append(notification)
- new_notifications.append(UforaNotification(item, course))
+ notifications[course].append(notification.get_id())
# Update list of notifications
if new_notifications:
From 1372f0a9968d35cceea4257f83f17a93328ce8d9 Mon Sep 17 00:00:00 2001
From: Stijn De Clercq
Date: Wed, 3 Mar 2021 18:16:23 +0100
Subject: [PATCH 07/11] Fix typo in urls
---
data/embeds/ufora.py | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/data/embeds/ufora.py b/data/embeds/ufora.py
index c02cbff..df52c83 100644
--- a/data/embeds/ufora.py
+++ b/data/embeds/ufora.py
@@ -33,7 +33,7 @@ class UforaNotification:
if self._notif_id is None or self._course_id is None:
return self._content["link"]
- return "https://ufora.ugent.be/d2l/le/news/{0}/{1}/view?ou={0}".format(self._notif_id, self._course_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)
@@ -54,6 +54,7 @@ class UforaNotification:
return desc
def _clean_content(self, text: str):
+ # Dict with HTML & markdown tags to replace
html_table = {
# CHARACTERS:
"&": '&',
From db0e7b8f9b9d3212f307ee2234bf9ef05200315c Mon Sep 17 00:00:00 2001
From: Stijn De Clercq
Date: Wed, 3 Mar 2021 18:37:09 +0100
Subject: [PATCH 08/11] Add support for linebreaks, parse id's beforehand to
check properly, add leading zeroes to timestamp
---
data/embeds/ufora.py | 24 +++++++++---------------
functions/ufora_notifications.py | 21 +++++++++++++++++----
2 files changed, 26 insertions(+), 19 deletions(-)
diff --git a/data/embeds/ufora.py b/data/embeds/ufora.py
index df52c83..3a38c46 100644
--- a/data/embeds/ufora.py
+++ b/data/embeds/ufora.py
@@ -1,15 +1,16 @@
from datetime import datetime, timedelta
from discord import Embed, Colour
+from functions.stringFormatters import leadingZero as lz
from functions.timeFormatters import intToWeekday
import pytz
import re
class UforaNotification:
- def __init__(self, content: dict, course):
+ def __init__(self, content: dict, course, notif_id, course_id):
self._content: dict = content
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._title = self._clean_content(self._content["title"])
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)
- 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):
desc = self._clean_content(self._content["summary"])
@@ -76,7 +67,10 @@ class UforaNotification:
"": "__",
"": "__",
# Represent paragraphs with newlines
- "": "\n"
+ "": "\n",
+ "
": "\n",
+ "
": "\n",
+ "
": "\n"
}
# Unescape HTML
@@ -96,6 +90,6 @@ class UforaNotification:
return "{} {}/{}/{} om {}:{}:{}".format(
intToWeekday(dt.weekday()),
- dt.day, dt.month, dt.year,
- dt.hour, dt.minute, dt.second
+ lz(dt.day), lz(dt.month), lz(dt.year),
+ lz(dt.hour), lz(dt.minute), lz(dt.second)
)
diff --git a/functions/ufora_notifications.py b/functions/ufora_notifications.py
index ecb8a1c..26a5419 100644
--- a/functions/ufora_notifications.py
+++ b/functions/ufora_notifications.py
@@ -1,3 +1,5 @@
+import re
+
import feedparser
from data.embeds import UforaNotification
import json
@@ -37,14 +39,14 @@ def run():
feed = feedparser.parse(url)
# 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:
for item in feed:
- notification = UforaNotification(item, course)
- new_notifications.append(notification)
+ notif_id, course_id = _parse_ids(item["id"])
+ 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
if new_notifications:
@@ -52,3 +54,14 @@ def run():
json.dump(notifications, fp)
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]
From 981f7a1457e433151e34f1958a7c7d9453e02fd0 Mon Sep 17 00:00:00 2001
From: Stijn De Clercq
Date: Wed, 3 Mar 2021 18:40:31 +0100
Subject: [PATCH 09/11] Change announcements channel
---
cogs/tasks.py | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/cogs/tasks.py b/cogs/tasks.py
index 14c84ff..ed9c32b 100644
--- a/cogs/tasks.py
+++ b/cogs/tasks.py
@@ -249,8 +249,7 @@ class Tasks(commands.Cog):
announcements = ufora_notifications.run()
if announcements:
- # TODO change to COC Bot testing if it works
- announcements_channel = self.client.get_channel(int(constants.ZandbakSpeeltuin))
+ announcements_channel = self.client.get_channel(816724500136591380)
for an in announcements:
await announcements_channel.send(embed=an.to_embed())
From 2c634912ef2d8177b748a1930608eaf33fee407c Mon Sep 17 00:00:00 2001
From: Stijn De Clercq
Date: Wed, 3 Mar 2021 18:44:53 +0100
Subject: [PATCH 10/11] Add three dots when truncating
---
data/embeds/ufora.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/data/embeds/ufora.py b/data/embeds/ufora.py
index 3a38c46..475110b 100644
--- a/data/embeds/ufora.py
+++ b/data/embeds/ufora.py
@@ -40,7 +40,7 @@ class UforaNotification:
desc = self._clean_content(self._content["summary"])
if len(desc) > 500:
- return desc[:500]
+ return desc[:497] + "..."
return desc
From 6c0f9fd017762372dae29a5351a791f6de2e4dbf Mon Sep 17 00:00:00 2001
From: Stijn De Clercq
Date: Wed, 3 Mar 2021 18:45:36 +0100
Subject: [PATCH 11/11] Run every 30 min instead of every 2
---
cogs/tasks.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/cogs/tasks.py b/cogs/tasks.py
index ed9c32b..b44bcac 100644
--- a/cogs/tasks.py
+++ b/cogs/tasks.py
@@ -240,7 +240,7 @@ class Tasks(commands.Cog):
async def beforeUpdateMatchweek(self):
await self.client.wait_until_ready()
- @tasks.loop(minutes=2)
+ @tasks.loop(minutes=30.0)
async def uforaAnnouncements(self):
"""
Task that checks for new Ufora announcements every few minutes