Skip to content

Commit cd35ed8

Browse files
committed
update sitemap generator
1 parent 1e94756 commit cd35ed8

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

generate_sitemap.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,29 @@
1212
def generate_sitemap():
1313
# Load all sitemap entries
1414
sitemap_entries = []
15-
for file_name in os.listdir("sitemap_data"):
16-
if file_name.endswith("_sitemap.json"):
17-
with open(f"sitemap_data/{file_name}", "r", encoding="utf-8") as f:
18-
entry = json.load(f)
19-
sitemap_entries.append(entry)
15+
# Iterate through subfolders in the JSON folder
16+
for root, dirs, files in os.walk(JSON_FOLDER):
17+
for file_name in files:
18+
if file_name.endswith(".json"):
19+
json_path = os.path.join(root, file_name)
20+
try:
21+
with open(json_path, "r", encoding="utf-8") as f:
22+
data = json.load(f)
23+
# Extract URL and frequency from the JSON file
24+
url = data.get("url", f"{RAW_BASE_URL}/{os.path.relpath(json_path, JSON_FOLDER)}")
25+
frequency = data.get("frequency", "never") # Default to 'weekly' if not specified
26+
lastmod = datetime.now().strftime("%Y-%m-%d") # Use current date as last modified
27+
28+
# Append the entry to the sitemap
29+
sitemap_entries.append({
30+
"url": url,
31+
"lastmod": lastmod,
32+
"changefreq": frequency
33+
})
34+
except json.JSONDecodeError:
35+
print(f"Error decoding JSON in file: {json_path}")
36+
except Exception as e:
37+
print(f"Unexpected error with file {json_path}: {e}")
2038

2139
# Generate sitemap XML
2240
sitemap = '<?xml version="1.0" encoding="UTF-8"?>\n'

0 commit comments

Comments
 (0)