#!/usr/bin/env zsh SITEMAP_TEMP_FILE=temp.xml BLOCK_TIP_HEIGHT="$(curl -s https://mempool.space/electrs/mainnet/blocks/tip/height)" BLOCK_HEIGHT=0 BLOCK_PER_FILE=100 FILE_TOTAL="$(($BLOCK_TIP_HEIGHT / $BLOCK_PER_FILE))" FILE_START=1 FILE_STOP="${FILE_TOTAL}" FILE="${FILE_START}" echo "${BLOCK_TIP_HEIGHT} blocks" echo "${BLOCK_PER_FILE} blocks per sitemap" echo "${FILE_TOTAL} total sitemaps" echo '' > "${SITEMAP_TEMP_FILE}" echo '' >> "${SITEMAP_TEMP_FILE}" until [ "${FILE}" = "${FILE_STOP}" ];do BLOCK_HEIGHT=$((($FILE * $BLOCK_PER_FILE) - 1)) if [ "${BLOCK_HEIGHT}" -gt "${BLOCK_TIP_HEIGHT}" ];then BLOCK_HEIGHT="${BLOCK_TIP_HEIGHT}" fi BLOCK_HASH=$(curl -s https://mempool.space/electrs/mainnet/block-height/${BLOCK_HEIGHT}) BLOCK=$(curl -s https://mempool.space/electrs/mainnet/block/${BLOCK_HASH}) BLOCK_TIMESTAMP=$(echo "${BLOCK}"|sed -e 's/.*timestamp.://' -e 's/,.*//') SITEMAP_FILE=$(printf "sitemap-%05d.xml" "${FILE}") SITEMAP_LASTMOD=$(date -r "${BLOCK_TIMESTAMP}" +"%Y-%m-%dT%H:%M:%SZ") if [ ! -f "${SITEMAP_FILE}" ] || [ "${BLOCK_HEIGHT}" -gt $(($BLOCK_TIP_HEIGHT - 2000)) ];then BLOCK_STOP="$((($BLOCK_HEIGHT - ${BLOCK_PER_FILE}) + 1))" echo "generating ${SITEMAP_FILE} for block ${BLOCK_STOP} ~ ${BLOCK_HEIGHT}" echo '' > "${SITEMAP_FILE}" echo '' >> "${SITEMAP_FILE}" while [ "${BLOCK_HEIGHT}" -ge "${BLOCK_STOP}" ];do BLOCK_HASH=$(curl -s https://mempool.space/electrs/mainnet/block-height/${BLOCK_HEIGHT}) BLOCK=$(curl -s https://mempool.space/electrs/mainnet/block/${BLOCK_HASH}) BLOCK_TIMESTAMP=$(echo "${BLOCK}"|sed -e 's/.*timestamp.://' -e 's/,.*//') BLOCK_LASTMOD=$(date -r "${BLOCK_TIMESTAMP}" +"%Y-%m-%dT%H:%M:%SZ") echo '' >> "${SITEMAP_FILE}" echo "https://mempool.space/block/${BLOCK_HASH}" >> "${SITEMAP_FILE}" echo "${BLOCK_LASTMOD}" >> "${SITEMAP_FILE}" echo '' >> "${SITEMAP_FILE}" ((BLOCK_HEIGHT--)) done echo '' >> "${SITEMAP_FILE}" fi echo '' >> "${SITEMAP_TEMP_FILE}" echo "https://mempool.space/${SITEMAP_FILE}" >> "${SITEMAP_TEMP_FILE}" echo "${SITEMAP_LASTMOD}" >> "${SITEMAP_TEMP_FILE}" echo '' >> "${SITEMAP_TEMP_FILE}" ((FILE++)) done echo '' >> "${SITEMAP_TEMP_FILE}" mv "${SITEMAP_TEMP_FILE}" "sitemap.xml"