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 /functions.sh |
Initial
Diffstat (limited to 'functions.sh')
-rwxr-xr-x | functions.sh | 104 |
1 files changed, 104 insertions, 0 deletions
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 |