#! /usr/bin/env python
from datetime import datetime
from functools import cmp_to_key
DAYS_SR = ["Ponedeljak", "Utorak", "Sreda", "Četvrtak", "Petak", "Subota", "Nedelja"]
DAYS_EN = ["Monday ", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
MONTHS_SR = [
"Januar", "Februar", "Mart", "April", "Maj", "Jun",
"Jul", "Avgust", "Septembar", "Oktobar", "Novembar", "Decembar",
]
MONTHS_EN = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December",
]
today = datetime.today().date()
def parse_date(date):
return datetime.strptime(date,"%d-%m-%Y").date()
def compare_events(one, two):
one = parse_date(one.split(", ")[0])
two = parse_date(two.split(", ")[0])
if one>two:
return 1
elif one==two:
return 0
else:
return -1
def is_past_event(event):
return event < today
def load_events():
events = []
with open("dogadjaji.csv", "rt") as file:
file.readline()
for event in file.readlines():
event = event.strip()
if event != "":
events.append(event)
return events
def write_events(events):
with open("dogadjaji.csv", "wt") as file:
file.write("datum, vreme, lokacija, tema\n")
for event in events:
file.write(event+"\n")
def sort_events(events):
return sorted(events, key = cmp_to_key(compare_events))
def build_html(events, dayNames, monthNames):
newevents = []
for event in events:
date, time, location, title = event.split(", ")
date = parse_date(date)
if is_past_event(date):
continue
date = dayNames[date.weekday()]+", "+str(date.day)+". "+monthNames[date.month-1]+" "+str(date.year)+"."
time = time+"h"
future_event = []
future_event.append("
"+date+" | ")
future_event.append(" "+time+" | ")
if "https://" in location:
place,link = location.split("https://")
future_event.append(" "+place.strip()+" | ")
else:
future_event.append(" "+location.strip()+" | ")
future_event.append(" "+title+" | ")
newevents.append("\n"+"\n".join(future_event)+"\n
")
return newevents
events = load_events()
events = sort_events(events)
write_events(events)
newevents = build_html(events, DAYS_SR, MONTHS_SR)
with open("pages/sr/events.html","wt") as file:
file.writelines(["Događaji
\n", "\n", "Datum | Vreme | Mesto | Tema |
\n"])
file.writelines(newevents)
file.writelines(["
"])
newevents = build_html(events, DAYS_EN, MONTHS_EN)
with open("pages/en/events.html","wt") as file:
file.writelines(["Events
\n", "\n", "Date | Time | Place | Topic |
"])
file.writelines(newevents)
file.writelines(["
"])