-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_website.py
130 lines (117 loc) · 5 KB
/
generate_website.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import os
from datetime import date, datetime
from typing import Iterable
import itertools
#import pandas as pd
import jinja2
import yaml
from constants import (DIR_TEMPLATES, DIR_PUBLIC, DIR_DATA,
COL_CONF_TYPE,
MAP_CONF_TYPE,
)
FORMATTER_DATE = lambda x: date.strftime(x, "%B %d, %Y")
def load_data():
yml_files = [x for x in os.listdir(DIR_DATA) if x.endswith(".yml")]
data = []
for _file in yml_files:
print(_file)
_file_path = os.path.join(DIR_DATA, _file)
with open(_file_path, 'r') as yml_file:
data.extend(yaml.safe_load(yml_file))
return data
def create_conference_table(conf_list: Iterable, year: int=date.today().year):
week_data = {k+1: [] for k in range(52)}
deadline_data = {k+1: [] for k in range(52)}
for conference in conf_list:
#conference["type"] = MAP_CONF_TYPE.get(conference.get("type", "Unknown"), "unknown")
conference['type'] = conference.get("type", "unknown")
dates = conference.get("dates")
if dates is None:# or len(dates) != 2:
continue
for _conf_dates in dates:
_start_date, _end_date = _conf_dates
if _start_date.year != year:
continue
start_week = _start_date.isocalendar()[1]
week_data[start_week].append(conference)
end_week = _end_date.isocalendar()[1]
if end_week != start_week:
week_data[end_week].append(conference)
deadlines = conference.get("deadline")
if deadlines is None:
continue
for deadline in deadlines:
deadline_iso = deadline.isocalendar()
deadline_sort_year = deadline_iso.year
if deadline_sort_year != year:
continue
deadline_week = deadline_iso.week
deadline_data[deadline_week].append(conference)
return week_data, deadline_data
def week_days_format(week: int, year=date.today().year, format="%b %d"):
first_day = date.fromisocalendar(year, week, 1)
last_day = date.fromisocalendar(year, week, 7)
_text = "{} – {}".format(first_day.strftime(format),
last_day.strftime(format))
return _text
def main():
data = load_data()
#print(week_data)
env = jinja2.Environment(loader=jinja2.FileSystemLoader(DIR_TEMPLATES))
env.filters['weekdates'] = week_days_format
#template = env.get_template("index.html")
template = env.get_template("weeks.html")
today = date.today()
now = datetime.now()
timestamp = today.strftime("%B %d, %Y")
import icalendar
cal = icalendar.Calendar()
cal.add("prodid", "-//Conference Calendar//Conference Calendar//EN")
cal.add("version", "2.0")
cal_deadline = icalendar.Calendar()
cal_deadline.add("prodid", "-//Conference Deadlines//Conference Calendar//EN")
cal_deadline.add("version", "2.0")
for conference in data:
for start_date, end_date in conference.get("dates", []):
event = icalendar.Event()
uid = "{:%Y%m%d}{:%Y%m%d}-{}".format(start_date, end_date,
conference["abbreviation"])
event.add("uid", uid)
event.add("dtstamp", now)
event.add("summary", conference['name'])
event.add("dtstart", start_date)
event.add("dtend", end_date)
cal.add_component(event)
for deadline in conference.get("deadline", []):
event = icalendar.Event()
uid = "{:%Y%m%d}-{}".format(deadline, conference["abbreviation"])
event.add("uid", uid)
event.add("dtstamp", now)
#event.add("summary", conference['name'])
event.add("summary", f"Deadline: {conference['abbreviation']}")
event.add("dtstart", deadline)
cal_deadline.add_component(event)
with open(os.path.join(DIR_PUBLIC, "conferences.ics"), "wb") as ics_file:
ics_file.write(cal.to_ical())
with open(os.path.join(DIR_PUBLIC, "deadlines.ics"), "wb") as ics_file:
ics_file.write(cal_deadline.to_ical())
week_data = {}
deadline_data = {}
for year in [today.year, today.year+1]:
_week_data, _deadline_data = create_conference_table(data, year)
week_data[year] = _week_data
deadline_data[year] = _deadline_data
datasets = [("Conference Dates", "index.html", week_data),
("Submission Deadlines", "deadlines.html", deadline_data),
]
for _title, _html_file, _data in datasets:
content = template.render(conferences=data,
conf_data=_data,
title=_title,
timestamp=timestamp,
today=today)
out_path = os.path.join(DIR_PUBLIC, _html_file)
with open(out_path, 'w') as html_file:
html_file.write(content)
if __name__ == "__main__":
results = main()