interplane.io
Source of interplane.io: a Rust generator that renders the page from the protocol repository's public record, deployed hourly from the Spark.
Brought by drakestapleton
interplane.io / scripts / fetch-papers.sh
| 1 | #!/bin/sh |
| 2 | # Writes the papers snapshot (content/papers.snapshot) from the public Zenodo API: every record whose |
| 3 | # creator carries the ORCID iD below. Shell, curl and jq only. No token, no email, a generic |
| 4 | # User-Agent. The site builder never touches the network; it reads the committed snapshot. |
| 5 | # |
| 6 | # sh scripts/fetch-papers.sh OUT query Zenodo and write OUT |
| 7 | # sh scripts/fetch-papers.sh --save DIR OUT the same, and keep the raw responses in DIR |
| 8 | # sh scripts/fetch-papers.sh --replay DIR OUT write OUT from saved responses DIR/response-N.json |
| 9 | # (no network; used by the controls) |
| 10 | # |
| 11 | # Settings: ORCID (default 0009-0002-6890-7111); FETCHED, the date written as the fetch date |
| 12 | # (default: today's date in UTC). |
| 13 | # The output is deterministic: the same responses and the same date give the same bytes. Records are |
| 14 | # sorted newest first (publication date, then record number), files by name, related identifiers by |
| 15 | # relation, scheme and identifier. A character outside printable ASCII is written as \uXXXX, and a |
| 16 | # backslash as \u005c, so the snapshot is plain ASCII; the builder accepts only the escapes it can write. |
| 17 | # Exit 0 written, 1 refused (bad response or a record the snapshot format cannot hold), 2 usage. |
| 18 | set -eu |
| 19 | export LC_ALL=C |
| 20 | ORCID=${ORCID:-0009-0002-6890-7111} |
| 21 | FETCHED=${FETCHED:-$(date -u +%Y-%m-%d)} |
| 22 | API=https://zenodo.org/api/records |
| 23 | SIZE=25 |
| 24 | MODE=fetch |
| 25 | KEEP= |
| 26 | usage() { echo 'usage: fetch-papers.sh [--save DIR | --replay DIR] OUT' >&2; exit 2; } |
| 27 | case ${1:-} in |
| 28 | --save) [ "$#" -eq 3 ] || usage; KEEP=$2; OUT=$3 ;; |
| 29 | --replay) [ "$#" -eq 3 ] || usage; MODE=replay; KEEP=$2; OUT=$3 ;; |
| 30 | -*|'') usage ;; |
| 31 | *) [ "$#" -eq 1 ] || usage; OUT=$1 ;; |
| 32 | esac |
| 33 | refuse() { echo "fetch-papers: refused: $1" >&2; exit 1; } |
| 34 | printf '%s\n' "$ORCID" | grep -Eq '^[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{3}[0-9X]$' || refuse "ORCID iD is malformed: $ORCID" |
| 35 | printf '%s\n' "$FETCHED" | grep -Eq '^[0-9]{4}-[0-9]{2}-[0-9]{2}$' || refuse "FETCHED is not YYYY-MM-DD: $FETCHED" |
| 36 | command -v jq > /dev/null 2>&1 || refuse 'jq is needed' |
| 37 | W=$(mktemp -d) |
| 38 | trap 'rm -rf "$W"' 0 |
| 39 | trap 'exit 1' 1 2 15 |
| 40 | |
| 41 | # ---- the raw responses, one file per page |
| 42 | if [ "$MODE" = replay ]; then |
| 43 | [ -f "$KEEP/response-1.json" ] || refuse "no saved response: $KEEP/response-1.json" |
| 44 | n=1 |
| 45 | while [ -f "$KEEP/response-$n.json" ]; do cp "$KEEP/response-$n.json" "$W/response-$n.json"; n=$((n + 1)); done |
| 46 | else |
| 47 | command -v curl > /dev/null 2>&1 || refuse 'curl is needed' |
| 48 | page=1; got=0 |
| 49 | while :; do |
| 50 | [ "$page" -le 40 ] || refuse 'more than 40 pages of results' |
| 51 | code=$(curl -sS --max-time 60 -A 'curl/8' -H 'Accept: application/json' -G "$API" \ |
| 52 | --data-urlencode "q=creators.orcid:\"$ORCID\"" --data-urlencode "size=$SIZE" \ |
| 53 | --data-urlencode "page=$page" -o "$W/response-$page.json" -w '%{http_code}') || refuse "request for page $page failed" |
| 54 | [ "$code" = 200 ] || refuse "Zenodo answered $code for page $page" |
| 55 | total=$(jq -e '.hits.total' "$W/response-$page.json") || refuse "page $page is not a search response" |
| 56 | hits=$(jq '.hits.hits | length' "$W/response-$page.json") |
| 57 | got=$((got + hits)) |
| 58 | if [ "$hits" -eq 0 ] || [ "$got" -ge "$total" ]; then break; fi |
| 59 | page=$((page + 1)) |
| 60 | done |
| 61 | if [ -n "$KEEP" ]; then mkdir -p "$KEEP"; cp "$W"/response-*.json "$KEEP/"; fi |
| 62 | fi |
| 63 | |
| 64 | # ---- the fingerprint of each raw response, in page order |
| 65 | : > "$W/head" |
| 66 | n=1 |
| 67 | while [ -f "$W/response-$n.json" ]; do |
| 68 | jq -e '.hits.hits | type == "array"' "$W/response-$n.json" > /dev/null || refuse "response $n is not a search response" |
| 69 | printf 'response %d sha256:%s\n' "$n" "$(sha256sum < "$W/response-$n.json" | cut -d' ' -f1)" >> "$W/head" |
| 70 | n=$((n + 1)) |
| 71 | done |
| 72 | |
| 73 | # ---- the records, in a fixed field order |
| 74 | # shellcheck disable=SC2016 |
| 75 | jq -r -s --arg orcid "$ORCID" ' |
| 76 | def hex4: [(./4096|floor)%16, (./256|floor)%16, (./16|floor)%16, .%16] |
| 77 | | map("0123456789abcdef"[.:.+1]) | join(""); |
| 78 | def ascii: tostring | explode |
| 79 | | map(if . == 92 then "\\u005c" |
| 80 | elif . < 32 or . > 126 then |
| 81 | (if . > 65535 then error("a character above U+FFFF") else "\\u" + hex4 end) |
| 82 | else [.] | implode end) | join(""); |
| 83 | def need(f): if f == null or f == "" then error("a record lacks a field") else f end; |
| 84 | [.[] | .hits.hits[]] | unique_by(.id) |
| 85 | | sort_by([.metadata.publication_date, .id]) | reverse |
| 86 | | (length | tostring) as $n |
| 87 | | "records " + $n, |
| 88 | (.[] | . as $r | $r.metadata as $m |
| 89 | | "", |
| 90 | "record " + need($r.doi), |
| 91 | "concept " + need($r.conceptdoi), |
| 92 | "title " + (need($m.title) | ascii), |
| 93 | "date " + need($m.publication_date), |
| 94 | "type " + (need($m.resource_type.title) | ascii), |
| 95 | (if ($m.version // "") != "" then "version " + ($m.version | ascii) else empty end), |
| 96 | "license " + (need($m.license.id) | ascii), |
| 97 | ($m.creators[] | "creator " + (.orcid // "none") + " " + (need(.name) | ascii)), |
| 98 | "orcid_attached " + (if any($m.creators[]; .orcid == $orcid) then "yes" else "no" end), |
| 99 | ($r.files | sort_by(.key)[] |
| 100 | | if (.key | test("^[A-Za-z0-9._-]+$")) then . else error("a file name outside A-Z a-z 0-9 . _ -") end |
| 101 | | "file " + .key + " " + (.size | tostring) + " " + .checksum), |
| 102 | (($m.related_identifiers // []) | sort_by([.relation, .scheme, .identifier])[] |
| 103 | | "related " + (.relation | ascii) + " " + (.scheme | ascii) + " " + (.identifier | ascii))) |
| 104 | ' "$W"/response-*.json > "$W/body" 2> "$W/jqerr" || refuse "$(cat "$W/jqerr")" |
| 105 | |
| 106 | { |
| 107 | printf '# Zenodo records whose creator carries ORCID iD %s.\n' "$ORCID" |
| 108 | printf '# Written by scripts/fetch-papers.sh from the public API; do not edit by hand.\n' |
| 109 | printf '# Query: %s?q=creators.orcid:"%s"&size=%s&page=N\n' "$API" "$ORCID" "$SIZE" |
| 110 | printf '# response N: SHA-256 of the raw bytes of page N. Checksums are as Zenodo reports them.\n' |
| 111 | printf 'orcid %s\n' "$ORCID" |
| 112 | printf 'fetched %s\n' "$FETCHED" |
| 113 | cat "$W/head" "$W/body" |
| 114 | } > "$W/out" |
| 115 | mv "$W/out" "$OUT" |
| 116 | printf 'fetch-papers: %s records, %s responses, written to %s\n' \ |
| 117 | "$(grep -c '^record ' "$OUT")" "$(grep -c '^response ' "$OUT")" "$OUT" |