Skip to content

Commit

Permalink
Create sitemap.py
Browse files Browse the repository at this point in the history
  • Loading branch information
LukePrior authored Oct 10, 2024
1 parent 76a6d9d commit 0b8db53
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions code/sitemap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import xml.etree.ElementTree as ET
from datetime import datetime
import utils

def generate_sitemap(json_file, output_file):
# Load suburbs data from JSON file
data = utils.read_json_file(json_file)

# Create the root element for the XML sitemap
urlset = ET.Element('urlset', xmlns="http://www.sitemaps.org/schemas/sitemap-image/1.1")

# Add the static stats page without a lastmod tag
add_url(urlset, "https://nbn.lukeprior.com/stats")

# Generate URLs for each suburb
for state, suburbs in data.items():
for suburb in suburbs:
suburb_name = suburb['name']
processed_date = suburb['processed_date']
url = f"https://nbn.lukeprior.com/?suburb={suburb_name.lower().replace(' ', '-')}&state={state.lower()}"
add_url(urlset, url, processed_date)

# Convert the XML tree to a string
sitemap_xml = ET.tostring(urlset, encoding='utf-8', xml_declaration=True).decode('utf-8')

# Save the XML to a file
with open(output_file, 'w') as f:
f.write(sitemap_xml)

def add_url(urlset, loc, lastmod=None):
url = ET.SubElement(urlset, 'url')
loc_element = ET.SubElement(url, 'loc')
loc_element.text = loc
if lastmod:
lastmod_element = ET.SubElement(url, 'lastmod')
lastmod_element.text = lastmod

# Example usage
generate_sitemap('results/combined-suburbs.json', 'site/sitemap.xml')

0 comments on commit 0b8db53

Please sign in to comment.