nixify and fixify
This commit is contained in:
65
src/pvv_calendar_bot/main.py
Normal file
65
src/pvv_calendar_bot/main.py
Normal file
@@ -0,0 +1,65 @@
|
||||
#! /usr/bin/env python
|
||||
|
||||
from .event import Event
|
||||
from .scraping import get_soup, process_soup, get_events_today
|
||||
|
||||
import os
|
||||
from nio import AsyncClient
|
||||
from textwrap import dedent
|
||||
from markdown2 import Markdown
|
||||
import asyncio
|
||||
import datetime
|
||||
|
||||
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 sendCalendarEvents() -> None:
|
||||
global client
|
||||
client = AsyncClient(MATRIX_URL, MATRIX_USER)
|
||||
client.access_token = MATRIX_TOKEN
|
||||
|
||||
scrapeData = get_soup()
|
||||
eventsToday = get_events_today(process_soup(scrapeData))
|
||||
|
||||
for event in eventsToday:
|
||||
await sendMatrixAnnouncement(event, ANNOUNCEMENT_CHANNEL, False)
|
||||
|
||||
await client.close()
|
||||
|
||||
def main():
|
||||
asyncio.run(sendCalendarEvents())
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user