website/prep.py

187 lines
7.0 KiB
Python
Raw Normal View History

#! /usr/bin/env python3
2023-05-05 10:55:05 +00:00
import csv
2023-05-05 10:55:05 +00:00
from datetime import datetime
DAYS_SR = ["PON", "UTO", "SRE", "ČET", "PET", "SUB", "NED"]
2024-04-03 19:23:13 +00:00
DAYS_EN = ["MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"]
2024-03-31 10:25:49 +00:00
TYPES_DICT = {
"hack": ("hakaton", "hackathon"),
"lecture": ("predavanje", "lecture"),
2024-03-31 10:28:34 +00:00
"workshop": ("radionica", "workshop"),
2024-03-31 10:25:49 +00:00
"discussion": ("diskusija", "discussion"),
"lighting": ("kratka predavanja", "short talks"),
"movie": ("film", "movie"),
"meeting": ("sastanak", "meeting"),
2024-05-20 23:56:19 +00:00
"conference": ("konferencija", "conference"),
2024-07-07 11:13:51 +00:00
"music": ("svirka", "gig"),
"party": ("zabava", "entertainment"),
2024-03-31 10:25:49 +00:00
}
2023-05-05 10:55:05 +00:00
def load_events(csv_path:str) -> list[dict]:
2023-05-05 10:55:05 +00:00
events = []
with open(csv_path) as csv_file:
2023-09-29 19:12:55 +00:00
csv_reader = csv.reader(csv_file, skipinitialspace=True)
next(csv_reader, None)
for event in csv_reader:
event_date = event[0]
event_date_parsed = datetime.strptime(event_date, "%d-%m-%Y").date()
event_time = event[1]
event_location = event[2]
event_title = event[3]
2024-03-31 10:25:49 +00:00
types = event[4].split()
link = event[5]
changed = event[6]
current_event = {"date":event_date_parsed,
"time":event_time,
"location": event_location,
2024-03-31 10:25:49 +00:00
"title":event_title.strip(),
2024-07-25 18:47:34 +00:00
"types": types,
"link": link,
"changed": changed.strip()}
events.append(current_event)
return events
2024-03-31 10:25:49 +00:00
def build_html(events: list[dict], dayNames: list[str], typesNames: dict) -> str:
events_html = []
2023-08-29 11:58:50 +00:00
for event in events:
title = event["title"]
location = event["location"]
date = event["date"]
date = dayNames[date.weekday()]+", "+str(date.day)+". "+str(date.month)+". "+str(date.year)+", "
time = event["time"]+"h"
changed = event["changed"]
event_html = []
timeChanged = changed == "time" or changed == "date"
locationChanged = changed == "location"
event_html.append(f"<div class='date {"changed" if timeChanged else ""}'>{date} {time}</div>")
2024-07-25 18:47:34 +00:00
if event["link"] != "":
event_html.append(f"<div class='title'><a href=\"{event['link']}\">{title}</a></div>")
else:
event_html.append(f"<div class='title'>{title}</div>")
2023-08-29 11:58:50 +00:00
if "https://" in location:
place,link = location.split("https://")
event_html.append(f"<div class='place {"changed" if locationChanged else ""}'><a href=\"https://{link}\">@{place.strip()}</a></div>")
2023-08-29 11:58:50 +00:00
else:
event_html.append(f"<div class='place {"changed" if locationChanged else ""}'>@{location.strip()}</div>")
if changed != "":
event_html.append("<div class ='changedMark'></div>")
2024-03-31 10:25:49 +00:00
if len(event["types"]) != 0:
types_list = "<div class='types'>"
last_item = event["types"][-1]
for t in event["types"]:
if typesNames.get(t) is not None:
types_list += typesNames.get(t)
if t != last_item:
types_list += ', '
else:
print(f"Unknown type {t}!")
types_list += "</div>"
event_html.append(types_list)
event_html = "".join(event_html)
events_html.append(f"\n<div class='event'>{event_html}</div>")
return events_html
2024-02-08 15:50:05 +00:00
def build_ical(events: list[dict]) -> str:
today = datetime.today().now()
# Header
events_ical = ""
with open("template/head.ical", "r") as file:
events_ical += file.read()
# Events
for event in events:
title = event["title"]
location = event["location"]
date = event["date"]
2024-02-08 16:21:52 +00:00
time = event["time"]
2024-08-05 10:36:54 +00:00
url = event["link"]
2024-02-08 16:21:52 +00:00
uid = str(date.month).zfill(2) + str(date.day).zfill(2) + time[:2]
2024-02-08 15:50:05 +00:00
date = str(date.year) + str(date.month).zfill(2) + str(date.day).zfill(2)
created = str(today.year) + str(today.month).zfill(2) + str(today.day).zfill(2) + "T" + str(today.hour).zfill(2) + str(today.minute).zfill(2) + str(today.second).zfill(2) + "Z"
date = date + "T" + time.replace(":", "") + "00"
event_template = ""
with open("template/event.ical", "r") as file:
event_template += file.read()
2024-02-08 16:19:13 +00:00
event_template = event_template.replace("<!--UID-->", uid)
2024-02-08 15:50:05 +00:00
event_template = event_template.replace("<!--CREATED-->", created)
event_template = event_template.replace("<!--DATE-->", date)
event_template = event_template.replace("<!--TITLE-->", title)
2024-08-05 10:36:54 +00:00
event_template = event_template.replace("<!--URL-->", url)
2024-11-29 18:02:56 +00:00
if location.startswith("DC Krov"):
event_template = event_template.replace("<!--LOCATION-->", "DC Krov\\, Kraljice Marije 47\\, 6\\, Beograd\\, Serbia")
elif location.startswith("Matematički fakultet (Učionica 153)"):
event_template = event_template.replace("<!--LOCATION-->", "Matematički fakultet\\, Svetog Nikole 39\\, Beograd\\, Serbia")
else:
event_template = event_template.replace("<!--LOCATION-->", "")
2024-02-08 15:50:05 +00:00
events_ical += event_template
# Footer
with open("template/end.ical", "r") as file:
events_ical += file.read()
return events_ical
events = sorted(load_events("dogadjaji.csv"), key=lambda e: e["date"])
today = datetime.today().date()
past_events = list(filter(lambda e: e["date"] <= today, events))
2023-11-12 09:29:32 +00:00
past_events.reverse()
new_events = list(filter(lambda e: e["date"] >= today, events))
page_template = ""
2024-03-31 10:25:49 +00:00
sr_types = {}
en_types = {}
for key, value_pair in TYPES_DICT.items():
sr_types[key] = value_pair[0]
en_types[key] = value_pair[1]
# Build Serbian Events page
2024-03-31 10:25:49 +00:00
new_events_html = build_html(new_events, DAYS_SR, sr_types)
with open("pages/sr/events.html", "r") as file:
page_template = ([line for line in file])
with open("pages/sr/events.html", "w") as file:
file.writelines(page_template + new_events_html)
# Build English Events page
2024-03-31 10:25:49 +00:00
new_events_html = build_html(new_events, DAYS_EN, en_types)
with open("pages/en/events.html", "r") as file:
page_template = ([line for line in file])
2023-08-29 11:58:50 +00:00
with open("pages/en/events.html", "w") as file:
file.writelines(page_template + new_events_html)
2023-05-05 10:55:05 +00:00
# Build Serbian Archive page
2024-03-31 10:25:49 +00:00
past_events_html = build_html(past_events, DAYS_SR, sr_types)
with open("pages/sr/events_archive.html", "r") as file:
page_template = ([line for line in file])
2023-05-05 10:55:05 +00:00
with open("pages/sr/events_archive.html", "w") as file:
file.writelines(page_template + past_events_html)
2023-05-05 10:55:05 +00:00
# Build English Archive page
2024-03-31 10:25:49 +00:00
past_events_html = build_html(past_events, DAYS_EN, en_types)
with open("pages/en/events_archive.html", "r") as file:
page_template = ([line for line in file])
2023-08-29 11:58:50 +00:00
with open("pages/en/events_archive.html", "w") as file:
file.writelines(page_template + past_events_html)
2024-02-08 15:50:05 +00:00
new_events_ical = build_ical(new_events)
# Build ical
with open("site/events.ical", "w") as file:
2024-02-08 15:56:11 +00:00
file.write(build_ical(new_events))
2024-02-08 15:50:05 +00:00