ruff format
This commit is contained in:
@@ -1,7 +1,16 @@
|
|||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
|
|
||||||
class Event:
|
class Event:
|
||||||
def __init__(self, id: int, start: 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.id = id
|
||||||
self.start = start
|
self.start = start
|
||||||
self.name = name
|
self.name = name
|
||||||
@@ -10,7 +19,7 @@ class Event:
|
|||||||
self.description = description
|
self.description = description
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f'{self.name} ({self.id})'
|
return f"{self.name} ({self.id})"
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f'Event({self.id}, {self.start}, {self.name})'
|
return f"Event({self.id}, {self.start}, {self.name})"
|
||||||
|
|||||||
@@ -10,16 +10,19 @@ from markdown2 import Markdown
|
|||||||
import asyncio
|
import asyncio
|
||||||
from .sql_connector import fetch_events, Event
|
from .sql_connector import fetch_events, Event
|
||||||
|
|
||||||
MATRIX_URL=os.environ.get("MATRIX_URL","https://matrix.pvv.ntnu.no").strip()
|
MATRIX_URL = os.environ.get("MATRIX_URL", "https://matrix.pvv.ntnu.no").strip()
|
||||||
MATRIX_USER=os.environ.get("MATRIX_USER","@bot_calendar:pvv.ntnu.no").strip()
|
MATRIX_USER = os.environ.get("MATRIX_USER", "@bot_calendar:pvv.ntnu.no").strip()
|
||||||
MATRIX_TOKEN=os.environ.get("MATRIX_TOKEN").strip()
|
MATRIX_TOKEN = os.environ.get("MATRIX_TOKEN").strip()
|
||||||
ANNOUNCEMENT_CHANNEL=os.environ.get("ANNOUNCEMENT_CHANNEL", "!announcements:pvv.ntnu.no").strip()
|
ANNOUNCEMENT_CHANNEL = os.environ.get(
|
||||||
|
"ANNOUNCEMENT_CHANNEL", "!announcements:pvv.ntnu.no"
|
||||||
|
).strip()
|
||||||
|
|
||||||
client = None
|
client = None
|
||||||
|
|
||||||
|
|
||||||
def create_announcement(event: Event, atEveryone: bool) -> str:
|
def create_announcement(event: Event, atEveryone: bool) -> str:
|
||||||
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 = dedent('''\
|
msgText = dedent("""\
|
||||||
## Dagens arrangement / Event of the Day: "{name}"
|
## Dagens arrangement / Event of the Day: "{name}"
|
||||||
- 🕒 **{start}**
|
- 🕒 **{start}**
|
||||||
- 📍 **{location}**
|
- 📍 **{location}**
|
||||||
@@ -27,26 +30,24 @@ def create_announcement(event: Event, atEveryone: bool) -> str:
|
|||||||
{description}
|
{description}
|
||||||
|
|
||||||
[Les mer / Read More]({url})
|
[Les mer / Read More]({url})
|
||||||
''').format(
|
""").format(
|
||||||
name=event.name,
|
name=event.name,
|
||||||
start=event.start.strftime('%H:%M'),
|
start=event.start.strftime("%H:%M"),
|
||||||
location=event.location,
|
location=event.location,
|
||||||
description=event.description,
|
description=event.description,
|
||||||
url=url,
|
url=url,
|
||||||
)
|
)
|
||||||
|
|
||||||
if atEveryone:
|
if atEveryone:
|
||||||
msgText = msgText + '\n@room'
|
msgText = msgText + "\n@room"
|
||||||
|
|
||||||
return msgText
|
return msgText
|
||||||
|
|
||||||
|
|
||||||
async def sendMatrixAnnouncement(
|
async def sendMatrixAnnouncement(
|
||||||
event: Event,
|
event: Event, channel: str = ANNOUNCEMENT_CHANNEL, atEveryone: bool = False
|
||||||
channel: str = ANNOUNCEMENT_CHANNEL,
|
|
||||||
atEveryone: bool = False
|
|
||||||
) -> None:
|
) -> None:
|
||||||
msgText = create_announcement(event,atEveryone)
|
msgText = create_announcement(event, atEveryone)
|
||||||
return await client.room_send(
|
return await client.room_send(
|
||||||
room_id=channel,
|
room_id=channel,
|
||||||
message_type="m.room.message",
|
message_type="m.room.message",
|
||||||
@@ -55,9 +56,10 @@ async def sendMatrixAnnouncement(
|
|||||||
"body": msgText,
|
"body": msgText,
|
||||||
"format": "org.matrix.custom.html",
|
"format": "org.matrix.custom.html",
|
||||||
"formatted_body": Markdown().convert(msgText),
|
"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)
|
||||||
@@ -70,8 +72,10 @@ async def sendCalendarEvents() -> None:
|
|||||||
|
|
||||||
await client.close()
|
await client.close()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
asyncio.run(sendCalendarEvents())
|
asyncio.run(sendCalendarEvents())
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ from operator import add
|
|||||||
from functools import reduce
|
from functools import reduce
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
|
|
||||||
def get_soup() -> BeautifulSoup:
|
def get_soup() -> BeautifulSoup:
|
||||||
r = requests.get("http://www.pvv.ntnu.no/hendelser/")
|
r = requests.get("http://www.pvv.ntnu.no/hendelser/")
|
||||||
soup = BeautifulSoup(r.text, "html.parser")
|
soup = BeautifulSoup(r.text, "html.parser")
|
||||||
@@ -18,24 +19,49 @@ def process_soup(soup: BeautifulSoup) -> List[Event]:
|
|||||||
events = soup.find_all("ul", "events")
|
events = soup.find_all("ul", "events")
|
||||||
|
|
||||||
for event in events:
|
for event in events:
|
||||||
times, locations, organizers = zip(*(list(
|
times, locations, organizers = zip(
|
||||||
map(lambda x: map(lambda y: y.find("strong").text, x),
|
*(
|
||||||
filter(lambda x: x != [],
|
list(
|
||||||
map(lambda x: x.find_all("li"),
|
map(
|
||||||
event.find_all("li")))))))
|
lambda x: map(lambda y: y.find("strong").text, x),
|
||||||
times = list(map(lambda x: datetime.datetime.strptime(x, "%A %d. %b %H.%M").replace(year=datetime.datetime.now().year), times))
|
filter(
|
||||||
names = list(map(lambda x: x[0].text,
|
lambda x: x != [],
|
||||||
filter(lambda x: x != [],
|
map(lambda x: x.find_all("li"), event.find_all("li")),
|
||||||
map(lambda x: x.find_all("a"),
|
),
|
||||||
event.find_all("li")))))
|
)
|
||||||
descriptions = list(map(lambda x: x.text,
|
)
|
||||||
filter(lambda x: x is not None,
|
)
|
||||||
map(lambda x: x.find("p"),
|
)
|
||||||
event.find_all("li")))))
|
times = list(
|
||||||
|
map(
|
||||||
|
lambda x: datetime.datetime.strptime(x, "%A %d. %b %H.%M").replace(
|
||||||
|
year=datetime.datetime.now().year
|
||||||
|
),
|
||||||
|
times,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
names = list(
|
||||||
|
map(
|
||||||
|
lambda x: x[0].text,
|
||||||
|
filter(
|
||||||
|
lambda x: x != [],
|
||||||
|
map(lambda x: x.find_all("a"), event.find_all("li")),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
descriptions = list(
|
||||||
|
map(
|
||||||
|
lambda x: x.text,
|
||||||
|
filter(
|
||||||
|
lambda x: x is not None,
|
||||||
|
map(lambda x: x.find("p"), event.find_all("li")),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
)
|
||||||
ids = []
|
ids = []
|
||||||
for a in event.find_all("a", href=True):
|
for a in event.find_all("a", href=True):
|
||||||
if a['href'][:10] == '/hendelser':
|
if a["href"][:10] == "/hendelser":
|
||||||
ids.append(int(a['href'][a['href'].find("=")+1:]))
|
ids.append(int(a["href"][a["href"].find("=") + 1 :]))
|
||||||
|
|
||||||
zips.append(list(zip(ids, times, names, organizers, locations, descriptions)))
|
zips.append(list(zip(ids, times, names, organizers, locations, descriptions)))
|
||||||
|
|
||||||
@@ -45,12 +71,10 @@ def process_soup(soup: BeautifulSoup) -> List[Event]:
|
|||||||
|
|
||||||
|
|
||||||
def get_events_today(events: List[Event]) -> List[Event]:
|
def get_events_today(events: List[Event]) -> List[Event]:
|
||||||
return list(filter(lambda e: e.start.date() == datetime.datetime.today().date(), events))
|
return list(
|
||||||
|
filter(lambda e: e.start.date() == datetime.datetime.today().date(), events)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print(get_events_today(process_soup(get_soup())))
|
print(get_events_today(process_soup(get_soup())))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -4,11 +4,13 @@ from pprint import pprint
|
|||||||
import os
|
import os
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Event:
|
class Event:
|
||||||
"""
|
"""
|
||||||
An Event with id,name,start,stop,location,description,organiser
|
An Event with id,name,start,stop,location,description,organiser
|
||||||
"""
|
"""
|
||||||
|
|
||||||
id: int
|
id: int
|
||||||
name: str
|
name: str
|
||||||
start: datetime
|
start: datetime
|
||||||
@@ -20,10 +22,10 @@ class Event:
|
|||||||
|
|
||||||
def fetch_events():
|
def fetch_events():
|
||||||
mydb = mysql.connector.connect(
|
mydb = mysql.connector.connect(
|
||||||
host = os.environ.get("MYSQL_URL","mysql.pvv.ntnu.no").strip(),
|
host=os.environ.get("MYSQL_URL", "mysql.pvv.ntnu.no").strip(),
|
||||||
user = os.environ.get("MYSQL_USER","calendar-bot").strip(),
|
user=os.environ.get("MYSQL_USER", "calendar-bot").strip(),
|
||||||
password = os.environ["MYSQL_PASSWORD"].strip(),
|
password=os.environ["MYSQL_PASSWORD"].strip(),
|
||||||
database = "www-data_nettside",
|
database="www-data_nettside",
|
||||||
)
|
)
|
||||||
|
|
||||||
mycursor = mydb.cursor()
|
mycursor = mydb.cursor()
|
||||||
@@ -37,15 +39,17 @@ def fetch_events():
|
|||||||
|
|
||||||
results = []
|
results = []
|
||||||
for row in db_result:
|
for row in db_result:
|
||||||
results.append(Event(
|
results.append(
|
||||||
id = row[0],
|
Event(
|
||||||
name = row[1],
|
id=row[0],
|
||||||
start = datetime.fromisoformat(row[2]),
|
name=row[1],
|
||||||
stop = datetime.fromisoformat(row[3]),
|
start=datetime.fromisoformat(row[2]),
|
||||||
location = row[4],
|
stop=datetime.fromisoformat(row[3]),
|
||||||
description = row[5],
|
location=row[4],
|
||||||
organiser = row[6],
|
description=row[5],
|
||||||
))
|
organiser=row[6],
|
||||||
|
)
|
||||||
|
)
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user