From 9372b46a1636eb6b18b3df627eb62b324dfce3f6 Mon Sep 17 00:00:00 2001 From: Kévin Le Gouguec Date: Tue, 22 Feb 2022 22:24:31 +0100 Subject: [wip] Add publication dates --- build-feed.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 47 insertions(+), 10 deletions(-) (limited to 'build-feed.py') 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:]) -- cgit v1.2.3