Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Save sitemap #4

Merged
merged 1 commit into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions sitemapr/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ def __init__(self, base_url: str, pages: list[Page]):
self._base_url = base_url
self._pages = pages

def save(self, path: str) -> None:
with open(path, "w") as f:
f.write(
'<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
)
for url in self.iter_urls():
f.write(f"<url><loc>{url.loc}</loc>")
if url.lastmod:
f.write(f"<lastmod>{url.lastmod}</lastmod>")
if url.changefreq:
f.write(f"<changefreq>{url.changefreq}</changefreq>")
if url.priority:
f.write(f"<priority>{url.priority}</priority>")
f.write("</url>")
f.write("</urlset>")

def iter_urls(self) -> Iterator[SiteMapUrl]:
for page in self._pages:
yield from self._iter_page(page)
Expand Down
40 changes: 40 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pathlib

from sitemapr import Page, Param, SiteMapr, SiteMapUrl


Expand Down Expand Up @@ -124,3 +126,41 @@ def test_sut_works():
),
]
assert actuals == expected


def test_save(tmp_path: pathlib.Path):
# given
base_url = "https://example.com"
pages = [
Page(
path="",
query_params=[
Param(name="page", values=["home", "about", "contact"]),
Param(name="sort", values=["asc", "desc"]),
],
),
Page(
path="/blog",
query_params=[
Param(name="page", values=["1", "2", "3"]),
Param(name="sort", values=["asc", "desc"]),
],
),
Page(
path="/blog/{id}",
path_params=[Param(name="id", values=["1", "2", "3"])],
),
]
sitemapr = SiteMapr(base_url=base_url, pages=pages)

# when
save_path = tmp_path / "sitemap.xml"
sitemapr.save(str(save_path))

# then
with open(save_path) as f:
content = f.read()
assert (
content
== '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>https://example.com?page=home&sort=asc</loc></url><url><loc>https://example.com?page=home&sort=desc</loc></url><url><loc>https://example.com?page=about&sort=asc</loc></url><url><loc>https://example.com?page=about&sort=desc</loc></url><url><loc>https://example.com?page=contact&sort=asc</loc></url><url><loc>https://example.com?page=contact&sort=desc</loc></url><url><loc>https://example.com/blog?page=1&sort=asc</loc></url><url><loc>https://example.com/blog?page=1&sort=desc</loc></url><url><loc>https://example.com/blog?page=2&sort=asc</loc></url><url><loc>https://example.com/blog?page=2&sort=desc</loc></url><url><loc>https://example.com/blog?page=3&sort=asc</loc></url><url><loc>https://example.com/blog?page=3&sort=desc</loc></url><url><loc>https://example.com/blog/1</loc></url><url><loc>https://example.com/blog/2</loc></url><url><loc>https://example.com/blog/3</loc></url></urlset>'
)