Skip to content
Snippets Groups Projects
Commit 15e0002e authored by dnns01's avatar dnns01
Browse files

fix $11w - Added functionality to check for news

parent d1de0913
No related branches found
No related tags found
No related merge requests found
......@@ -119,3 +119,4 @@ GitHub.sublime-settings
/appointments.json
/tops.json
/text_commands.json
/news.json
......@@ -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):
......
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()
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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment