diff --git a/extensions/sprachrohr.py b/extensions/sprachrohr.py new file mode 100644 index 0000000000000000000000000000000000000000..0419e5895fb9141c4aa044b37115c6169c1ec8e3 --- /dev/null +++ b/extensions/sprachrohr.py @@ -0,0 +1,42 @@ +from aiohttp import ClientSession +from bs4 import BeautifulSoup +from discord.ext import commands, tasks +import models + + +class Sprachrohr(commands.Cog): + def __init__(self, bot): + self.bot = bot + self.config = bot.config["extensions"][__name__.split(".")[-1]] + self.url = self.config.get("url") + + self.update_loop.start() + + @tasks.loop(hours=3) + async def update_loop(self): + async with ClientSession() as session: + async with session.get(self.url) as r: + if r.status == 200: + content = await r.read() + soup = BeautifulSoup(content, "html.parser") + + for issue in soup.find("ul", attrs={"class": "wp-block-latest-posts__list"}).find_all("li"): + link = issue.find("a") + url = link["href"] + year = link.text[-4:] + issue_in_year = link.text[len("SprachRohr "):-5] + + (sprach_rohr, created) = models.SprachRohr.get_or_create(url=url, year=year, + issue=issue_in_year) + if created: + await self.announce_new_issue(sprach_rohr) + + async def announce_new_issue(self, sprach_rohr: models.SprachRohr): + if channel := await self.bot.fetch_channel(self.config.get("channel_id")): + msg = await channel.send(f"{self.config.get('text')}\n{sprach_rohr.url}") + if channel.is_news(): + await msg.publish() + + +async def setup(bot: commands.Bot) -> None: + await bot.add_cog(Sprachrohr(bot)) diff --git a/models.py b/models.py index a10b81d0baa6d41245dfcb9bc830f2672a054a96..732b816ae716557a467677fd97fae84fc784469a 100644 --- a/models.py +++ b/models.py @@ -205,6 +205,11 @@ class CommandText(BaseModel): text = CharField() command = ForeignKeyField(Command, backref="texts") +class SprachRohr(BaseModel): + url = CharField() + year = IntegerField() + issue = CharField() + db.create_tables([Poll, PollChoice, PollChoiceChosen, Appointment, Attendee, Topic, Link, Timer, TimerAttendee, Command, - CommandText], safe=True) + CommandText, SprachRohr], safe=True) diff --git a/requirements.txt b/requirements.txt index 273dd1150088fe5f5e8f31815d9fdd33397a9e3c..531c9fc9bfbe59537cc9090ce94ec9a071bef7df 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,18 +1,17 @@ -aiohttp==3.8.3 +aiohttp==3.8.4 aiosignal==1.3.1 async-timeout==4.0.2 -attrs==22.2.0 -certifi==2022.12.7 +attrs==23.1.0 +beautifulsoup4==4.12.2 cffi==1.15.1 -charset-normalizer==2.1.1 -discord.py==2.1.0 -emoji==2.0.0 +charset-normalizer==3.1.0 +discord.py==2.3.1 +emoji==2.6.0 frozenlist==1.3.3 idna==3.4 -multidict==6.0.3 -peewee==3.15.2 +multidict==6.0.4 +peewee==3.16.2 pycparser==2.21 PyNaCl==1.5.0 -requests==2.28.1 -urllib3==1.26.13 -yarl==1.8.2 +soupsieve==2.4.1 +yarl==1.9.2