This repository has been archived by the owner on Dec 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
newsletter.py
117 lines (93 loc) · 4.04 KB
/
newsletter.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
from database import entriesFromCategory, week
from string import Template
from datetime import date, timedelta
from config import *
with open("kilta-tiedottaa-template.html", 'r') as f:
txt = f.read()
template = Template(txt)
first_paragraph = ""
table_of_contents = ""
contents = ""
first_paragraph_en = ""
table_of_contents_en = ""
contents_en = ""
# TODO
# - use Jinja for templating
# - sort entries by date
# - english version
# - some smart way to make first paragraph
# - maybe categories could be defined only in one place instead of two
#categories = ["Killan tapahtumat", "Muut yhdistykset", "Opinnot", "Yleistä"]
# index for unique id:s to create links
idx = 1
for category in categories:
entries = entriesFromCategory(category)
entries.sort(key=lambda k: date(k['date'][2], k['date'][1], k['date'][0]))
if entries:
table_of_contents += f"""<tr>
<td style="color: #201E1E; font-family: Georgia; font-size: 24px;">
<i>{category}</i>
</td>
</tr>\n"""
table_of_contents += """<tr>
<td style="padding: 10px 0px 15px 0px; font-family: Calibri; font-size: 16px;">
<ul style=" margin: 0;">\n"""
for entry in entries:
# escape some characters and change linebreaks to html
formattedHeader = entry['header'].replace("&","&")\
.replace("<","<").replace(">", ">")\
.replace("\"",""").replace("\'","'")
formattedContent = entry['content'].replace("&","&")\
.replace("<","<").replace(">", ">")\
.replace("\"",""").replace("\'","'").replace("\n", "<br/>")
table_of_contents += f"<li><a href='#id{idx}'>{formattedHeader}</a></li>\n"
contents += f"""<tr>
<td id='id{idx}' style="padding: 10px 0px 15px 0px; font-family: Calibri; font-size: 16px;">
<b>{formattedHeader}</b><br/>
{formattedContent}
</td>
</tr>\n"""
idx += 1
table_of_contents += "</ul>\n</td>\n</tr>\n"
#categories_en = ["Guild's events", "Other organizations", "Studies", "General"]
for category in categoriesEn:
entries = entriesFromCategory(category, True)
entries.sort(key=lambda k: date(k['date'][2], k['date'][1], k['date'][0]))
if entries:
table_of_contents_en += f"""<tr>
<td style="color: #201E1E; font-family: Georgia; font-size: 24px;">
<i>{category}</i>
</td>
</tr>\n"""
table_of_contents_en += """<tr>
<td style="padding: 10px 0px 15px 0px; font-family: Calibri; font-size: 16px;">
<ul style=" margin: 0;">\n"""
for entry in entries:
formattedHeader = entry['header'].replace("&","&")\
.replace("<","<").replace(">", ">")\
.replace("\"",""").replace("\'","'")
formattedContent = entry['content'].replace("&","&")\
.replace("<","<").replace(">", ">")\
.replace("\"",""").replace("\'","'").replace("\n", "<br/>")
table_of_contents_en += f"<li><a href='#id{idx}'>{formattedHeader}</a></li>\n"
contents_en += f"""<tr>
<td id='id{idx}' style="padding: 10px 0px 15px 0px; font-family: Calibri; font-size: 16px;">
<b>{formattedHeader}</b><br/>
{formattedContent}
</td>
</tr>\n"""
idx += 1
table_of_contents_en += "</ul>\n</td>\n</tr>\n"
d = {
'WEEK': week,
'FIRST_PARAGRAPH': first_paragraph,
'TABLE_OF_CONTENTS': table_of_contents,
'CONTENTS': contents,
'FIRST_PARAGRAPH_EN': first_paragraph_en,
'TABLE_OF_CONTENTS_EN': table_of_contents_en,
'CONTENTS_EN': contents_en
}
newsLetter = template.substitute(d)
with open(f'mails/kilta-tiedottaa-viikko-{week}.html', 'w') as f:
f.write(newsLetter)
print(newsLetter)