XmlDocument Class

A class to store an XML Sitemap (index, sub-sitemaps, or main-sitemap) Document.

This class contains several function methods to handle XML Sitemaps. It includes convenient functions e.g. add_url and add_url_set.

Source code in pysitemaps/__init__.py
class XmlDocument:
    """
    A class to store an XML Sitemap (index, sub-sitemaps, or main-sitemap) Document.

    This class contains several function methods to handle XML Sitemaps.
    It includes convenient functions e.g. add_url and add_url_set.

    """

    def __init__(
        self,
        loc: str,
        lastmod: str = datetime.now().strftime("%Y-%m-%d"),
        include_urls: bool = False,
    ) -> None:
        """Initialize XmlDocument

        Args:
            loc (str): loc of XmlDocument
            lastmod (str, optional): lastmod of XmlDocument. Defaults to datetime.now().strftime("%Y-%m-%d").
            include_urls (bool, optional): If true then XmlDocument will also include (aviaalble) Urls otherwise ignored. Defaults to False.
        """

        self.loc = loc
        self.lastmod = lastmod
        self.urls = []

        if include_urls:
            file_url = get_corrected_url(loc, fix_slash="")
            response = get_remote_content(file_url)
            if response.status_code == 200:
                self.add_from_text(response.text)

    def add_object(self, url_object: Url):
        """add Url Object to current XmlDocument

        Args:
            url_object (Url): Url Object.
        """
        if url_object:
            self.urls.append(url_object)

    def add_url(
        self,
        loc: str,
        lastmod: str = datetime.now().strftime("%Y-%m-%d"),
        images_loc: list = [],
    ) -> None:
        """add url to current sitemap by specifiyig loc, lastmod and list of images.

        Args:
            loc (str): _description_
            lastmod (str, optional): _description_. Defaults to datetime.now().strftime("%Y-%m-%d").
            images_loc (list, optional): _description_. Defaults to [].
        """
        if loc:
            self.urls.append(Url(loc=loc, lastmod=lastmod, images_loc=images_loc))

    def add_from_text(self, xml_as_text: str) -> None:
        """ """
        self.urls += extract_url_set(xml_as_text=xml_as_text)

    def as_dict(self) -> dict:
        """return XmlDocument as dit

        Returns:
            dict: contains loc, lastmod of XML Document. It also include urls key containing list of Url(s).
        """
        return {
            "loc": self.loc,
            "lastmod": self.lastmod,
            "urls": [url.as_dict() for url in self.urls],
        }

__init__(loc, lastmod=datetime.now().strftime('%Y-%m-%d'), include_urls=False)

Initialize XmlDocument

Parameters:

Name Type Description Default
loc str

loc of XmlDocument

required
lastmod str

lastmod of XmlDocument. Defaults to datetime.now().strftime(“%Y-%m-%d”).

datetime.now().strftime('%Y-%m-%d')
include_urls bool

If true then XmlDocument will also include (aviaalble) Urls otherwise ignored. Defaults to False.

False
Source code in pysitemaps/__init__.py
def __init__(
    self,
    loc: str,
    lastmod: str = datetime.now().strftime("%Y-%m-%d"),
    include_urls: bool = False,
) -> None:
    """Initialize XmlDocument

    Args:
        loc (str): loc of XmlDocument
        lastmod (str, optional): lastmod of XmlDocument. Defaults to datetime.now().strftime("%Y-%m-%d").
        include_urls (bool, optional): If true then XmlDocument will also include (aviaalble) Urls otherwise ignored. Defaults to False.
    """

    self.loc = loc
    self.lastmod = lastmod
    self.urls = []

    if include_urls:
        file_url = get_corrected_url(loc, fix_slash="")
        response = get_remote_content(file_url)
        if response.status_code == 200:
            self.add_from_text(response.text)

add_object(url_object)

add Url Object to current XmlDocument

Parameters:

Name Type Description Default
url_object Url

Url Object.

required
Source code in pysitemaps/__init__.py
def add_object(self, url_object: Url):
    """add Url Object to current XmlDocument

    Args:
        url_object (Url): Url Object.
    """
    if url_object:
        self.urls.append(url_object)

add_url(loc, lastmod=datetime.now().strftime('%Y-%m-%d'), images_loc=[])

add url to current sitemap by specifiyig loc, lastmod and list of images.

Parameters:

Name Type Description Default
loc str

description

required
lastmod str

description. Defaults to datetime.now().strftime(“%Y-%m-%d”).

datetime.now().strftime('%Y-%m-%d')
images_loc list

description. Defaults to [].

[]
Source code in pysitemaps/__init__.py
def add_url(
    self,
    loc: str,
    lastmod: str = datetime.now().strftime("%Y-%m-%d"),
    images_loc: list = [],
) -> None:
    """add url to current sitemap by specifiyig loc, lastmod and list of images.

    Args:
        loc (str): _description_
        lastmod (str, optional): _description_. Defaults to datetime.now().strftime("%Y-%m-%d").
        images_loc (list, optional): _description_. Defaults to [].
    """
    if loc:
        self.urls.append(Url(loc=loc, lastmod=lastmod, images_loc=images_loc))

as_dict()

return XmlDocument as dit

Returns:

Name Type Description
dict dict

contains loc, lastmod of XML Document. It also include urls key containing list of Url(s).

Source code in pysitemaps/__init__.py
def as_dict(self) -> dict:
    """return XmlDocument as dit

    Returns:
        dict: contains loc, lastmod of XML Document. It also include urls key containing list of Url(s).
    """
    return {
        "loc": self.loc,
        "lastmod": self.lastmod,
        "urls": [url.as_dict() for url in self.urls],
    }