-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsitemap.py
78 lines (64 loc) · 2.4 KB
/
sitemap.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# sitemap.py
import json
from datetime import datetime, timezone
# File paths
mods_file_path = 'mods.json'
other_file_path = 'other.json'
sitemap_file_path = 'itemsitemap.xml'
def update_sitemap(item_id=None):
""" Updates the sitemap for a specific item or regenerates the full sitemap if item_id is None. """
def extract_ids(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
data = json.load(file)
return [item['id'] for item in data]
# Extract IDs from JSON files
mods_ids = extract_ids(mods_file_path)
other_ids = extract_ids(other_file_path)
all_ids = sorted(mods_ids + other_ids, key=lambda x: int(x))
# Base URL
base_url = "https://sumtranslate.netlify.app/item.html?id="
# Get current time
current_time = datetime.now(timezone.utc).isoformat()
# Read existing sitemap
try:
with open(sitemap_file_path, 'r', encoding='utf-8') as file:
existing_sitemap = file.read()
except FileNotFoundError:
existing_sitemap = ""
# If an item_id is provided, update only that entry
if item_id:
new_entry = f"""<url>
<loc>{base_url}{item_id}</loc>
<lastmod>{current_time}</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>"""
if f"<loc>{base_url}{item_id}</loc>" in existing_sitemap:
# Replace the existing entry
updated_sitemap = existing_sitemap.replace(
f"<loc>{base_url}{item_id}</loc>", new_entry
)
else:
# Append new entry
updated_sitemap = existing_sitemap.replace(
"</urlset>", f"{new_entry}\n</urlset>"
)
else:
# Regenerate the whole sitemap
sitemap_entries = [
f"""<url>
<loc>{base_url}{item}</loc>
<lastmod>{current_time}</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>"""
for item in all_ids
]
updated_sitemap = f"""<?xml version='1.0' encoding='UTF-8'?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{''.join(sitemap_entries)}
</urlset>"""
# Write back to file
with open(sitemap_file_path, 'w', encoding='utf-8') as file:
file.write(updated_sitemap)
print(f"Sitemap {'updated' if item_id else 'regenerated'} successfully!")