| 1 | """Deterministic Atom rendering for the saved research library.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import re |
| 6 | from collections.abc import Mapping, Sequence |
| 7 | from datetime import datetime |
| 8 | from xml.etree import ElementTree as ET |
| 9 | |
| 10 | from .library import LibraryEntry |
| 11 | |
| 12 | |
| 13 | ATOM_NS = "http://www.w3.org/2005/Atom" |
| 14 | ET.register_namespace("", ATOM_NS) |
| 15 | |
| 16 | # XML 1.0 forbids most C0 controls, lone surrogates, and the U+FFFE/U+FFFF |
| 17 | # noncharacters, but ElementTree writes them out verbatim. One such byte in a |
| 18 | # saved report title or summary makes the whole feed unparseable for readers. |
| 19 | _XML_INVALID_CHARS = re.compile( |
| 20 | "[^\t\n\r\u0020-\ud7ff\ue000-\ufffd\U00010000-\U0010ffff]" |
| 21 | ) |
| 22 | |
| 23 | |
| 24 | def render_atom( |
| 25 | entries: Sequence[LibraryEntry], |
| 26 | *, |
| 27 | library_id: str, |
| 28 | entry_urls: Mapping[str, str] | None = None, |
| 29 | feed_url: str | None = None, |
| 30 | title: str = "last30days research library", |
| 31 | author: str = "last30days research library", |
| 32 | ) -> str: |
| 33 | """Render an Atom feed whose IDs and timestamps are stable across runs.""" |
| 34 | urls = entry_urls or {} |
| 35 | feed_id = f"urn:last30days:research-library:{library_id}" |
| 36 | root = ET.Element(_tag("feed")) |
| 37 | ET.SubElement(root, _tag("id")).text = feed_id |
| 38 | ET.SubElement(root, _tag("title")).text = _xml_text(title) |
| 39 | author_node = ET.SubElement(root, _tag("author")) |
| 40 | ET.SubElement(author_node, _tag("name")).text = _xml_text(author) |
| 41 | updated = max((item.source_updated_at for item in entries), default=None) |
| 42 | ET.SubElement(root, _tag("updated")).text = ( |
| 43 | _format_timestamp(updated) if updated else "1970-01-01T00:00:00Z" |
| 44 | ) |
| 45 | if feed_url: |
| 46 | ET.SubElement(root, _tag("link"), {"rel": "self", "href": feed_url}) |
| 47 | |
| 48 | for item in entries: |
| 49 | node = ET.SubElement(root, _tag("entry")) |
| 50 | entry_id = item.entry_id.removeprefix("urn:last30days:") |
| 51 | ET.SubElement(node, _tag("id")).text = f"{feed_id}:{entry_id}" |
| 52 | ET.SubElement(node, _tag("title")).text = _xml_text(item.headline) |
| 53 | ET.SubElement(node, _tag("updated")).text = _format_timestamp(item.source_updated_at) |
| 54 | ET.SubElement(node, _tag("published")).text = f"{item.published_date.isoformat()}T00:00:00Z" |
| 55 | ET.SubElement(node, _tag("category"), {"term": _xml_text(item.topic)}) |
| 56 | url = urls.get(item.entry_id, f"briefs/{item.output_name}") |
| 57 | ET.SubElement(node, _tag("link"), {"href": _xml_text(url)}) |
| 58 | ET.SubElement(node, _tag("summary"), {"type": "text"}).text = _xml_text(item.summary) |
| 59 | |
| 60 | ET.indent(root, space=" ") |
| 61 | return '<?xml version="1.0" encoding="utf-8"?>\n' + ET.tostring(root, encoding="unicode") + "\n" |
| 62 | |
| 63 | |
| 64 | def _tag(name: str) -> str: |
| 65 | return f"{{{ATOM_NS}}}{name}" |
| 66 | |
| 67 | |
| 68 | def _xml_text(value: str) -> str: |
| 69 | return _XML_INVALID_CHARS.sub("", value) |
| 70 | |
| 71 | |
| 72 | def _format_timestamp(value: datetime) -> str: |
| 73 | return value.isoformat().replace("+00:00", "Z") |
| 74 |