Skip to content

Commit

Permalink
Merge pull request #399 from LukePrior/sitemap-new-fix
Browse files Browse the repository at this point in the history
Sitemap new fix
  • Loading branch information
LukePrior authored Oct 10, 2024
2 parents 56329c8 + 55dbaaa commit 53f0a61
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 45 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/auto.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ jobs:
run: |
python code/main.py -i -t 60
- name: Generate Sitemap
run: |
python code/sitemap.py
- name: Commit changes
uses: EndBug/add-and-commit@v9
with:
Expand All @@ -45,4 +49,4 @@ jobs:
if: success() || failure()
uses: peter-evans/repository-dispatch@v3
with:
event-type: auto-suburb
event-type: auto-suburb
30 changes: 0 additions & 30 deletions .github/workflows/sitemap.yml

This file was deleted.

30 changes: 16 additions & 14 deletions code/sitemap.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
import xml.etree.ElementTree as ET
from datetime import datetime
import utils
from urllib.parse import quote

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")
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']
encoded_suburb = quote(suburb_name.lower().replace(' ', '-'))
processed_date = suburb['processed_date']
suburb_name = suburb["name"]
encoded_suburb = quote(suburb_name.lower().replace(" ", "-"))
processed_date = suburb["processed_date"]
url = f"https://nbn.lukeprior.com/?suburb={encoded_suburb}&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')
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:
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')
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 = ET.SubElement(url, "lastmod")
lastmod_element.text = lastmod

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

generate_sitemap("results/combined-suburbs.json", "site/sitemap.xml")

0 comments on commit 53f0a61

Please sign in to comment.