This repository has been archived by the owner on Aug 17, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-mastodon-servers-list.sh
72 lines (64 loc) · 2.49 KB
/
update-mastodon-servers-list.sh
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
70
71
72
#!/bin/bash
# Copyright (C) 2020 Andrew Larson ([email protected])
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# requires a file downloader to work
command -v wget >/dev/null 2>&1 && wget=0 || wget=1
command -v curl >/dev/null 2>&1 && curl=0 || curl=1
if [ $wget -eq 1 ] && [ $curl -eq 1 ]; then
echo "Error: this tool requires either \`wget\` or \`curl\` to be installed." >/dev/stderr
exit 1
fi
# check to make sure we are online
echo -e "GET http://google.com HTTP/1.0\n\n" | nc google.com 80 > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "Error: this tool requires internet access to work."
exit 1
fi
URL="https://instances.social/list/old"
JS_FILE="./js/custom.js"
TEMP_FILE="temp.txt"
# determines how to download based on availability of `wget` and `curl`
dlFile() {
url="$1"
# use wget, otherwise, use curl
if [ $wget -eq 0 ]; then
wget -qO - "$url" > /dev/stdout
elif [ $curl -eq 0 ]; then
curl -sL "$url"
fi
}
# gets sorted array of Mastodon servers (only the ones that are UP; running)
servers=($(dlFile "${URL}" | grep -A 3 -F '<td class="table-success">UP</td>' | grep -oP 'scope="row".*href="https?://\K[^"]+' | sort))
# creates JS object code
echo -en "\nConverting server list to JS object"
echo -n "let mastodonServers = {" > "${TEMP_FILE}"
index=0
for server in "${servers[@]}"
do
# echo progress dots
! (( index % 500 )) && echo -n " ."
# create servers as keys, with their values being their URI encoded counterpart
serverPartialEncoded=${server//./%2E} # encode periods to throw off domain detection on Twitter
echo -n "'$server': '$serverPartialEncoded'" >> "${TEMP_FILE}"
index=$((index+1))
if [ $index -eq ${#servers[@]} ]; then
echo "};" >> "${TEMP_FILE}"
else
echo -n ", " >> "${TEMP_FILE}"
fi
done
# updates custom.js with new mastodon servers if any
sed -i -e '/let mastodonServers \=/{r '"${TEMP_FILE}"'' -e 'd}' "${JS_FILE}"
rm -f "${TEMP_FILE}"
echo -e "\nDone."