From 0109645b0f32d08211ba30ad9bb2482ee6c033d9 Mon Sep 17 00:00:00 2001 From: Felix Albrigtsen Date: Fri, 18 Aug 2023 00:33:33 +0200 Subject: [PATCH] Replace simplematrixbotlib with nio, remove end time --- event.py | 6 +++--- requirements.txt | 3 ++- scraping.py | 2 +- sendMatrix.py | 54 +++++++++++++++++++++++++++++++++--------------- 4 files changed, 43 insertions(+), 22 deletions(-) diff --git a/event.py b/event.py index 5e1394c..88baff4 100644 --- a/event.py +++ b/event.py @@ -1,9 +1,9 @@ import datetime class Event: - def __init__(self, id: int, time: datetime.datetime, name: str, location: str, organizer: str, description: str): + def __init__(self, id: int, start: datetime.datetime, name: str, location: str, organizer: str, description: str): self.id = id - self.time = time + self.start = start self.name = name self.location = location self.organizer = organizer @@ -13,4 +13,4 @@ class Event: return f'{self.name} ({self.id})' def __repr__(self): - return f'Event({self.id}, {self.time}, {self.name})' + return f'Event({self.id}, {self.start}, {self.name})' diff --git a/requirements.txt b/requirements.txt index bbd7c24..50ca0d5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ -simplematrixbotlib beautifulsoup4 +markdown2 +nio requests diff --git a/scraping.py b/scraping.py index 73a0f4a..0f6154f 100644 --- a/scraping.py +++ b/scraping.py @@ -45,7 +45,7 @@ def process_soup(soup: BeautifulSoup) -> List[Event]: def get_events_today(events: List[Event]): - return list(filter(lambda e: e.time.date() == datetime.datetime.today().date(), events)) + return list(filter(lambda e: e.start.date() == datetime.datetime.today().date(), events)) if __name__ == "__main__": diff --git a/sendMatrix.py b/sendMatrix.py index fabd5b8..fccbdc5 100644 --- a/sendMatrix.py +++ b/sendMatrix.py @@ -1,36 +1,56 @@ from event import Event +import os +from nio import AsyncClient from textwrap import dedent -import simplematrixbotlib as botlib +from markdown2 import Markdown +import asyncio +import datetime -MATRIX_URL="https://matrix.pvv.ntnu.no" -MATRIX_USER="@calbot:pvv.ntnu.no" -MATRIX_PASS="foo" -ANNOUNCEMENT_CHANNEL = "#announcements:pvv.ntnu.no" +MATRIX_URL=os.environ["MATRIX_URL"] +MATRIX_USER=os.environ["MATRIX_USER"] +MATRIX_TOKEN=os.environ["MATRIX_TOKEN"] +ANNOUNCEMENT_CHANNEL=os.environ["ANNOUNCEMENT_CHANNEL"] -creds = botlib.Creds(MATRIX_URL, MATRIX_USER, MATRIX_PASS) -bot = botlib.Bot(creds) +client = None -async def sendAnnouncement(event: Event, channel: str = ANNOUNCEMENT_CHANNEL, atEveryone: bool = False) -> None: - url = f'https://www.pvv.ntnu.no/hendelser/info.php?id=${id}' +async def sendMatrixAnnouncement(event: Event, channel: str = ANNOUNCEMENT_CHANNEL, atEveryone: bool = False) -> None: + url = "https://www.pvv.ntnu.no/hendelser/info.php?id={}".format(event.id) msgText = '''\ - # Dagens arrangement / Todays event: {name} - ## 🕒 {start} - {end} - ## 📍 {location} + ## Dagens arrangement / Event of the Day: "{name}" + #### 🕒 {start} + #### 📍 {location} + #### 🛂 av / by "{organizer}" {description} [Les mer / Read More]({url}) - '''.format(name=event.name, start=event.start.strftime('%H:%M'), end=event.end.strftime('%H:%M'), location=event.location, description=event.description, url=url) + '''.format(name=event.name, start=event.start.strftime('%H:%M'), location=event.location, organizer=event.organizer, description=event.description, url=url) msgText = dedent(msgText) if atEveryone: msgText = msgText + '\n@room' - botResult = await bot.api.send_markdown_message( - room_id=channel, - message=msgText, - msgtype="m.text") + return await client.room_send( + room_id=channel, + message_type="m.room.message", + content={ + "msgtype": "m.text", + "body": msgText, + "format": "org.matrix.custom.html", + "formatted_body": Markdown().convert(msgText) + } + ) +async def main() -> None: + global client + client = AsyncClient(MATRIX_URL, MATRIX_USER) + client.access_token = MATRIX_TOKEN + + await sendMatrixAnnouncement(Event(1, datetime.datetime.now(), "Name", "Location", "PVV", "Description"), ANNOUNCEMENT_CHANNEL, False) + + await client.close() + +asyncio.run(main())