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
|
#!/usr/bin/env python3
from sys import argv
from urllib.parse import urljoin
from xml.etree.ElementTree import Element, SubElement, indent, tostring
from helpers import (
DATE_FORMATTERS,
guess_language,
read_concerts,
tmplocale,
touchup_plaintext,
)
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 text_element(tag, text, /, **kwargs):
elt = Element(tag, **kwargs)
elt.text = text
return elt
def block(tag, content):
return f'<{tag}>{content}</{tag}>'
def cdata_concert(concert, lang):
formatters = DATE_FORMATTERS[lang]
blocks = []
if concert.warning is None:
blocks.append(block('p', concert.warning))
blocks.extend((
block('p', formatters['date'](concert.time)),
block('p', formatters['time'](concert.time)),
block('p', concert.address.replace('\n', '<br>'))
))
piece_list = '\n'.join(
block('li', touchup_plaintext(p)) for p in concert.pieces.splitlines()
)
blocks.append(block('ol', piece_list))
blocks.extend(
block('p', touchup_plaintext(line))
for line in concert.instructions.splitlines()
)
cdata = '\n'.join((blocks))
return f'<![CDATA[{cdata}]]'
def generate_concert(concert, concerts_url, lang):
formatters = LOCALIZED_FORMATS[lang]
item = Element('item')
with tmplocale(lang):
title = formatters['title'](concert)
item.append(text_element('title', title))
anchor = f'concert-{concert.time.strftime("%F")}'
item.append(text_element('link', f'{concerts_url}#{anchor}'))
cdata = cdata_concert(concert, lang)
item.append(text_element('description', cdata))
return item
def generate_concerts(concerts_src, concerts_url, lang):
for c in read_concerts(concerts_src):
yield generate_concert(c, concerts_url, lang)
def main(concerts_src, feed_dst, 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')
rss = Element('rss', version='2.0')
channel = SubElement(rss, 'channel')
channel.extend((
text_element('title', text['title']),
text_element('link', index_url),
text_element('description', text['description']),
))
image = SubElement(channel, 'image')
image.extend((
text_element('url', urljoin(url, 'images/logo.svg')),
text_element('link', concerts_url),
))
channel.append(text_element('language', lang))
items = generate_concerts(concerts_src, concerts_url, lang)
channel.extend(tuple(items))
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], argv[2], argv[3])
|