Created
May 1, 2023 16:02
-
-
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
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
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