sitemapr 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sitemapr-0.1.0/LICENSE +21 -0
- sitemapr-0.1.0/PKG-INFO +84 -0
- sitemapr-0.1.0/README.md +67 -0
- sitemapr-0.1.0/pyproject.toml +53 -0
- sitemapr-0.1.0/sitemapr/__init__.py +4 -0
- sitemapr-0.1.0/sitemapr/core.py +125 -0
- sitemapr-0.1.0/sitemapr/models.py +26 -0
sitemapr-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Seonu Jang
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
sitemapr-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: sitemapr
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: sitemapr is a library that generates sitemaps for SPA websites with declaritve configuration in Python.
|
|
5
|
+
License: MIT
|
|
6
|
+
Author: sjquant
|
|
7
|
+
Author-email: seonujang92@gmail.com
|
|
8
|
+
Requires-Python: >=3.11,<4.0
|
|
9
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Requires-Dist: pydantic (>=2.6.3,<3.0.0)
|
|
14
|
+
Requires-Dist: typer (>=0.9.0,<0.10.0)
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# sitemapr
|
|
18
|
+
|
|
19
|
+
`sitemapr` is a Python library designed to generate and save sitemaps for websites. It allows for the creation of detailed sitemaps with customizable parameters, making it easier for search engines to crawl and index web pages efficiently.
|
|
20
|
+
|
|
21
|
+
## Features
|
|
22
|
+
|
|
23
|
+
- Generate sitemaps with dynamic URL parameters.
|
|
24
|
+
- Split large sitemaps into chunks to comply with sitemap index specifications.
|
|
25
|
+
- Customizable base URLs for sitemaps and websites.
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
28
|
+
|
|
29
|
+
SiteMapr can be easily installed using pip. Ensure you have pip installed and run the following command:
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
pip install sitemapr
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
This command will download and install SiteMapr along with its dependencies.
|
|
36
|
+
|
|
37
|
+
## Quick Start
|
|
38
|
+
|
|
39
|
+
Here's how to quickly generate a sitemap for your website using SiteMapr:
|
|
40
|
+
|
|
41
|
+
1. **Define Your Pages**: First, define the pages you want to include in your sitemap, including any dynamic path or query parameters.
|
|
42
|
+
|
|
43
|
+
2. **Create a SiteMapr Instance**: Initialize SiteMapr with your website's base URL and the pages you've defined.
|
|
44
|
+
|
|
45
|
+
3. **Save Your Sitemap**: Choose a directory and save your sitemap, specifying chunk sizes if needed.
|
|
46
|
+
|
|
47
|
+
### Example
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
from sitemapr import Page, Param, SiteMapr
|
|
51
|
+
|
|
52
|
+
# Define the pages of your site
|
|
53
|
+
pages = [
|
|
54
|
+
Page(
|
|
55
|
+
path="",
|
|
56
|
+
query_params=[
|
|
57
|
+
Param(name="page", values=["home", "about", "contact"]),
|
|
58
|
+
Param(name="sort", values=["asc", "desc"]),
|
|
59
|
+
],
|
|
60
|
+
),
|
|
61
|
+
Page(
|
|
62
|
+
path="/blog",
|
|
63
|
+
query_params=[
|
|
64
|
+
Param(name="page", values=["1", "2", "3"]),
|
|
65
|
+
Param(name="sort", values=["asc", "desc"]),
|
|
66
|
+
],
|
|
67
|
+
),
|
|
68
|
+
Page(
|
|
69
|
+
path="/blog/{id}",
|
|
70
|
+
path_params=[Param(name="id", values=["1", "2", "3"])],
|
|
71
|
+
),
|
|
72
|
+
]
|
|
73
|
+
|
|
74
|
+
# Initialize SiteMapr with your website's base URL and the defined pages
|
|
75
|
+
sitemapr = SiteMapr(base_url="https://example.com", pages=pages)
|
|
76
|
+
|
|
77
|
+
# Save the sitemap to the specified directory
|
|
78
|
+
sitemapr.save("/path/to/your/sitemap/directory")
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## License
|
|
82
|
+
|
|
83
|
+
`sitemapr` is released under the MIT License. See the LICENSE file for more details.
|
|
84
|
+
|
sitemapr-0.1.0/README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# sitemapr
|
|
2
|
+
|
|
3
|
+
`sitemapr` is a Python library designed to generate and save sitemaps for websites. It allows for the creation of detailed sitemaps with customizable parameters, making it easier for search engines to crawl and index web pages efficiently.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Generate sitemaps with dynamic URL parameters.
|
|
8
|
+
- Split large sitemaps into chunks to comply with sitemap index specifications.
|
|
9
|
+
- Customizable base URLs for sitemaps and websites.
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
SiteMapr can be easily installed using pip. Ensure you have pip installed and run the following command:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
pip install sitemapr
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
This command will download and install SiteMapr along with its dependencies.
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
Here's how to quickly generate a sitemap for your website using SiteMapr:
|
|
24
|
+
|
|
25
|
+
1. **Define Your Pages**: First, define the pages you want to include in your sitemap, including any dynamic path or query parameters.
|
|
26
|
+
|
|
27
|
+
2. **Create a SiteMapr Instance**: Initialize SiteMapr with your website's base URL and the pages you've defined.
|
|
28
|
+
|
|
29
|
+
3. **Save Your Sitemap**: Choose a directory and save your sitemap, specifying chunk sizes if needed.
|
|
30
|
+
|
|
31
|
+
### Example
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
from sitemapr import Page, Param, SiteMapr
|
|
35
|
+
|
|
36
|
+
# Define the pages of your site
|
|
37
|
+
pages = [
|
|
38
|
+
Page(
|
|
39
|
+
path="",
|
|
40
|
+
query_params=[
|
|
41
|
+
Param(name="page", values=["home", "about", "contact"]),
|
|
42
|
+
Param(name="sort", values=["asc", "desc"]),
|
|
43
|
+
],
|
|
44
|
+
),
|
|
45
|
+
Page(
|
|
46
|
+
path="/blog",
|
|
47
|
+
query_params=[
|
|
48
|
+
Param(name="page", values=["1", "2", "3"]),
|
|
49
|
+
Param(name="sort", values=["asc", "desc"]),
|
|
50
|
+
],
|
|
51
|
+
),
|
|
52
|
+
Page(
|
|
53
|
+
path="/blog/{id}",
|
|
54
|
+
path_params=[Param(name="id", values=["1", "2", "3"])],
|
|
55
|
+
),
|
|
56
|
+
]
|
|
57
|
+
|
|
58
|
+
# Initialize SiteMapr with your website's base URL and the defined pages
|
|
59
|
+
sitemapr = SiteMapr(base_url="https://example.com", pages=pages)
|
|
60
|
+
|
|
61
|
+
# Save the sitemap to the specified directory
|
|
62
|
+
sitemapr.save("/path/to/your/sitemap/directory")
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## License
|
|
66
|
+
|
|
67
|
+
`sitemapr` is released under the MIT License. See the LICENSE file for more details.
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "sitemapr"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "sitemapr is a library that generates sitemaps for SPA websites with declaritve configuration in Python."
|
|
5
|
+
authors = ["sjquant <seonujang92@gmail.com>"]
|
|
6
|
+
license = "MIT"
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
|
|
9
|
+
[tool.poetry.dependencies]
|
|
10
|
+
python = "^3.11"
|
|
11
|
+
typer = "^0.9.0"
|
|
12
|
+
pydantic = "^2.6.3"
|
|
13
|
+
|
|
14
|
+
[tool.poetry.group.dev.dependencies]
|
|
15
|
+
ruff = "^0.2.2"
|
|
16
|
+
pyright = "^1.1.351"
|
|
17
|
+
pytest = "^8.0.2"
|
|
18
|
+
|
|
19
|
+
[build-system]
|
|
20
|
+
requires = ["poetry-core"]
|
|
21
|
+
build-backend = "poetry.core.masonry.api"
|
|
22
|
+
|
|
23
|
+
[tool.ruff]
|
|
24
|
+
line-length = 100
|
|
25
|
+
fix = true
|
|
26
|
+
exclude = [
|
|
27
|
+
'*.pyi',
|
|
28
|
+
"site-packages",
|
|
29
|
+
".mypy_cache",
|
|
30
|
+
".ruff_cache",
|
|
31
|
+
]
|
|
32
|
+
target-version = "py311"
|
|
33
|
+
|
|
34
|
+
[tool.ruff.lint]
|
|
35
|
+
select = [
|
|
36
|
+
"E", # pycodestyle errors
|
|
37
|
+
"W", # pycodestyle warnings
|
|
38
|
+
"F", # pyflakes
|
|
39
|
+
"I", # isort
|
|
40
|
+
"B", # flake8-bugbear
|
|
41
|
+
"C4", # flake8-comprehensions
|
|
42
|
+
"UP", # pyupgrade
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
[tool.pyright]
|
|
46
|
+
include = ["src"]
|
|
47
|
+
exclude = ["**/node_modules",
|
|
48
|
+
"**/__pycache__",
|
|
49
|
+
"src/experimental",
|
|
50
|
+
"src/typestubs"
|
|
51
|
+
]
|
|
52
|
+
pythonVersion = "3.11"
|
|
53
|
+
typeCheckingMode = "strict"
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
from collections.abc import Iterator
|
|
2
|
+
from io import TextIOWrapper
|
|
3
|
+
from itertools import product
|
|
4
|
+
from urllib.parse import urlencode
|
|
5
|
+
|
|
6
|
+
from sitemapr.models import Page, Param, SiteMapUrl
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class SiteMapr:
|
|
10
|
+
"""
|
|
11
|
+
A class for generating and saving sitemaps.
|
|
12
|
+
|
|
13
|
+
Args:
|
|
14
|
+
base_url: The base URL of the website.
|
|
15
|
+
pages: A list of Page objects representing the pages to include in the sitemap.
|
|
16
|
+
sitemap_base_url: The base URL for the sitemap. Defaults to None, which uses the base_url.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
def __init__(
|
|
20
|
+
self, base_url: str, pages: list[Page], *, sitemap_base_url: str | None = None
|
|
21
|
+
):
|
|
22
|
+
self._base_url = base_url
|
|
23
|
+
self._sitemap_base_url = sitemap_base_url or base_url
|
|
24
|
+
self._pages = pages
|
|
25
|
+
|
|
26
|
+
def save(self, dirname: str, *, chunk_size: int = 50000) -> None:
|
|
27
|
+
"""
|
|
28
|
+
Save the sitemap to the specified directory.
|
|
29
|
+
|
|
30
|
+
Args:
|
|
31
|
+
dirname: The directory path where the sitemap will be saved.
|
|
32
|
+
chunk_size: The number of URLs to include in each chunk. Defaults to 50000.
|
|
33
|
+
"""
|
|
34
|
+
chunk: list[SiteMapUrl] = []
|
|
35
|
+
idx = 0
|
|
36
|
+
for url in self.iter_urls():
|
|
37
|
+
if len(chunk) == chunk_size:
|
|
38
|
+
self._save_chunk(dirname, idx, chunk)
|
|
39
|
+
idx += 1
|
|
40
|
+
chunk.clear()
|
|
41
|
+
|
|
42
|
+
chunk.append(url)
|
|
43
|
+
|
|
44
|
+
if not chunk:
|
|
45
|
+
return
|
|
46
|
+
|
|
47
|
+
if idx == 0:
|
|
48
|
+
with open(f"{dirname}/sitemap.xml", "w") as f:
|
|
49
|
+
self._write_urls(f, chunk)
|
|
50
|
+
else:
|
|
51
|
+
self._save_chunk(dirname, idx, chunk)
|
|
52
|
+
|
|
53
|
+
if idx > 0:
|
|
54
|
+
self._write_index_file(dirname, idx)
|
|
55
|
+
|
|
56
|
+
def _save_chunk(self, dirname: str, idx: int, chunk: list[SiteMapUrl]) -> None:
|
|
57
|
+
with open(f"{dirname}/sitemap-{idx}.xml", "w") as f:
|
|
58
|
+
self._write_urls(f, chunk)
|
|
59
|
+
|
|
60
|
+
def _write_index_file(self, dirname: str, idx: int) -> None:
|
|
61
|
+
with open(f"{dirname}/sitemap.xml", "w") as f:
|
|
62
|
+
f.write(
|
|
63
|
+
'<?xml version="1.0" encoding="UTF-8"?><sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
|
|
64
|
+
)
|
|
65
|
+
for i in range(idx + 1):
|
|
66
|
+
f.write(
|
|
67
|
+
f"<sitemap><loc>{self._sitemap_base_url}/sitemap-{i}.xml</loc></sitemap>"
|
|
68
|
+
)
|
|
69
|
+
f.write("</sitemapindex>")
|
|
70
|
+
|
|
71
|
+
def _write_urls(self, f: TextIOWrapper, urls: list[SiteMapUrl]):
|
|
72
|
+
f.write(
|
|
73
|
+
'<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
|
|
74
|
+
)
|
|
75
|
+
for url in urls:
|
|
76
|
+
f.write(f"<url><loc>{url.loc}</loc>")
|
|
77
|
+
if url.lastmod:
|
|
78
|
+
f.write(f"<lastmod>{url.lastmod}</lastmod>")
|
|
79
|
+
if url.changefreq:
|
|
80
|
+
f.write(f"<changefreq>{url.changefreq}</changefreq>")
|
|
81
|
+
if url.priority:
|
|
82
|
+
f.write(f"<priority>{url.priority}</priority>")
|
|
83
|
+
f.write("</url>")
|
|
84
|
+
f.write("</urlset>")
|
|
85
|
+
|
|
86
|
+
def iter_urls(self) -> Iterator[SiteMapUrl]:
|
|
87
|
+
"""
|
|
88
|
+
Iterates over the URLs in the sitemap.
|
|
89
|
+
|
|
90
|
+
Yields:
|
|
91
|
+
SiteMapUrl: A SiteMapUrl object representing a URL in the sitemap.
|
|
92
|
+
"""
|
|
93
|
+
for page in self._pages:
|
|
94
|
+
yield from self._iter_page(page)
|
|
95
|
+
|
|
96
|
+
def _iter_page(self, page: Page) -> Iterator[SiteMapUrl]:
|
|
97
|
+
query_param_combinations = self._get_param_combinations(page.query_params)
|
|
98
|
+
path_param_combinations: list[dict[str, str]] = self._get_param_combinations(
|
|
99
|
+
page.path_params
|
|
100
|
+
)
|
|
101
|
+
for query_params, path_params in product(
|
|
102
|
+
query_param_combinations, path_param_combinations
|
|
103
|
+
):
|
|
104
|
+
path = page.path.format(**path_params)
|
|
105
|
+
query_string = urlencode(query_params)
|
|
106
|
+
loc = (
|
|
107
|
+
f"{self._base_url}{path}?{query_string}"
|
|
108
|
+
if query_string
|
|
109
|
+
else f"{self._base_url}{path}"
|
|
110
|
+
)
|
|
111
|
+
yield SiteMapUrl(loc=loc)
|
|
112
|
+
|
|
113
|
+
def _get_param_combinations(
|
|
114
|
+
self, params: list[Param] | None
|
|
115
|
+
) -> list[dict[str, str]]:
|
|
116
|
+
if not params:
|
|
117
|
+
return [{}]
|
|
118
|
+
|
|
119
|
+
combinations: list[dict[str, str]] = []
|
|
120
|
+
for values in product(*[param.values for param in params]):
|
|
121
|
+
combination = {
|
|
122
|
+
param.name: value for param, value in zip(params, values, strict=False)
|
|
123
|
+
}
|
|
124
|
+
combinations.append(combination)
|
|
125
|
+
return combinations
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
from typing import Literal
|
|
2
|
+
|
|
3
|
+
from pydantic import BaseModel
|
|
4
|
+
|
|
5
|
+
ChangeFreq = Literal[
|
|
6
|
+
"always", "hourly", "daily", "weekly", "monthly", "yearly", "never"
|
|
7
|
+
]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class Param(BaseModel):
|
|
11
|
+
name: str
|
|
12
|
+
values: list[str] = []
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class Page(BaseModel):
|
|
16
|
+
path: str
|
|
17
|
+
query_params: list[Param] = []
|
|
18
|
+
path_params: list[Param] = []
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class SiteMapUrl(BaseModel):
|
|
22
|
+
# Refer to https://developers.google.com/search/docs/crawling-indexing/sitemaps/build-sitemap?hl=ko#xml
|
|
23
|
+
loc: str
|
|
24
|
+
lastmod: str | None = None
|
|
25
|
+
changefreq: ChangeFreq | None = None # Google ignores this
|
|
26
|
+
priority: float | None = None # Google ignores this
|