From 15e0002ee38eaa0d20105747989f24b4fbc15aaf Mon Sep 17 00:00:00 2001
From: dnns01 <mail@dnns01.de>
Date: Mon, 7 Dec 2020 00:19:30 +0100
Subject: [PATCH] fix $11w - Added functionality to check for news

---
 .gitignore       |  1 +
 fernuni_bot.py   |  5 ++---
 news_cog.py      | 50 ++++++++++++++++++++++++++++++++++++++++++++++++
 requirements.txt |  3 ++-
 4 files changed, 55 insertions(+), 4 deletions(-)
 create mode 100644 news_cog.py

diff --git a/.gitignore b/.gitignore
index 07b48cc..bc8ec9a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -119,3 +119,4 @@ GitHub.sublime-settings
 /appointments.json
 /tops.json
 /text_commands.json
+/news.json
diff --git a/fernuni_bot.py b/fernuni_bot.py
index bf58ac5..8d8fc22 100644
--- a/fernuni_bot.py
+++ b/fernuni_bot.py
@@ -9,6 +9,7 @@ from dotenv import load_dotenv
 import utils
 from appointments_cog import AppointmentsCog
 from christmas_cog import ChristmasCog
+from news_cog import NewsCog
 from poll_cog import PollCog
 from roles_cog import RolesCog
 from support_cog import SupportCog
@@ -45,9 +46,7 @@ bot.add_cog(roles_cog)
 bot.add_cog(welcome_cog)
 bot.add_cog(christmas_cog)
 bot.add_cog(SupportCog(bot))
-
-
-# bot.add_cog(welcome_cog)
+bot.add_cog(NewsCog(bot))
 
 
 def get_reaction(reactions):
diff --git a/news_cog.py b/news_cog.py
new file mode 100644
index 0000000..2b4bdc9
--- /dev/null
+++ b/news_cog.py
@@ -0,0 +1,50 @@
+import json
+import os
+
+import requests
+from bs4 import BeautifulSoup
+from discord.ext import commands, tasks
+
+
+class NewsCog(commands.Cog):
+    def __init__(self, bot):
+        self.bot = bot
+        self.channel_id = int(os.getenv("DISCORD_NEWS_CHANNEL"))
+        self.url = "https://www.fernuni-hagen.de/mi/studium/aktuelles/index.shtml"
+        self.news = {}
+        self.load_news()
+        self.news_loop.start()
+
+    def load_news(self):
+        news_file = open("news.json", mode="r")
+        self.news = json.load(news_file)
+
+    def save_news(self):
+        news_file = open("news.json", mode="w")
+        json.dump(self.news, news_file)
+
+    @tasks.loop(minutes=1)
+    async def news_loop(self):
+        req = requests.get(self.url)
+        soup = BeautifulSoup(req.content, "html.parser")
+        channel = await self.bot.fetch_channel(self.channel_id)
+
+        for news in soup.find("ul", attrs={"class": "fu-link-list"}).find_all("li"):
+            date = news.span.text
+            title = str(news.a.text)
+            link = news.a['href']
+
+            if link[0] == "/":
+                link = f"https://www.fernuni-hagen.de" + link
+
+            if not self.news.get(link):
+                self.news[link] = date
+                await channel.send(f":loudspeaker: Neues aus der Fakultät vom {date} :loudspeaker: \n{title} \n{link}")
+            else:
+                prev_date = self.news[link]
+                if date != prev_date:
+                    self.news[link] = date
+                    await channel.send(
+                        f":loudspeaker: Neues aus der Fakultät vom {date} :loudspeaker: \n{title} \n{link}")
+
+        self.save_news()
diff --git a/requirements.txt b/requirements.txt
index 391d1de..ed1120b 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,3 +1,4 @@
+requests
 aiohttp==3.7.3
 async-timeout==3.0.1
 attrs==20.3.0
@@ -5,7 +6,7 @@ chardet==3.0.4
 discord==1.0.1
 discord.py==1.5.1
 idna==2.10
-multidict==5.0.2
+multidict==5.1.0
 python-dotenv==0.15.0
 typing-extensions==3.7.4.3
 websockets==8.1
-- 
GitLab