|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# ─── configuration ─── |
| 5 | +BASE_URL="https://mainnet.mirrornode.hedera.com/api/v1/network/nodes?file.id=0.0.102&limit=100&order=asc" |
| 6 | +PAGE2_URL="${BASE_URL}&node.id=gt:27" |
| 7 | +DOC_FILE="networks/mainnet/mainnet-nodes/README.md" |
| 8 | + |
| 9 | +TABLE_A_START="<!-- TABLE A START -->" |
| 10 | +TABLE_A_END="<!-- TABLE A END -->" |
| 11 | +TABLE_B_START="<!-- TABLE B START -->" |
| 12 | +TABLE_B_END="<!-- TABLE B END -->" |
| 13 | + |
| 14 | +# ─── fetch page 1 and page 2 ─── |
| 15 | +resp1=$(curl -sS --fail --max-time 10 "$BASE_URL") |
| 16 | +resp2=$(curl -sS --fail --max-time 10 "$PAGE2_URL") |
| 17 | + |
| 18 | +# write each nodes array to a temp file |
| 19 | +tmp1=$(mktemp) |
| 20 | +tmp2=$(mktemp) |
| 21 | +jq '.nodes' <<<"$resp1" > "$tmp1" |
| 22 | +jq '.nodes' <<<"$resp2" > "$tmp2" |
| 23 | + |
| 24 | +# merge into one array |
| 25 | +all_nodes=$(jq -s '.[0] + .[1]' "$tmp1" "$tmp2") |
| 26 | + |
| 27 | +# clean up |
| 28 | +rm "$tmp1" "$tmp2" |
| 29 | + |
| 30 | + |
| 31 | +node_count=$(jq 'length' <<<"$all_nodes") |
| 32 | +echo "ℹ️ found $node_count total nodes after merging pages" |
| 33 | +[ "$node_count" -eq 0 ] && { echo "⚠️ no nodes found; aborting"; exit 1; } |
| 34 | + |
| 35 | +nodes_json="$all_nodes" |
| 36 | + |
| 37 | +# ─── build Table A (node_id ≤ 15) ─── |
| 38 | +tableA_header="| Node | Node ID | Node Account ID | Endpoints | Node Certificate Thumbprint | |
| 39 | +|------|---------|-----------------|-----------|-----------------------------| |
| 40 | +" |
| 41 | +tableA_rows=$(jq -r ' |
| 42 | + map(select(.node_id >= 0)) |
| 43 | + | map({ |
| 44 | + node: (.description | capture("Hosted by (?<node>[^|]+) \\|").node), |
| 45 | + id: .node_id, |
| 46 | + acct: .node_account_id, |
| 47 | + endpoints: [.service_endpoints[] | "\(.ip_address_v4):\(.port)"], |
| 48 | + thumb: .node_cert_hash |
| 49 | + }) |
| 50 | + | sort_by(.id) |
| 51 | + | .[] |
| 52 | + | "| \(.node) | \(.id) | \(.acct) | \(.endpoints | join(", ")) | \(.thumb) |" |
| 53 | +' <<<"$nodes_json") |
| 54 | +tableA_content="${tableA_header}${tableA_rows}" |
| 55 | + |
| 56 | +# ─── build Table B (node_id > 15) ─── |
| 57 | +tableB_header="| Node Account ID | Public Key | |
| 58 | +|-----------------|-----------------------------| |
| 59 | +" |
| 60 | +tableB_rows=$(jq -r ' |
| 61 | + map(select(.node_id >= 0)) |
| 62 | + | sort_by(.node_id) |
| 63 | + | map({ |
| 64 | + acct: .node_account_id, |
| 65 | + key: .public_key |
| 66 | + }) |
| 67 | + | .[] |
| 68 | + | "| \(.acct) | \(.key) |" |
| 69 | +' <<<"$nodes_json") |
| 70 | +tableB_content="${tableB_header}${tableB_rows}" |
| 71 | + |
| 72 | +# ─── injection helper via awk ─── |
| 73 | +inject_table() { |
| 74 | + local file="$1" start="$2" end="$3" table_file="$4" |
| 75 | + cp "$file" "${file}.bak" |
| 76 | + awk -v start="$start" -v end="$end" -v tf="$table_file" ' |
| 77 | + $0 == start { print; while ((getline line < tf) > 0) print line; inblock=1; next } |
| 78 | + $0 == end { inblock=0; print; next } |
| 79 | + !inblock { print } |
| 80 | + ' "$file" > "${file}.tmp" && mv "${file}.tmp" "$file" |
| 81 | +} |
| 82 | + |
| 83 | +# ─── write and inject Table A ─── |
| 84 | +TABLE_A_FILE=$(mktemp) |
| 85 | +printf '%s\n' "$tableA_content" > "$TABLE_A_FILE" |
| 86 | +inject_table "$DOC_FILE" "$TABLE_A_START" "$TABLE_A_END" "$TABLE_A_FILE" |
| 87 | +rm "$TABLE_A_FILE" |
| 88 | + |
| 89 | +# ─── write and inject Table B ─── |
| 90 | +TABLE_B_FILE=$(mktemp) |
| 91 | +printf '%s\n' "$tableB_content" > "$TABLE_B_FILE" |
| 92 | +inject_table "$DOC_FILE" "$TABLE_B_START" "$TABLE_B_END" "$TABLE_B_FILE" |
| 93 | +rm "$TABLE_B_FILE" |
| 94 | + |
| 95 | +echo "✅ Updated $DOC_FILE with merged pages and two tables" |
0 commit comments