|
12 | 12 | def generate_sitemap(): |
13 | 13 | # Load all sitemap entries |
14 | 14 | 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}") |
20 | 38 |
|
21 | 39 | # Generate sitemap XML |
22 | 40 | sitemap = '<?xml version="1.0" encoding="UTF-8"?>\n' |
|
0 commit comments