Endring av fix triks
This commit is contained in:
parent
71e136da8c
commit
383e1bbf79
|
@ -1,4 +1,4 @@
|
|||
beautifulsoup4
|
||||
markdown2
|
||||
nio
|
||||
matrix-nio
|
||||
requests
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#! /usr/bin/env python
|
||||
|
||||
from .event import Event
|
||||
from .scraping import get_soup, process_soup, get_events_today
|
||||
# from .event import Event
|
||||
# from .scraping import get_soup, process_soup, get_events_today
|
||||
|
||||
import os
|
||||
from nio import AsyncClient
|
||||
|
@ -9,17 +9,19 @@ from textwrap import dedent
|
|||
from markdown2 import Markdown
|
||||
import asyncio
|
||||
import datetime
|
||||
from sql_connector import fetch_events, Event
|
||||
|
||||
MATRIX_URL=os.environ["MATRIX_URL"]
|
||||
MATRIX_USER=os.environ["MATRIX_USER"]
|
||||
MATRIX_TOKEN=os.environ["MATRIX_TOKEN"]
|
||||
ANNOUNCEMENT_CHANNEL=os.environ["ANNOUNCEMENT_CHANNEL"]
|
||||
MATRIX_URL=os.environ.get("MATRIX_URL","https://matrix.pvv.ntnu.no")
|
||||
MATRIX_USER=os.environ.get("MATRIX_USER","@bot_calendar:pvv.ntnu.no")
|
||||
MATRIX_TOKEN=os.environ.get("MATRIX_TOKEN","test")
|
||||
ANNOUNCEMENT_CHANNEL=os.environ.get("ANNOUNCEMENT_CHANNEL","!announcements:pvv.ntnu.no")
|
||||
|
||||
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)
|
||||
msgText = '''\
|
||||
msgText = dedent('''\
|
||||
## Dagens arrangement / Event of the Day: "{name}"
|
||||
- 🕒 **{start}**
|
||||
- 📍 **{location}**
|
||||
|
@ -27,36 +29,46 @@ async def sendMatrixAnnouncement(event: Event, channel: str = ANNOUNCEMENT_CHANN
|
|||
{description}
|
||||
|
||||
[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:
|
||||
msgText = msgText + '\n@room'
|
||||
return msgText
|
||||
|
||||
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 sendMatrixAnnouncement(event: Event, channel: str = ANNOUNCEMENT_CHANNEL, atEveryone: bool = False) -> None:
|
||||
msgText = create_announcement(event,atEveryone)
|
||||
print(msgText)
|
||||
# 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
|
||||
# global client
|
||||
# client = AsyncClient(MATRIX_URL, MATRIX_USER)
|
||||
# client.access_token = MATRIX_TOKEN
|
||||
|
||||
scrapeData = get_soup()
|
||||
eventsToday = get_events_today(process_soup(scrapeData))
|
||||
eventsToday = fetch_events()
|
||||
|
||||
for event in eventsToday:
|
||||
await sendMatrixAnnouncement(event, ANNOUNCEMENT_CHANNEL, False)
|
||||
|
||||
await client.close()
|
||||
# await client.close()
|
||||
|
||||
def main():
|
||||
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