Skip to content

Instantly share code, notes, and snippets.

@ssaurel
Created May 1, 2023 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssaurel/96fd261ec23445c03ee69ee5f01944eb to your computer and use it in GitHub Desktop.
Save ssaurel/96fd261ec23445c03ee69ee5f01944eb to your computer and use it in GitHub Desktop.
generatefile method of the SitemapGenerator class for a tutorial on the SSaurel's Blog
def generatefile(self):
urlsbylevel = {}
maxlevel = 0
for key, value in self.urls.items():
if value > maxlevel :
maxlevel = value
listurls = None
if value not in urlsbylevel:
listurls = []
else :
listurls = urlsbylevel[value]
if listurls != None :
listurls.append(key)
urlsbylevel[value] = listurls
# priority between 0 and 1
# calculate the step between each level
step = 1 / (maxlevel * 2)
rootstr = '<urlset></urlset>'
root = ET.fromstring(rootstr)
root.attrib = {'xmlns' : 'http://www.sitemaps.org/schemas/sitemap/0.9'}
for key, value in urlsbylevel.items():
priority = round(1 - step * key, 2)
if priority < 0:
print("Step = " + str(step) + " Key = " + str(key))
for item in value:
url = ET.SubElement(root, "url")
ET.SubElement(url, "loc").text = item
ET.SubElement(url, "priority").text = str(priority)
tree = ET.ElementTree(root)
ET.indent(tree, ' ')
# writing xml
tree.write(self.filename, encoding="utf-8", xml_declaration=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment