2020-03-26 23:48:48 +01:00
|
|
|
#!/usr/bin/env zsh
|
2020-03-27 01:00:42 +01:00
|
|
|
SITEMAP_TEMP_FILE=temp.xml
|
|
|
|
|
2020-03-26 23:48:48 +01:00
|
|
|
BLOCK_TIP_HEIGHT="$(curl -s https://mempool.space/electrs/mainnet/blocks/tip/height)"
|
2020-03-27 00:11:07 +01:00
|
|
|
BLOCK_HEIGHT=0
|
2020-03-27 01:00:42 +01:00
|
|
|
BLOCK_PER_FILE=100
|
2020-03-26 23:48:48 +01:00
|
|
|
|
2020-03-27 00:11:07 +01:00
|
|
|
FILE_TOTAL="$(($BLOCK_TIP_HEIGHT / $BLOCK_PER_FILE))"
|
2020-03-27 00:33:08 +01:00
|
|
|
FILE_START=1
|
2020-03-27 00:11:07 +01:00
|
|
|
FILE_STOP="${FILE_TOTAL}"
|
2020-03-26 23:48:48 +01:00
|
|
|
FILE="${FILE_START}"
|
|
|
|
|
|
|
|
echo "${BLOCK_TIP_HEIGHT} blocks"
|
2020-03-27 00:11:07 +01:00
|
|
|
echo "${BLOCK_PER_FILE} blocks per sitemap"
|
2020-03-26 23:48:48 +01:00
|
|
|
echo "${FILE_TOTAL} total sitemaps"
|
|
|
|
|
2020-03-27 01:26:54 +01:00
|
|
|
echo '<?xml version="1.0" encoding="UTF-8"?>' > "${SITEMAP_TEMP_FILE}"
|
|
|
|
echo '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' >> "${SITEMAP_TEMP_FILE}"
|
2020-03-26 23:48:48 +01:00
|
|
|
|
2020-03-27 00:11:07 +01:00
|
|
|
until [ "${FILE}" = "${FILE_STOP}" ];do
|
|
|
|
|
2020-03-27 01:00:42 +01:00
|
|
|
BLOCK_HEIGHT=$((($FILE * $BLOCK_PER_FILE) - 1))
|
|
|
|
if [ "${BLOCK_HEIGHT}" -gt "${BLOCK_TIP_HEIGHT}" ];then
|
|
|
|
BLOCK_HEIGHT="${BLOCK_TIP_HEIGHT}"
|
|
|
|
fi
|
2020-03-26 23:48:48 +01:00
|
|
|
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/,.*//')
|
|
|
|
|
2020-03-27 15:04:46 +01:00
|
|
|
SITEMAP_FILE=$(printf "sitemap/%05d.xml" "${FILE}")
|
2020-03-26 23:48:48 +01:00
|
|
|
SITEMAP_LASTMOD=$(date -r "${BLOCK_TIMESTAMP}" +"%Y-%m-%dT%H:%M:%SZ")
|
|
|
|
|
2020-03-27 01:26:54 +01:00
|
|
|
if [ ! -f "${SITEMAP_FILE}" ] || [ "${BLOCK_HEIGHT}" -gt $(($BLOCK_TIP_HEIGHT - 2000)) ];then
|
2020-03-26 23:48:48 +01:00
|
|
|
|
2020-03-27 01:00:42 +01:00
|
|
|
BLOCK_STOP="$((($BLOCK_HEIGHT - ${BLOCK_PER_FILE}) + 1))"
|
2020-03-27 00:33:08 +01:00
|
|
|
echo "generating ${SITEMAP_FILE} for block ${BLOCK_STOP} ~ ${BLOCK_HEIGHT}"
|
2020-03-26 23:48:48 +01:00
|
|
|
|
2020-03-27 01:26:54 +01:00
|
|
|
echo '<?xml version="1.0" encoding="UTF-8"?>' > "${SITEMAP_FILE}"
|
|
|
|
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}"
|
2020-03-26 23:48:48 +01:00
|
|
|
|
2020-03-27 01:00:42 +01:00
|
|
|
while [ "${BLOCK_HEIGHT}" -ge "${BLOCK_STOP}" ];do
|
2020-03-26 23:48:48 +01:00
|
|
|
|
2020-03-27 00:33:08 +01:00
|
|
|
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
|
2020-03-26 23:48:48 +01:00
|
|
|
|
2020-03-27 00:33:08 +01:00
|
|
|
echo '</urlset>' >> "${SITEMAP_FILE}"
|
|
|
|
fi
|
2020-03-26 23:48:48 +01:00
|
|
|
|
2020-03-27 01:00:42 +01:00
|
|
|
echo '<sitemap>' >> "${SITEMAP_TEMP_FILE}"
|
2020-03-27 01:26:54 +01:00
|
|
|
echo "<loc>https://mempool.space/${SITEMAP_FILE}</loc>" >> "${SITEMAP_TEMP_FILE}"
|
2020-03-27 01:00:42 +01:00
|
|
|
echo "<lastmod>${SITEMAP_LASTMOD}</lastmod>" >> "${SITEMAP_TEMP_FILE}"
|
|
|
|
echo '</sitemap>' >> "${SITEMAP_TEMP_FILE}"
|
2020-03-26 23:48:48 +01:00
|
|
|
|
2020-03-27 00:11:07 +01:00
|
|
|
((FILE++))
|
2020-03-26 23:48:48 +01:00
|
|
|
|
|
|
|
done
|
|
|
|
|
2020-03-27 01:00:42 +01:00
|
|
|
echo '</sitemapindex>' >> "${SITEMAP_TEMP_FILE}"
|
|
|
|
mv "${SITEMAP_TEMP_FILE}" "sitemap.xml"
|