diff options
| -rwxr-xr-x | build-feed.py | 57 | ||||
| -rw-r--r-- | concerts-pubdates.json | 15 |
2 files changed, 62 insertions, 10 deletions
diff --git a/build-feed.py b/build-feed.py index fb85cab..2359344 100755 --- a/build-feed.py +++ b/build-feed.py @@ -1,6 +1,8 @@ #!/usr/bin/env python3 from datetime import datetime +import json +import re from sys import argv from urllib.parse import urljoin @@ -16,6 +18,20 @@ from helpers import ( ) +# TODO: handle timezones correctly. +# Places to disambiguate: +# +# - concerts.in: +# either add the zone explicitly, or deduce it from the place, +# assuming all times in concerts.in are local times. +# +# - concerts-pubdates.json: +# just add the zone explicitly. +# +# Until then, assume all these "naive times" describe the same timezone +# (CET/CEST). + + TIMEZONE = datetime.now().astimezone().tzinfo NOW = datetime.now(tz=TIMEZONE) DATE_FORMAT = '%-d %b %Y %H:%M %z' @@ -59,6 +75,9 @@ def join(sequence, joiner_factory): result.append(joiner_factory()) +CDATA_INDENT = 8*' ' + + def cdata_concert(concert, lang): formatters = DATE_FORMATTERS[lang] @@ -77,12 +96,15 @@ def cdata_concert(concert, lang): *(E.p(line) for line in concert.instructions.splitlines()), )) - return CDATA('\n'.join( - tostring(b, encoding='utf-8').decode() for b in blocks - )) + html_blocks = (tostring(b, encoding='utf-8').decode() for b in blocks) + + cdata = '\n' + '\n'.join(html_blocks) + '\n' + cdata = re.sub('^', CDATA_INDENT, cdata, flags=re.MULTILINE) + + return CDATA(cdata) -def generate_concert(concert, concerts_url, lang): +def generate_concert(concert, concerts_url, pubdates, lang): formatters = LOCALIZED_FORMATS[lang] with tmplocale(lang): @@ -90,21 +112,32 @@ def generate_concert(concert, concerts_url, lang): anchor = f'concert-{concert.time.strftime("%F")}' - return E.item( + 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 = datetime.fromisoformat(pubdate_str).replace(tzinfo=TIMEZONE) + 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) -def generate_concerts(concerts_src, concerts_url, lang): return tuple( - generate_concert(c, concerts_url, lang) + generate_concert(c, concerts_url, pubdates, lang) for c in read_concerts(concerts_src) ) -def main(concerts_src, feed_dst, domain): +def main(concerts_src, feed_dst, concerts_pubdates, domain): lang = guess_language(concerts_src) text = LOCALIZED_TEXT[lang] @@ -114,6 +147,10 @@ def main(concerts_src, feed_dst, domain): 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']), @@ -126,7 +163,7 @@ def main(concerts_src, feed_dst, domain): E.lastBuildDate(now_formatted), E.pubDate(now_formatted), E.language(lang), - *generate_concerts(concerts_src, concerts_url, lang), + *concerts, ), version='2.0', ) @@ -138,4 +175,4 @@ def main(concerts_src, feed_dst, domain): if __name__ == '__main__': - main(argv[1], argv[2], argv[3]) + main(*argv[1:]) diff --git a/concerts-pubdates.json b/concerts-pubdates.json new file mode 100644 index 0000000..4232ed7 --- /dev/null +++ b/concerts-pubdates.json @@ -0,0 +1,15 @@ +{ + "2019-10-05T17:00": null, + "2019-10-06T16:00": null, + "2020-03-08T16:00": null, + "2020-08-24T20:00": null, + "2021-04-03T20:00": "2021-03-19T16:00", + "2021-06-13T15:00": "2021-03-19T16:00", + "2021-08-17T20:30": null, + "2021-08-19T20:00": null, + "2021-10-25T18:00": null, + "2021-10-28T18:00": null, + "2021-12-12T16:00": "2021-11-23T23:28", + "2021-12-31T20:00": null, + "2022-05-07T17:00": null +} |
