-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
88 lines (67 loc) · 2.21 KB
/
generate.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
import datetime
import json
from pathlib import Path
from jinja2 import Environment, FileSystemLoader
main_dir = Path(__file__).resolve().parent
def filter_geojson(geojson, filter_dates):
"""
Keep only events whose dates are included
in filter dates
"""
tmp = {
"type": "FeatureCollection",
"features": []
}
for feature in geojson['features']:
dates = feature['properties']['dates']
for d in filter_dates:
if d in dates:
tmp['features'].append(feature)
break
return tmp
def format_dates(geojson):
"""
Transform
[Sunday 10 October, Monday 11 October]
to
Sunday 10, Monday 11 [October]
"""
for feature in geojson['features']:
dates = feature['properties']['dates']
date_str = ''
days = [' '.join(i.split(' ')[:2]) for i in dates]
months = [i.split(' ')[2] for i in dates]
for i in range(len(days)):
date_str += days[i]
if i == len(days)-1 or months[i] != months[i+1]:
date_str += f' [{months[i]}] '
else:
date_str += ', '
feature['properties']['dates'] = date_str
return geojson
def generate_html():
print('# Generating html map')
with open(main_dir / 'data' / 'gzmusica.geojson') as f:
geojson = json.load(f)
geojson = format_dates(geojson)
today = datetime.date.today()
# today = datetime.date(2021, 8, 1)
env = Environment(loader=FileSystemLoader(main_dir / 'htmls'),
trim_blocks=True)
template = env.get_template('axenda_template.html')
html_str = template.render(
geojson=geojson,
today_geojson=filter_geojson(
geojson,
[today.strftime("%A %d")]),
tomorrow_geojson=filter_geojson(
geojson,
[(today + datetime.timedelta(days=1)).strftime("%A %d")]),
week_geojson=filter_geojson(
geojson,
[(today + datetime.timedelta(days=i)).strftime("%A %d") for i in range(0, 7)]),
)
with open(main_dir / 'htmls' / 'axenda.html', 'w') as f:
f.write(html_str)
if __name__ == "__main__":
generate_html()