-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #399 from LukePrior/sitemap-new-fix
Sitemap new fix
- Loading branch information
Showing
3 changed files
with
21 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |