From 0645754d5742c955965d35cdbd617d550f959146 Mon Sep 17 00:00:00 2001 From: Kévin Le Gouguec Date: Mon, 21 Feb 2022 13:19:24 +0100 Subject: [wip] Generate RSS feed from concerts list --- build-feed.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100755 build-feed.py diff --git a/build-feed.py b/build-feed.py new file mode 100755 index 0000000..5b14326 --- /dev/null +++ b/build-feed.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 + +from urllib.parse import urljoin +from xml.etree.ElementTree import Element, SubElement, indent, tostring + + +DOMAIN = 'quatuorbellefeuille.com' +LANG = 'fr' + + +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', + }, +} + +URL = f'https://{DOMAIN}' +INDEX_URL = urljoin(URL, LOCALIZED_TEXT[LANG]['indexpath']) + + +def text_element(tag, text, /, **kwargs): + elt = Element(tag, **kwargs) + elt.text = text + return elt + + +rss = Element('rss', version='2.0') + +channel = SubElement(rss, 'channel') + +channel.extend(( + text_element('title', LOCALIZED_TEXT[LANG]['title']), + text_element('link', INDEX_URL), + text_element('description', LOCALIZED_TEXT[LANG]['description']), +)) + +image = SubElement(channel, 'image') +image.extend(( + text_element('url', urljoin(URL, 'images/logo.svg')), + text_element('link', urljoin(INDEX_URL, 'concerts.html')), +)) + +channel.append(text_element('language', LANG)) + +indent(rss) + +print(tostring(rss, encoding='utf-8', xml_declaration=True).decode()) -- cgit v1.2.3 From 6d9636d35c5d0d244208165010bb70635c98ab2c Mon Sep 17 00:00:00 2001 From: Kévin Le Gouguec Date: Mon, 21 Feb 2022 15:18:39 +0100 Subject: Move some concert stuff to common helpers The RSS builder will need it. --- build-concerts.py | 114 +----------------------------------------------------- helpers.py | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 113 deletions(-) diff --git a/build-concerts.py b/build-concerts.py index 43dce57..7b0f75d 100755 --- a/build-concerts.py +++ b/build-concerts.py @@ -1,16 +1,13 @@ #!/usr/bin/env python3 from contextlib import contextmanager -from dataclasses import dataclass from datetime import datetime import locale -from operator import attrgetter from pathlib import Path import re from sys import argv -from typing import Iterator, Optional -from helpers import relative_path +from helpers import guess_language, read_concerts, relative_path # TODO: change some jargon: @@ -18,115 +15,6 @@ from helpers import relative_path # - canceled => warning -LICENSE_URLS = { - 'CC0': 'https://creativecommons.org/publicdomain/zero', - 'CC BY': 'https://creativecommons.org/licenses/by', - 'CC BY-SA': 'https://creativecommons.org/licenses/by-sa', -} - -LICENSE_RE = re.compile( - '('+'|'.join(LICENSE_URLS.keys())+')' + ' ([0-9.]+)' -) - - -@dataclass -class LicenseInfo: - tag: str - version: str - - @classmethod - def deserialize(cls, info): - if info is None: - return None - return cls(*LICENSE_RE.fullmatch(info).groups()) - - def format(self): - url = f'{LICENSE_URLS[self.tag]}/{self.version}/' - - return f'{self.tag}' - - -@dataclass -class Illustration: - file: str - alt_text: str - source_name: str - source_link: Optional[str] - license_info: Optional[LicenseInfo] - - @classmethod - def deserialize(cls, d): - return cls(d['pic_file'], - d['pic_alt'], - d['pic_src'], - d['pic_link'], - LicenseInfo.deserialize(d['pic_license'])) - - -@dataclass -class Concert: - time: datetime - place: str - address: str - pieces: Iterator[str] - instructions: str - illustration: Illustration - warning: Optional[str] - - @classmethod - def deserialize(cls, d): - return cls( - time=datetime.strptime(d['time'], '%d/%m/%Y %Hh%M'), - place=d['place'], - address=d['address'], - pieces=d['pieces'], - instructions=d['instructions'], - illustration=Illustration.deserialize(d), - warning=d['warning'] - ) - - -def optional(line): - return f'(?:{line})?' - - -CONCERT_LINES = ( - r'QUAND : (?P