"""XML sitemaps and sitemap indexes.""" from __future__ import annotations import re from dataclasses import dataclass from xml.etree import ElementTree as ET _NS = re.compile(r"\{[^}]+\}") @dataclass class SitemapEntry: loc: str lastmod: str | None = None is_index: bool = False def parse_sitemap(content: bytes | str) -> list[SitemapEntry]: if isinstance(content, str): content = content.encode() try: root = ET.fromstring(content) except ET.ParseError: return [] tag = _NS.sub("", root.tag).lower() is_index = tag == "sitemapindex" out: list[SitemapEntry] = [] for node in root: loc = None lastmod = None for child in node: name = _NS.sub("", child.tag).lower() if name == "loc" and child.text: loc = child.text.strip() elif name == "lastmod" and child.text: lastmod = child.text.strip() if loc: out.append(SitemapEntry(loc=loc, lastmod=lastmod, is_index=is_index)) return out __all__ = ["SitemapEntry", "parse_sitemap"]