Created
February 15, 2020 09:42
-
-
Save tjkendev/e7ce68e864b8d39f0d8677920122281a to your computer and use it in GitHub Desktop.
sitemap generators
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
# usage: python3 sitemap-generator.py <base_url> <base_dir> | |
import os, sys, subprocess | |
from datetime import datetime | |
argv = sys.argv | |
base_url = argv[1] | |
base_dir = argv[2] | |
if base_url[-1] == '/': | |
base_url = base_url[:-1] | |
result = ['<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'] | |
for base, dnames, fnames in os.walk(base_dir): | |
relative_base = base[len(base_dir)+1:] | |
for fname in fnames: | |
if not fname.endswith(".html"): | |
continue | |
if base.find("node_modules") > -1: | |
continue | |
path = os.path.join(relative_base, fname) | |
p = subprocess.run(["git", "--no-pager", "log", "--pretty=%at", "-n1", path], cwd=base_dir, stdout=subprocess.PIPE) | |
if not p.stdout: | |
continue | |
url = "{}/{}".format(base_url, path) | |
timestamp = int(p.stdout) | |
dt = datetime.fromtimestamp(timestamp) | |
tss = dt.strftime("%Y-%m-%d") | |
print(url, timestamp, tss, file=sys.stderr) | |
result.append(' <url>') | |
result.append(' <loc>{}</loc>'.format(url)) | |
result.append(' <lastmod>{}</lastmod>'.format(tss)) | |
result.append(' </url>') | |
result.append('</urlset>') | |
print('\n'.join(result)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment