Add script to generate sitemap links for each block hash

This commit is contained in:
wiz 2020-03-27 07:48:48 +09:00
parent 1d0dc5c418
commit 028bcbf53e
No known key found for this signature in database
GPG Key ID: A394E332255A6173

57
sitemap Executable file
View File

@ -0,0 +1,57 @@
#!/usr/bin/env zsh
BLOCK_TIP_HEIGHT="$(curl -s https://mempool.space/electrs/mainnet/blocks/tip/height)"
BLOCK_HEIGHT="${BLOCK_TIP_HEIGHT}"
BLOCKS_PER_FILE=1000
FILE_TOTAL="$(($BLOCK_TIP_HEIGHT / $BLOCKS_PER_FILE))"
FILE_START="${FILE_TOTAL}"
FILE_STOP="$(($FILE_TOTAL - 10))"
FILE="${FILE_START}"
echo "${BLOCK_TIP_HEIGHT} blocks"
echo "${BLOCKS_PER_FILE} blocks per sitemap"
echo "${FILE_TOTAL} total sitemaps"
echo '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' > sitemap.xml
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/,.*//')
until [ "${FILE}" = "${FILE_STOP}" ];do
SITEMAP_FILE="sitemap-${FILE}.xml"
SITEMAP_LASTMOD=$(date -r "${BLOCK_TIMESTAMP}" +"%Y-%m-%dT%H:%M:%SZ")
echo "generating ${SITEMAP_FILE} from block ${BLOCK_HEIGHT}"
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' > "${SITEMAP_FILE}"
BLOCK_STOP="$(($BLOCK_HEIGHT - ${BLOCKS_PER_FILE}))"
until [ "${BLOCK_HEIGHT}" = "${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 '<url>' >> "${SITEMAP_FILE}"
echo "<loc>https://mempool.space/block/${BLOCK_HASH}</loc>" >> "${SITEMAP_FILE}"
echo "<lastmod>${BLOCK_LASTMOD}</lastmod>" >> "${SITEMAP_FILE}"
echo '</url>' >> "${SITEMAP_FILE}"
((BLOCK_HEIGHT--))
done
echo '</urlset>' >> "${SITEMAP_FILE}"
gzip "${SITEMAP_FILE}"
echo '<sitemap>' >> sitemap.xml
echo "<loc>https://mempool.space/${SITEMAP_FILE}</loc>" >> sitemap.xml
echo "<lastmod>${SITEMAP_LASTMOD}</lastmod>" >> sitemap.xml
echo '</sitemap>' >> sitemap.xml
((FILE--))
done
echo '</sitemapindex>' >> sitemap.xml