From f59dc14d0432784f80e02c5062ac707a670e7f07 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Tue, 11 Jun 2024 11:45:12 -0400 Subject: [PATCH] generate sitemap as part of deployment and automate IndexNow This allows adding the lastmod field based on the last modification date of the file along with automating submission to IndexNow. --- deploy-static | 10 +++++++++ generate-sitemap | 54 ++++++++++++++++++++++++++++++++++++++++++++++ static/sitemap.xml | 45 -------------------------------------- 3 files changed, 64 insertions(+), 45 deletions(-) create mode 100755 generate-sitemap delete mode 100644 static/sitemap.xml diff --git a/deploy-static b/deploy-static index ce664878..9f30482e 100755 --- a/deploy-static +++ b/deploy-static @@ -16,10 +16,16 @@ remote=root@attestation.app # use last modified timestamps from attestation.app rsync -rptcv --chmod=D755,F644 --delete --fsync --preallocate $remote:/srv/attestation.app/ static-production +rsync -pcv --chmod=D755,F644 --fsync --preallocate static-production/sitemap.xml{,.gz,.br} static-tmp/ rsync -rpcv --chmod=D755,F644 --delete --fsync --preallocate static-tmp/ static-production for f in static-production/**.*(br|gz); do touch -r "${f%.*}" "$f" done +changed="$(./generate-sitemap)" +xmllint --noblanks static-tmp/sitemap.xml --output static-tmp/sitemap.xml +brotli -f static-tmp/sitemap.xml +zopfli static-tmp/sitemap.xml +rsync -pcv --chmod=D755,F644 --fsync --preallocate static-tmp/sitemap.xml{,.gz,.br} static-production/ active=$(ssh $remote readlink /srv/attestation.app) @@ -48,3 +54,7 @@ rsync -pcv --chmod=755 --fsync --preallocate remote-backup $remote:/usr/local/bi rsync -pcv --chmod=644 --fsync --preallocate systemd/system/remote-backup.timer $remote:/etc/systemd/system/remote-backup.timer rsync -pcv --chmod=644 --fsync --preallocate systemd/system/remote-backup.service $remote:/etc/systemd/system/remote-backup.service rsync -pcv --chmod=644 --chown attestation:attestation --fsync --preallocate backup-public-key.txt cloud-archive.sh $remote:/var/lib/attestation/ + +if [[ -n "$changed" ]]; then + ./indexnow <<< "$changed" +fi diff --git a/generate-sitemap b/generate-sitemap new file mode 100755 index 00000000..17e51cd5 --- /dev/null +++ b/generate-sitemap @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 + +from datetime import datetime, timezone +from os.path import getmtime +from pathlib import Path + +base = "https://attestation.app" + +pages = [ + ["/", 1.0], + ["/.well-known/security.txt", 0.0], + ["/LICENSE.txt", 0.0], + ["/about", 1.0], + ["/donate", 0.5], + ["/contact", 0.1], + ["/humans.txt", 0.0], + ["/privacy-policy", 0.2], + ["/source", 0.1], + ["/tutorial", 1.0] +] + +base_mtime = getmtime("static-tmp") +entries = [] + +for page in pages: + path = page[0] + loc = base + path + filepath = "static-production" + path + if path[-1] == '/': + filepath += "index.html" + elif "." not in path: + filepath += ".html" + + mtime = getmtime(filepath) + if mtime > base_mtime: + print(loc) + lastmod = datetime.fromtimestamp(mtime, timezone.utc).strftime("%Y-%m-%dT%H:%M:%S%:z") + priority = page[1] + entries.append(f""" + + {loc} + {lastmod} + {priority} + """) + +sitemap = f""" +{"".join(entries)} + +""" + +with open("static-tmp/sitemap.xml", "w") as f: + f.write(sitemap) diff --git a/static/sitemap.xml b/static/sitemap.xml deleted file mode 100644 index e1bba6dc..00000000 --- a/static/sitemap.xml +++ /dev/null @@ -1,45 +0,0 @@ - - - - https://attestation.app/ - 1.0 - - - https://attestation.app/.well-known/security.txt - 0.0 - - - https://attestation.app/LICENSE.txt - 0.0 - - - https://attestation.app/about - 1.0 - - - https://attestation.app/donate - 0.5 - - - https://attestation.app/contact - 0.1 - - - https://attestation.app/humans.txt - 0.0 - - - https://attestation.app/privacy-policy - 0.2 - - - https://attestation.app/source - 0.1 - - - https://attestation.app/tutorial - 1.0 - -