diff options
author | neodarz <neodarz@neodarz.net> | 2019-05-26 15:49:38 +0200 |
---|---|---|
committer | neodarz <neodarz@neodarz.net> | 2019-05-26 15:49:38 +0200 |
commit | c777eefdd0a9caceebd46237cd4170d9e07c8d2a (patch) | |
tree | bc27f5170e27c84867ab0b69e91ccf7621bb1de7 /generators | |
parent | 7e3060cc02aacecd13ba220cf69d1b8ae2517e09 (diff) | |
download | my_new_personal_website-c777eefdd0a9caceebd46237cd4170d9e07c8d2a.tar.xz my_new_personal_website-c777eefdd0a9caceebd46237cd4170d9e07c8d2a.zip |
Move generate_sitemap to external file
Diffstat (limited to '')
-rw-r--r-- | generators/generators.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/generators/generators.py b/generators/generators.py index 06a34743..8887bddb 100644 --- a/generators/generators.py +++ b/generators/generators.py @@ -11,8 +11,12 @@ import dateutil import io import subprocess +import lxml.etree as ET + from config.config import * +from utils import utils + def generate_menu(): """Generate menu.""" @@ -348,3 +352,34 @@ def generate_index(feed): except subprocess.CalledProcessError: sys.stderr.write("error: failed to generate index.html\n") os.remove(tmppath) + +def generate_sitemap(feed): + """Generate sitemap.xml.""" + sitemap = ET.Element("urlset", xmlns="http://www.sitemaps.org/schemas/sitemap/0.9") + # index + sitemap.append(utils.make_sitemap_url_element(BLOG_HOME, feed.updated, "daily", 1.0)) + # other top level pages + for name in os.listdir(BUILDDIR): + if (not name.endswith(".html") or name == "index.html" or + re.match("google[a-z0-9]+\.html", name)): # exclude Google's site ownership verification file + continue + link = urllib.parse.urljoin(BLOG_HOME, name) + fullpath = os.path.join(BUILDDIR, name) + # try to extract updated time + updated = None + with open(fullpath, encoding="utf-8") as htmlobj: + soup = bs4.BeautifulSoup(htmlobj.read(), "lxml") + if soup.footer is not None: + updated_tag = soup.footer.find(attrs={"class": "updated"}) + if updated_tag is not None: + updated = dateutil.parser.parse(updated_tag.text) + sitemap.append(utils.make_sitemap_url_element(link, updated, "monthly", 0.9)) + + # blog entries + for entry in feed.entries: + sitemap.append(utils.make_sitemap_url_element(entry.link, entry.updated, "monthly", 0.9)) + sitemappath = os.path.join(BUILDDIR, "sitemap.xml") + with open(sitemappath, "w", encoding="utf-8") as sitemapfile: + sitemapfile.write('<?xml version="1.0" encoding="UTF-8"?>\n%s\n' % + ET.tostring(sitemap).decode('utf-8')) + sys.stderr.write("wrote sitemap.xml\n") |