2023-08-17 23:32:37 +02:00
|
|
|
from typing import List
|
2023-08-17 22:52:36 +02:00
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
import requests
|
|
|
|
from operator import add
|
|
|
|
from functools import reduce
|
2023-08-17 23:32:37 +02:00
|
|
|
import datetime
|
|
|
|
from event import Event
|
2023-08-17 22:52:36 +02:00
|
|
|
|
|
|
|
|
2023-08-17 23:32:37 +02:00
|
|
|
def get_soup() -> BeautifulSoup:
|
2023-08-17 22:52:36 +02:00
|
|
|
r = requests.get("http://www.pvv.ntnu.no/hendelser/")
|
|
|
|
soup = BeautifulSoup(r.text, "html.parser")
|
2023-08-17 23:32:37 +02:00
|
|
|
return soup
|
|
|
|
|
|
|
|
|
|
|
|
def process_soup(soup: BeautifulSoup) -> List[Event]:
|
2023-08-17 22:52:36 +02:00
|
|
|
zips = []
|
|
|
|
events = soup.find_all("ul", "events")
|
2023-08-17 23:32:37 +02:00
|
|
|
|
2023-08-17 22:52:36 +02:00
|
|
|
for event in events:
|
2023-08-17 23:32:37 +02:00
|
|
|
times, locations, organizers = zip(*(list(
|
2023-08-17 22:52:36 +02:00
|
|
|
map(lambda x: map(lambda y: y.find("strong").text, x),
|
|
|
|
filter(lambda x: x != [],
|
|
|
|
map(lambda x: x.find_all("li"),
|
|
|
|
event.find_all("li")))))))
|
2023-08-17 23:32:37 +02:00
|
|
|
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,
|
2023-08-17 22:52:36 +02:00
|
|
|
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 = []
|
|
|
|
for a in event.find_all("a", href=True):
|
|
|
|
if a['href'][:10] == '/hendelser':
|
|
|
|
ids.append(int(a['href'][a['href'].find("=")+1:]))
|
|
|
|
|
2023-08-17 23:32:37 +02:00
|
|
|
zips.append(list(zip(ids, times, names, organizers, locations, descriptions)))
|
2023-08-17 22:52:36 +02:00
|
|
|
|
|
|
|
events = reduce(add, zips)
|
2023-08-17 23:32:37 +02:00
|
|
|
events = list(map(lambda x: Event(*x), events))
|
|
|
|
return events
|
|
|
|
|
|
|
|
|
|
|
|
def get_events_today(events: List[Event]):
|
2023-08-18 00:33:33 +02:00
|
|
|
return list(filter(lambda e: e.start.date() == datetime.datetime.today().date(), events))
|
2023-08-17 23:32:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
print(get_events_today(process_soup(get_soup())))
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-17 22:52:36 +02:00
|
|
|
|