summary refs log tree commit diff
path: root/bin
diff options
context:
space:
mode:
Diffstat (limited to 'bin')
-rwxr-xr-xbin/hspl102
1 files changed, 102 insertions, 0 deletions
diff --git a/bin/hspl b/bin/hspl
new file mode 100755
index 0000000..3059400
--- /dev/null
+++ b/bin/hspl
@@ -0,0 +1,102 @@
+#!/bin/bash
+
+set -e
+[ -n "$DEBUG" ] && set -x
+
+print_gateway_uri=
+print_time_diff=
+print_json=
+while getopts djuh opt; do
+	case "$opt" in
+	d) print_time_diff=1 ;;
+	j) print_json=1 ;;
+	u) print_gateway_uri=1 ;;
+	h | ?)
+		printf "usage: hspl [options]\n"
+		printf "\n"
+		printf "supported hotspot vendors:\n"
+		printf "    hotsplots.de (Espresso House Berlin)\n"
+		printf "    pbspot (CoffeeFellows Berlin)\n"
+		printf "\n"
+		printf "options:\n"
+		printf "  -d   print time until expiry in seconds\n"
+		printf "  -j   output json\n"
+		printf "  -u   print gateway address\n"
+		printf "\n"
+		exit 2
+		;;
+	esac
+done
+shift $((OPTIND - 1))
+
+if=wlan0
+
+gateway=$(ip route show dev "$if" | awk '/default via/{print $3}')
+
+if [ -n "$print_gateway_uri" ]; then
+	echo "$gateway"
+	exit 0
+fi
+
+html=$(curl -Ls --interface "$if" "$gateway")
+
+t=
+tx=
+rx=
+vendor=
+
+# hotspot vendor implementations go here:
+
+if grep -q pbspot <<<"$html"; then
+	vendor=pbspot
+	t=$(grep -i 'expires' <<<"$html" | awk -F 'td>' '{print $4}' | tr -d '</' | tr -d ':' | sed -e 's,s, seconds,' -e 's,m, minutes,')
+	tx=$(grep -i 'transmitted' <<<"$html" | awk -F 'td>' '{print $4}' | tr -d '</')
+	rx=$(grep -i 'received' <<<"$html" | awk -F 'td>' '{print $4}' | tr -d '</')
+fi
+
+if grep -q hotsplots <<<"$html"; then
+	vendor=hotsplots
+	html=$(sed 's,\/tr>,\/tr>\n,g' <<<"$html")
+	t=$(grep -i 'verbleibende zeit' <<<"$html" | awk -F 'td>' '{print $3}' | tr -d '</' | sed -e 's,&nbsp,,' -e 's,h, hours,' -e 's,min, minutes,' -e 's,sec, seconds,')
+	tx=$(grep -i 'übertragene daten' <<<"$html" | awk -F 'td>' '{print $3}' | tr -d '</' | sed -e 's,&nbsp,,' -e 's, (.*),,')
+	rx="-"
+fi
+
+# end of hotspot vendor implementations
+
+# no hotspot or unimplemented vendor
+if [ -z "$t" ]; then
+	exit 1
+fi
+
+expiry_time=$(date --date "now + $t" +%s)
+remaining=$(((expiry_time - $(date +%s))))
+
+if [ -n "$print_time_diff" ]; then
+	echo "$remaining"
+	exit 0
+fi
+
+# format remaining time like: 5h30m or 10s
+remaining=$(awk '{
+	s=$0
+	h=(s/60/60);
+	if(h>1){ printf "%dh", h }
+	m=(s/60%60)
+	if(m>1){ printf "%dm", m }
+	if(h<1 && m<1){ printf "%ds", $0 }
+}' <<<"$remaining")
+
+if [ -n "$print_json" ]; then
+	jq -n --args '{
+		remaining:$ARGS.positional[0],
+		tx:$ARGS.positional[1],
+		rx:$ARGS.positional[2],
+		t:$ARGS.positional[3],
+		vendor:$ARGS.positional[4]
+	}' "$remaining" "$tx" "$rx" "$t" "$vendor"
+else
+	echo "$remaining"
+	echo "TX:" "$tx"
+	echo "RX:" "$rx"
+fi