forked from mempool/mempool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sitemap-update
executable file
·69 lines (50 loc) · 2.7 KB
/
sitemap-update
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/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 '<?xml version="1.0" encoding="UTF-8"?>' > "${SITEMAP_TEMP_FILE}"
echo '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' >> "${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 '<?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}"
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 '<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}"
fi
echo '<sitemap>' >> "${SITEMAP_TEMP_FILE}"
echo "<loc>https://mempool.space/${SITEMAP_FILE}</loc>" >> "${SITEMAP_TEMP_FILE}"
echo "<lastmod>${SITEMAP_LASTMOD}</lastmod>" >> "${SITEMAP_TEMP_FILE}"
echo '</sitemap>' >> "${SITEMAP_TEMP_FILE}"
((FILE++))
done
echo '</sitemapindex>' >> "${SITEMAP_TEMP_FILE}"
mv "${SITEMAP_TEMP_FILE}" "sitemap.xml"