from event import Event import os from nio import AsyncClient from textwrap import dedent from markdown2 import Markdown import asyncio import datetime from scraping import get_soup, process_soup, get_events_today MATRIX_URL=os.environ["MATRIX_URL"] MATRIX_USER=os.environ["MATRIX_USER"] MATRIX_TOKEN=os.environ["MATRIX_TOKEN"] ANNOUNCEMENT_CHANNEL=os.environ["ANNOUNCEMENT_CHANNEL"] client = None 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 / Event of the Day: "{name}" - 🕒 **{start}** - 📍 **{location}** {description} [Les mer / Read More]({url}) '''.format(name=event.name, start=event.start.strftime('%H:%M'), location=event.location, description=event.description, url=url) msgText = dedent(msgText) if atEveryone: msgText = msgText + '\n@room' 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 s = get_soup() es = get_events_today(process_soup(s)) for e in es: await sendMatrixAnnouncement(e, ANNOUNCEMENT_CHANNEL, False) await client.close() asyncio.run(main())