diff options
author | tomsmeding <tom.smeding@gmail.com> | 2016-01-13 14:19:08 +0100 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2016-01-13 14:19:08 +0100 |
commit | 150f751aba6fb7b6697a65441d10ba29b96df021 (patch) | |
tree | 769c5b0b60a7a58fc7a462348053c99ba7c7f2ca |
Initial
-rw-r--r-- | .gitignore | 3 | ||||
-rwxr-xr-x | functions.sh | 104 | ||||
-rwxr-xr-x | gettemp.sh | 7 | ||||
-rwxr-xr-x | inctemp.sh | 3 | ||||
-rwxr-xr-x | login.sh | 3 | ||||
-rwxr-xr-x | logout.sh | 3 |
6 files changed, 123 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..168ee3e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +credentials.txt +cookiejar.txt +csrftoken.txt diff --git a/functions.sh b/functions.sh new file mode 100755 index 0000000..857ae55 --- /dev/null +++ b/functions.sh @@ -0,0 +1,104 @@ +#!/usr/bin/env bash +function flogin() { + rm -f cookiejar.txt csrftoken.txt + local authtoken + authtoken="$(curl -s https://www.e-thermostaat.nl | grep -o -m 1 '<input name="authenticity_token.*\?/>' | sed 's/.*value="//g' | sed 's/".*$//g' | sed 's/=/%3D/g')" + declare -a creds + creds=($(fgetcredentials)) + curl -s -c cookiejar.txt 'https://www.e-thermostaat.nl/process_login?locale=nl&secure=true' -d "utf8=%E2%9C%93&authenticity_token=$authtoken&login=${creds[0]}&password=${creds[1]}&commit=Inloggen" >/dev/null + fgetcsrftoken >csrftoken.txt +} + +function flogout() { + curl -s -c cookiejar.txt -b cookiejar.txt 'https://www.e-thermostaat.nl/logout' >/dev/null + rm -f cookiejar.txt csrftoken.txt +} + +function fgetcredentials() { + #credentials.txt should be something like "username password". + #code currently can't handle spaces in either. + cat credentials.txt +} + +function fensurelogin() { + # fgetthermpage >/dev/null #STUB + curl -s -I -c cookiejar.txt -b cookiejar.txt 'https://www.e-thermostaat.nl/pages/index' | head -n 1 | grep -m 1 200 >/dev/null + if test $? -ne 0; then + flogin + fi +} + +function fgetthermpage() { + local page + while true; do + page="$(curl -s -c cookiejar.txt -b cookiejar.txt 'https://www.e-thermostaat.nl/pages/thermostat')" + if test -z "$(grep -o -m 1 redirected <<<"$page")"; then + break + fi + flogin + done + cat <<<"$page" +} + +function fgetcsrftoken() { + fgetthermpage | grep '<meta' | grep 'csrf-token' | sed 's/.*content="\([^"]*\)".*/\1/' +} + +function fcsrftoken() { + if ! test -e csrftoken.txt; then + flogin + fi + cat csrftoken.txt +} + +function fcurlwithtoken() { + curl -s -b cookiejar.txt -c cookiejar.txt -H "X-CSRF-token: $(fcsrftoken)" -e 'https://www.e-thermostaat.nl/pages/index' $@ +} + +#returns currenttemp targettemp. +#capture with declare -a temp; temp=($(fgettemp)); echo ${temp[0]} ${temp[1]} +function fgettemp() { + local page images templateimgs tempcontext tempimgs tempsrcs tempstr currenttemp targettemp + page="$(fgetthermpage)" + images="$(grep -o "<img.*\?/>" <<<"$page")" + templateimgs="$(grep 'height="1"' <<<"$images")" + declare -A srcalt #associative array + for templateimg in $(tr -d ' ' <<<"$templateimgs"); do + src="$(sed 's/.*src="\([^"]*\)".*/\1/' <<<"$templateimg")" + alt="$(sed 's/.*alt="\([^"]*\)".*/\1/' <<<"$templateimg" | tr , .)" + srcalt["$src"]="$alt" + done + tempcontext="$(grep -C 3 '<div \+id *= *"temperature">' <<<"$page")" + tempimgs="$(grep -o '<img[^>]*>' <<<"$tempcontext" | grep -v "Thermostaat mode" | grep 'height="35"')" + tempsrcs="$(sed 's/.*src="\([^"]*\)".*/\1/' <<<"$tempimgs")" + tempstr="" + for tempsrc in $tempsrcs; do + tempstr+="${srcalt["$tempsrc"]}" + done + currenttemp="${tempstr:0:4}" + targettemp="${tempstr:4:4}" + echo "$currenttemp" "$targettemp" +} + +#takes one argument, the temperature increment in floating point format. +#the system only accepts multiples of 0.5, though +function finctemp() { + if ! [[ "$1" =~ ^-?[0-9]*(\.[05])?$ ]]; then + return 1 + fi + fensurelogin + fcurlwithtoken -d "increment=$1" 'https://www.e-thermostaat.nl/processing/temperature' >/dev/null +} + + +function fissourced() { + test ${BASH_SOURCE[0]} != $0 +} + +if ! fissourced; then + if test $# -eq 0; then + echo "functions.sh is meant to be sourced (or called with parameters, but that's only for advanced people)" + else + eval $@ + fi +fi diff --git a/gettemp.sh b/gettemp.sh new file mode 100755 index 0000000..6283876 --- /dev/null +++ b/gettemp.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +source functions.sh + +declare -a temp +temp=($(fgettemp)) + +echo "current: ${temp[0]}; target: ${temp[1]}" diff --git a/inctemp.sh b/inctemp.sh new file mode 100755 index 0000000..e139e4d --- /dev/null +++ b/inctemp.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +source functions.sh +finctemp $@ || exit 1 diff --git a/login.sh b/login.sh new file mode 100755 index 0000000..69ad12b --- /dev/null +++ b/login.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +source functions.sh +flogin diff --git a/logout.sh b/logout.sh new file mode 100755 index 0000000..aeed34b --- /dev/null +++ b/logout.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +source functions.sh +flogout |