Endring av fix triks
This commit is contained in:
parent
71e136da8c
commit
383e1bbf79
|
@ -1,4 +1,4 @@
|
||||||
beautifulsoup4
|
beautifulsoup4
|
||||||
markdown2
|
markdown2
|
||||||
nio
|
matrix-nio
|
||||||
requests
|
requests
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#! /usr/bin/env python
|
#! /usr/bin/env python
|
||||||
|
|
||||||
from .event import Event
|
# from .event import Event
|
||||||
from .scraping import get_soup, process_soup, get_events_today
|
# from .scraping import get_soup, process_soup, get_events_today
|
||||||
|
|
||||||
import os
|
import os
|
||||||
from nio import AsyncClient
|
from nio import AsyncClient
|
||||||
|
@ -9,17 +9,19 @@ from textwrap import dedent
|
||||||
from markdown2 import Markdown
|
from markdown2 import Markdown
|
||||||
import asyncio
|
import asyncio
|
||||||
import datetime
|
import datetime
|
||||||
|
from sql_connector import fetch_events, Event
|
||||||
|
|
||||||
MATRIX_URL=os.environ["MATRIX_URL"]
|
MATRIX_URL=os.environ.get("MATRIX_URL","https://matrix.pvv.ntnu.no")
|
||||||
MATRIX_USER=os.environ["MATRIX_USER"]
|
MATRIX_USER=os.environ.get("MATRIX_USER","@bot_calendar:pvv.ntnu.no")
|
||||||
MATRIX_TOKEN=os.environ["MATRIX_TOKEN"]
|
MATRIX_TOKEN=os.environ.get("MATRIX_TOKEN","test")
|
||||||
ANNOUNCEMENT_CHANNEL=os.environ["ANNOUNCEMENT_CHANNEL"]
|
ANNOUNCEMENT_CHANNEL=os.environ.get("ANNOUNCEMENT_CHANNEL","!announcements:pvv.ntnu.no")
|
||||||
|
|
||||||
client = None
|
client = None
|
||||||
|
|
||||||
async def sendMatrixAnnouncement(event: Event, channel: str = ANNOUNCEMENT_CHANNEL, atEveryone: bool = False) -> None:
|
|
||||||
|
def create_announcement(event,atEveryone):
|
||||||
url = "https://www.pvv.ntnu.no/hendelser/info.php?id={}".format(event.id)
|
url = "https://www.pvv.ntnu.no/hendelser/info.php?id={}".format(event.id)
|
||||||
msgText = '''\
|
msgText = dedent('''\
|
||||||
## Dagens arrangement / Event of the Day: "{name}"
|
## Dagens arrangement / Event of the Day: "{name}"
|
||||||
- 🕒 **{start}**
|
- 🕒 **{start}**
|
||||||
- 📍 **{location}**
|
- 📍 **{location}**
|
||||||
|
@ -27,36 +29,46 @@ async def sendMatrixAnnouncement(event: Event, channel: str = ANNOUNCEMENT_CHANN
|
||||||
{description}
|
{description}
|
||||||
|
|
||||||
[Les mer / Read More]({url})
|
[Les mer / Read More]({url})
|
||||||
'''.format(name=event.name, start=event.start.strftime('%H:%M'), location=event.location, description=event.description, url=url)
|
''').format(
|
||||||
|
name=event.name,
|
||||||
|
start=event.start.strftime('%H:%M'),
|
||||||
|
location=event.location,
|
||||||
|
description=event.description,
|
||||||
|
url=url,
|
||||||
|
)
|
||||||
|
|
||||||
msgText = dedent(msgText)
|
# msgText = dedent(msgText)
|
||||||
|
|
||||||
if atEveryone:
|
if atEveryone:
|
||||||
msgText = msgText + '\n@room'
|
msgText = msgText + '\n@room'
|
||||||
|
return msgText
|
||||||
|
|
||||||
return await client.room_send(
|
|
||||||
room_id=channel,
|
async def sendMatrixAnnouncement(event: Event, channel: str = ANNOUNCEMENT_CHANNEL, atEveryone: bool = False) -> None:
|
||||||
message_type="m.room.message",
|
msgText = create_announcement(event,atEveryone)
|
||||||
content={
|
print(msgText)
|
||||||
"msgtype": "m.text",
|
# return await client.room_send(
|
||||||
"body": msgText,
|
# room_id=channel,
|
||||||
"format": "org.matrix.custom.html",
|
# message_type="m.room.message",
|
||||||
"formatted_body": Markdown().convert(msgText)
|
# content={
|
||||||
}
|
# "msgtype": "m.text",
|
||||||
)
|
# "body": msgText,
|
||||||
|
# "format": "org.matrix.custom.html",
|
||||||
|
# "formatted_body": Markdown().convert(msgText)
|
||||||
|
# }
|
||||||
|
# )
|
||||||
|
|
||||||
async def sendCalendarEvents() -> None:
|
async def sendCalendarEvents() -> None:
|
||||||
global client
|
# global client
|
||||||
client = AsyncClient(MATRIX_URL, MATRIX_USER)
|
# client = AsyncClient(MATRIX_URL, MATRIX_USER)
|
||||||
client.access_token = MATRIX_TOKEN
|
# client.access_token = MATRIX_TOKEN
|
||||||
|
|
||||||
scrapeData = get_soup()
|
eventsToday = fetch_events()
|
||||||
eventsToday = get_events_today(process_soup(scrapeData))
|
|
||||||
|
|
||||||
for event in eventsToday:
|
for event in eventsToday:
|
||||||
await sendMatrixAnnouncement(event, ANNOUNCEMENT_CHANNEL, False)
|
await sendMatrixAnnouncement(event, ANNOUNCEMENT_CHANNEL, False)
|
||||||
|
|
||||||
await client.close()
|
# await client.close()
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
asyncio.run(sendCalendarEvents())
|
asyncio.run(sendCalendarEvents())
|
||||||
|
|
|
@ -0,0 +1,54 @@
|
||||||
|
from dataclasses import dataclass
|
||||||
|
import mysql.connector
|
||||||
|
from pprint import pprint
|
||||||
|
import os
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Event:
|
||||||
|
"""
|
||||||
|
An Event with id,name,start,stop,location,description,organiser
|
||||||
|
"""
|
||||||
|
id: int
|
||||||
|
name: str
|
||||||
|
start: datetime
|
||||||
|
stop: datetime
|
||||||
|
location: str
|
||||||
|
description: str
|
||||||
|
organiser: str
|
||||||
|
|
||||||
|
|
||||||
|
def fetch_events():
|
||||||
|
mydb = mysql.connector.connect(
|
||||||
|
host = os.environ.get("MYSQL_URL","mysql.pvv.ntnu.no"),
|
||||||
|
user = os.environ.get("MYSQL_USER","calendar-bot"),
|
||||||
|
password = os.environ["MYSQL_PASSWORD"], #lmao
|
||||||
|
database = "www-data_nettside"
|
||||||
|
)
|
||||||
|
|
||||||
|
mycursor = mydb.cursor()
|
||||||
|
mycursor.execute("""
|
||||||
|
SELECT id,name,start,stop,location,description,organiser
|
||||||
|
FROM events
|
||||||
|
WHERE DATE(start) = CURDATE()
|
||||||
|
""")
|
||||||
|
|
||||||
|
db_result = mycursor.fetchall()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
results = []
|
||||||
|
for row in db_result:
|
||||||
|
results.append(Event(id=row[0],
|
||||||
|
name=row[1],
|
||||||
|
start=datetime.fromisoformat(row[2]),
|
||||||
|
stop=datetime.fromisoformat(row[3]),
|
||||||
|
location=row[4],
|
||||||
|
description=row[5],
|
||||||
|
organiser=row[6],
|
||||||
|
))
|
||||||
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
pprint(fetch_events())
|
Loading…
Reference in New Issue