Files
web/scripts/fetch-citations.sh
T

50 lines
1.4 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Fetch CSL-JSON citation metadata for every DOI listed in
# data/publications.yaml and cache it under data/citations/.
#
# The cache is committed to the repo so the site builds offline and
# reproducibly. Re-run this only when you add or change a DOI:
#
# make citations # fetch any not yet cached
# make citations FORCE=1 # re-fetch everything (refresh)
#
set -euo pipefail
cd "$(dirname "$0")/.."
SRC=data/publications.yaml
OUT=data/citations
mkdir -p "$OUT"
# Same slug rule as the Hugo template: lowercase, non-alphanumerics -> "_".
slug() { printf '%s' "$1" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/_/g'; }
mapfile -t dois < <(grep -oP '^\s*-\s*doi:\s*"\K[^"]+' "$SRC")
if [[ ${#dois[@]} -eq 0 ]]; then
echo "No DOIs found in $SRC" >&2
exit 1
fi
for doi in "${dois[@]}"; do
out="$OUT/$(slug "$doi").json"
if [[ -f "$out" && "${FORCE:-}" != "1" ]]; then
echo "cached $doi"
continue
fi
echo "fetch $doi"
tmp=$(mktemp)
if curl -fsSL --retry 3 --retry-delay 2 \
-H "Accept: application/vnd.citationstyles.csl+json" \
"https://doi.org/$doi" -o "$tmp" \
&& python3 -c "import json,sys; json.load(open(sys.argv[1]))" "$tmp" 2>/dev/null; then
mv "$tmp" "$out"
else
rm -f "$tmp"
echo " !! failed to fetch valid CSL-JSON for $doi" >&2
exit 1
fi
done
echo "Done. Cached ${#dois[@]} citation(s) in $OUT/"