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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
#!/usr/bin/env python3
from datetime import datetime
import json
import re
from sys import argv
from urllib.parse import urljoin
from lxml.builder import E
from lxml.etree import CDATA, XML, indent, tostring
from helpers import (
DATE_FORMATTERS,
guess_language,
read_concerts,
tmplocale,
touchup_plaintext,
)
def zoned_datetime(dt):
# Assume that whoever wrote the naive timestamp that this datetime
# was generated from had the same local time we do.
# Note: even if we assume that all timestamps in concerts.in and
# concerts-pubdates.json come from the same "zone" (Europe/Paris),
# the *time offset* can differ (CET vs CEST), so we can't just
# define a global TIMEZONE = datetime.now().tzinfo and slap that
# on every datetime.
return dt.replace(tzinfo=dt.astimezone().tzinfo)
NOW = zoned_datetime(datetime.now())
DATE_FORMAT = '%-d %b %Y %H:%M %z'
LOCALIZED_TEXT = {
'en': {
'title': 'Bellefeuille Quartet',
'indexpath': 'en/',
'description': 'News from the Bellefeuille quartet',
},
'fr': {
'title': 'Quatuor Bellefeuille',
'indexpath': '/',
'description': 'Des nouvelles du quatuor Bellefeuille',
},
}
LOCALIZED_FORMATS = {
'en': {
'title': lambda c: f'{c.time.strftime("%B %-d %Y")} in {c.place}',
},
'fr': {
'title': lambda c: f'{c.time.strftime("%-d %B %Y")} à {c.place}',
},
}
def join(sequence, joiner_factory):
# There's got to be a standard itertools/functools thingy to do that…
result = []
for i, item in enumerate(sequence, start=1):
result.append(item)
if i == len(sequence):
break
result.append(joiner_factory())
return result
def cdata_concert(concert, lang):
formatters = DATE_FORMATTERS[lang]
blocks = []
if concert.warning is not None:
blocks.append(E.p(concert.warning))
with tmplocale(lang):
blocks.extend((
E.p(formatters['date'](concert.time)),
E.p(formatters['time'](concert.time)),
))
pieces = touchup_plaintext(concert.pieces)
instructions = touchup_plaintext(concert.instructions)
blocks.extend((
E.p(*join(concert.address.splitlines(), E.br)),
E.ol(
*(XML(f'<li>{line}</li>') for line in pieces.splitlines())
),
*(XML(f'<p>{line}</p>') for line in instructions.splitlines() if line),
))
# Do a silly dance to indent CDATA correctly.
for b in blocks:
indent(b)
html_blocks = (tostring(b, encoding='utf-8').decode() for b in blocks)
cdata = '\n'.join(html_blocks) + '\n'
cdata = re.sub('^', 8*' ', cdata, flags=re.MULTILINE)
return CDATA('\n' + cdata)
def generate_concert(concert, concerts_url, pubdates, lang):
formatters = LOCALIZED_FORMATS[lang]
with tmplocale(lang):
title = formatters['title'](concert)
anchor = f'concert-{concert.time.strftime("%F")}'
item = E.item(
E.title(title),
E.link(f'{concerts_url}#{anchor}'),
E.description(cdata_concert(concert, lang)),
)
pubdate_str = pubdates[concert.time.isoformat(timespec='minutes')]
if pubdate_str is not None:
pubdate = zoned_datetime(datetime.fromisoformat(pubdate_str))
item.append(E.pubDate(pubdate.strftime(DATE_FORMAT)))
return item
def generate_concerts(concerts_src, concerts_url, concerts_pubdates, lang):
with open(concerts_pubdates) as pubdates_file:
pubdates = json.load(pubdates_file)
return tuple(
generate_concert(c, concerts_url, pubdates, lang)
for c in read_concerts(concerts_src)
)
def main(concerts_src, feed_dst, concerts_pubdates, domain):
lang = guess_language(concerts_src)
text = LOCALIZED_TEXT[lang]
url = f'https://{domain}'
index_url = urljoin(url, text['indexpath'])
concerts_url = urljoin(index_url, 'concerts.html')
now_formatted = NOW.strftime(DATE_FORMAT)
concerts = generate_concerts(
concerts_src, concerts_url, concerts_pubdates, lang
)
rss = E.rss(
E.channel(
E.title(text['title']),
E.link(index_url),
E.description(text['description']),
E.image(
E.url(urljoin(url, 'images/logo.svg')),
E.link(concerts_url),
),
E.lastBuildDate(now_formatted),
E.pubDate(now_formatted),
E.language(lang),
*concerts,
),
version='2.0',
)
indent(rss)
with open(feed_dst, 'wb') as feed:
feed.write(tostring(rss, encoding='utf-8', xml_declaration=True))
if __name__ == '__main__':
main(*argv[1:])
|