-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsiteMapCreate.py
More file actions
47 lines (35 loc) · 1.6 KB
/
siteMapCreate.py
File metadata and controls
47 lines (35 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import os
from datetime import datetime
# Configuration
BASE_URL = "https://rpseaman.com/"
ROOT_DIR = "./" # The directory containing your .html files
OUTPUT_FILE = "public/sitemap.xml"
EXTENSIONS = (".html", ".htm")
def generate_sitemap():
pages = []
# Walk through the directory to find html files
for root, _, files in os.walk(ROOT_DIR):
for file in files:
if file.endswith(EXTENSIONS):
# Construct the path
path = os.path.join(root, file).replace(ROOT_DIR, "").replace("\\", "/")
# Format URL (e.g., index.html becomes just the base URL)
if path == "index.html":
url = BASE_URL
else:
url = f"{BASE_URL}{path}"
# Get last modification time
mod_time = os.path.getmtime(os.path.join(root, file))
lastmod = datetime.fromtimestamp(mod_time).strftime('%Y-%m-%d')
pages.append((url, lastmod))
# Build the XML string
xml_content = '<?xml version="1.0" encoding="UTF-8"?>\n'
xml_content += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n'
for url, lastmod in pages:
xml_content += f" <url>\n <loc>{url}</loc>\n <lastmod>{lastmod}</lastmod>\n <priority>0.8</priority>\n </url>\n"
xml_content += "</urlset>"
with open(OUTPUT_FILE, "w") as f:
f.write(xml_content)
print(f"Success! {OUTPUT_FILE} generated with {len(pages)} URLs.")
if __name__ == "__main__":
generate_sitemap()