-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-update.sh
executable file
·68 lines (53 loc) · 1.58 KB
/
api-update.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
#!/bin/sh
#
# api update - Generate api file based on template
#
# This script will update node counter based on meshviewer json
# data in a template used for freifunk api.
#
# Copyright Freifunk Erfurt, 2021
# Marcel Pennewiss <[email protected]>
#
# Version: 1.1
# Last-Modified: 2021-08-01
#
# REQUIREMENTS:
# * jq
# CONFIGURATION ################################################
TEMPLATE="template.json"
OUTPUT="/var/www/public_html/freifunk-api.json"
MESHVIEWER_JSON_URL="https://map.erfurt.freifunk.net/meshviewer/data/meshviewer.json"
TEMP_FILE=$(mktemp)
# SCRIPT #######################################################
SCRIPT_PATH=$(realpath "$0" | sed 's|\(.*\)/.*|\1|')
# Download meshviewer json file
get_meshviewer_json() {
# Get Meshviewer file
$(which wget) --quiet $MESHVIEWER_JSON_URL -O "$TEMP_FILE" > /dev/null 2>&1
return $?
}
# Update api file
update_api_file() {
# Get nodes
__NODECOUNT=$(jq '.nodes[] | select(.is_online == true and .is_gateway == false) | .node_id' "$TEMP_FILE" | wc -l)
# Get current date/time
__LASTCHANGE=$(date -u +%FT%TZ)
# Update json template to new file
jq --ascii-output \
--arg NODECOUNT "$__NODECOUNT" \
--arg LASTCHANGE "$__LASTCHANGE" \
'.state.nodes = ($NODECOUNT|tonumber) | .state.lastchange = $LASTCHANGE' \
"$SCRIPT_PATH/$TEMPLATE" > "$OUTPUT.new"
# Move new file to destination file
mv "$OUTPUT.new" "$OUTPUT"
}
# Cleanup temp file
cleanup() {
# Remove existing tempfile
[ -f "$TEMP_FILE" ] && rm "$TEMP_FILE"
}
get_meshviewer_json
if get_meshviewer_json; then
update_api_file
fi
cleanup