SPB Git forge

spb/ai-atlas

Public
41commits 1branches 0releases
4.6 MBsize
maindefault branch
12 days agolast push
HTML 77.2% TypeScript 10.5% Python 9.6% JavaScript 2.5%
1.1 KB · 43 lines python
Raw Blame History
1"""XML sitemaps and sitemap indexes."""2from __future__ import annotations34import re5from dataclasses import dataclass6from xml.etree import ElementTree as ET78_NS = re.compile(r"\{[^}]+\}")91011@dataclass12class SitemapEntry:13    loc: str14    lastmod: str | None = None15    is_index: bool = False161718def parse_sitemap(content: bytes | str) -> list[SitemapEntry]:19    if isinstance(content, str):20        content = content.encode()21    try:22        root = ET.fromstring(content)23    except ET.ParseError:24        return []25    tag = _NS.sub("", root.tag).lower()26    is_index = tag == "sitemapindex"27    out: list[SitemapEntry] = []28    for node in root:29        loc = None30        lastmod = None31        for child in node:32            name = _NS.sub("", child.tag).lower()33            if name == "loc" and child.text:34                loc = child.text.strip()35            elif name == "lastmod" and child.text:36                lastmod = child.text.strip()37        if loc:38            out.append(SitemapEntry(loc=loc, lastmod=lastmod, is_index=is_index))39    return out404142__all__ = ["SitemapEntry", "parse_sitemap"]43