From 29bccefec4d8d86bb96ae9c8a2453f9529d55886 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 10 Jan 2018 08:24:36 +0100 Subject: [PATCH] added Bluetooth device list - added echo-show to ALL group, TuneIn station can now be up to 6 digits - added bluetooth list function --- alexa_remote_control.sh | 43 ++++++++++++++++++++++++++++------- alexa_remote_control_plain.sh | 33 +++++++++++++++++++++++---- 2 files changed, 63 insertions(+), 13 deletions(-) diff --git a/alexa_remote_control.sh b/alexa_remote_control.sh index 9899601..e6bf134 100644 --- a/alexa_remote_control.sh +++ b/alexa_remote_control.sh @@ -17,12 +17,15 @@ # 2017-12-18: v0.7c fixed US version # 2017-12-19: v0.7d fixed AWK csrf extraction on some systems # 2017-12-20: v0.7e moved get_devlist after check_status +# 2018-01-08: v0.7f added echo-show to ALL group, TuneIn station can now be up to 6 digits +# 2018-01-08: v0.8 added bluetooth list function +# 2018-01-10: v0.8a abort when login was unsuccessful # ### # # (no BASHisms were used, should run with any shell) # - requires cURL for web communication -# - sed and awk for extraction +# - (GNU) sed and awk for extraction # - jq as command line JSON parser (optional for the fancy bits) # ########################################## @@ -74,10 +77,10 @@ BLUETOOTH="" usage() { - echo "$0 [-d |ALL] -e > | -b [<\"AA:BB:CC:DD:EE:FF\">] | -q | -r <\"station name\"|stationid> | -s | -t |" + echo "$0 [-d |ALL] -e > | -b [list|<\"AA:BB:CC:DD:EE:FF\">] | -q | -r <\"station name\"|stationid> | -s | -t |" echo " -u | -v | -w | -i | -p | -P | -S | -a | -m [device_1 .. device_X] | -l | -h" echo " -e : run command" - echo " -b : connect/disconnect bluetooth device" + echo " -b : connect/disconnect/list bluetooth device" echo " -q : query queue" echo " -r : play tunein radio" echo " -s : play library track" @@ -145,7 +148,7 @@ while [ "$#" -gt 0 ] ; do STATIONID=$2 shift # stationIDs are "s1234" or "s12345" - if [ -n "${STATIONID##s[0-9][0-9][0-9][0-9]}" -a -n "${STATIONID##s[0-9][0-9][0-9][0-9][0-9]}" ] ; then + if [ -n "${STATIONID##s[0-9][0-9][0-9][0-9]}" -a -n "${STATIONID##s[0-9][0-9][0-9][0-9][0-9]}" -a -n "${STATIONID##s[0-9][0-9][0-9][0-9][0-9][0-9]}" ] ; then # search for station name STATIONID=$(${CURL} ${OPTS} -s --data-urlencode "query=${STATIONID}" -G "https://api.tunein.com/profiles?fullTextSearch=true" | jq -r '.Items[] | select(.ContainerType == "Stations") | .Children[] | select( .Index==1 ) | .GuideId') if [ -z "$STATIONID" ] ; then @@ -359,7 +362,7 @@ set_var() if [ -z "${DEVICE}" ] ; then # if no device was supplied, use the first Echo(dot) in device list echo "setting default device to:" - DEVICE=$(jq -r '[ .devices[] | select(.deviceFamily == "ECHO" ) | .accountName] | .[0]' ${DEVLIST}) + DEVICE=$(jq -r '[ .devices[] | select(.deviceFamily == "ECHO" or .deviceFamily == "KNIGHT" ) | .accountName] | .[0]' ${DEVLIST}) echo ${DEVICE} fi @@ -573,6 +576,17 @@ ${CURL} ${OPTS} -s -b ${COOKIE} -A "Mozilla/5.0" -H "DNT: 1" -H "Connection: kee "https://${ALEXA}/api/lemur/tail" } +# +# list bluetooth devices +# +list_bluetooth() +{ +${CURL} ${OPTS} -s -b ${COOKIE} -A "Mozilla/5.0" -H "DNT: 1" -H "Connection: keep-alive" -L\ + -H "Content-Type: application/json; charset=UTF-8" -H "Referer: https://alexa.${AMAZON}/spa/index.html" -H "Origin: https://alexa.${AMAZON}"\ + -H "csrf: $(awk "\$0 ~/.${AMAZON}.*csrf[ \\s\\t]+/ {print \$7}" ${COOKIE})" -X GET \ + "https://${ALEXA}/api/bluetooth?cached=false" | jq --arg serial "${DEVICESERIALNUMBER}" -r '.bluetoothStates[] | select(.deviceSerialNumber == $serial) | "\(.pairedDeviceList[]?.address) \(.pairedDeviceList[]?.friendlyName)"' +} + # # connect bluetooth device # @@ -641,7 +655,7 @@ fi if [ -n "$COMMAND" -o -n "$QUEUE" ] ; then if [ "${DEVICE}" = "ALL" ] ; then - for DEVICE in $(jq -r '.devices[] | select( .deviceFamily == "ECHO" or .deviceFamily == "WHA") | .accountName' ${DEVLIST} | sed -r 's/ /%20/g') ; do + for DEVICE in $(jq -r '.devices[] | select( .deviceFamily == "ECHO" or .deviceFamily == "KNIGHT" or .deviceFamily == "WHA") | .accountName' ${DEVLIST} | sed -r 's/ /%20/g') ; do set_var if [ -n "$COMMAND" ] ; then echo "sending cmd:${COMMAND} to dev:${DEVICE} type:${DEVICETYPE} serial:${DEVICESERIALNUMBER}" @@ -680,11 +694,24 @@ elif [ -n "$LEMUR" ] ; then rm -f ${DEVLIST} get_devlist elif [ -n "$BLUETOOTH" ] ; then - set_var - if [ "$BLUETOOTH" = "null" ] ; then + if [ "$BLUETOOTH" = "list" -o "$BLUETOOTH" = "List" -o "$BLUETOOTH" = "LIST" ] ; then + if [ "${DEVICE}" = "ALL" ] ; then + for DEVICE in $(jq -r '.devices[] | select( .deviceFamily == "ECHO" or .deviceFamily == "KNIGHT" or .deviceFamily == "WHA") | .accountName' ${DEVLIST} | sed -r 's/ /%20/g') ; do + set_var + echo "bluetooth devices for dev:${DEVICE} type:${DEVICETYPE} serial:${DEVICESERIALNUMBER}:" + list_bluetooth + done + else + set_var + echo "bluetooth devices for dev:${DEVICE} type:${DEVICETYPE} serial:${DEVICESERIALNUMBER}:" + list_bluetooth + fi + elif [ "$BLUETOOTH" = "null" ] ; then + set_var echo "disconnecting dev:${DEVICE} type:${DEVICETYPE} serial:${DEVICESERIALNUMBER} from bluetooth" disconnect_bluetooth else + set_var echo "connecting dev:${DEVICE} type:${DEVICETYPE} serial:${DEVICESERIALNUMBER} to bluetooth device:${BLUETOOTH}" connect_bluetooth fi diff --git a/alexa_remote_control_plain.sh b/alexa_remote_control_plain.sh index f354419..b3323f7 100644 --- a/alexa_remote_control_plain.sh +++ b/alexa_remote_control_plain.sh @@ -3,13 +3,13 @@ # Amazon Alexa Remote Control (PLAIN shell) # alex(at)loetzimmer.de # -# 2017-12-20: v0.7e(for updates see http://blog.loetzimmer.de/2017/10/amazon-alexa-hort-auf-die-shell-echo.html) +# 2018-01-10: v0.8a (for updates see http://blog.loetzimmer.de/2017/10/amazon-alexa-hort-auf-die-shell-echo.html) # ### # # (no BASHisms were used, should run with any shell) # - requires cURL for web communication -# - sed and awk for extraction +# - (GNU) sed and awk for extraction # ########################################## @@ -60,11 +60,11 @@ BLUETOOTH="" usage() { - echo "$0 [-d ] -e > | -b [<\"AA:BB:CC:DD:EE:FF\">] | -q | -r | -s | -t |" + echo "$0 [-d ] -e > | -b [list|<\"AA:BB:CC:DD:EE:FF\">] | -q | -r | -s | -t |" echo " -u | -v | -w | -a | -m [device_1 .. device_X] | -l | -h" echo " -e : run command" echo " -q : query queue" - echo " -b : connect/disconnect bluetooth device" + echo " -b : connect/disconnect/list bluetooth device" echo " -r : play tunein radio" echo " -s : play library track" echo " -t : play Prime playlist" @@ -348,7 +348,7 @@ set_var() # if no device was supplied, use the first Echo(dot) in device list IDX=0 for I in $FAMILY ; do - if [ "$I" = "ECHO" ] ; then + if [ "$I" = "ECHO" -o "$I" = "KNIGHT" ] ; then break; fi IDX=$((IDX+1)) @@ -539,6 +539,17 @@ ${CURL} ${OPTS} -s -b ${COOKIE} -A "Mozilla/5.0" -H "DNT: 1" -H "Connection: kee "https://${ALEXA}/api/lemur/tail" } +# +# list bluetooth devices +# +list_bluetooth() +{ +${CURL} ${OPTS} -s -b ${COOKIE} -A "Mozilla/5.0" -H "DNT: 1" -H "Connection: keep-alive" -L\ + -H "Content-Type: application/json; charset=UTF-8" -H "Referer: https://alexa.${AMAZON}/spa/index.html" -H "Origin: https://alexa.${AMAZON}"\ + -H "csrf: $(awk "\$0 ~/.${AMAZON}.*csrf[ \\s\\t]+/ {print \$7}" ${COOKIE})" -X GET \ + "https://${ALEXA}/api/bluetooth?cached=false" +} + # # connect bluetooth device # @@ -589,11 +600,20 @@ check_status if [ $? -eq 0 ] ; then echo "cookie expired, logging in again ..." log_in + check_status + if [ $? -eq 0 ] ; then + echo "log in failed, aborting" + exit 1 + fi fi if [ ! -f ${DEVTXT} ] ; then echo "device list do not exist. downloading ..." get_devlist + if [ ! -f ${DEVTXT} ] ; then + echo "failed to download device list, aborting" + exit 1 + fi fi if [ -n "$COMMAND" ] ; then @@ -622,6 +642,9 @@ elif [ -n "$BLUETOOTH" ] ; then if [ "$BLUETOOTH" = "null" ] ; then echo "disconnecting dev:${DEVICE} type:${DEVICETYPE} serial:${DEVICESERIALNUMBER} from bluetooth" disconnect_bluetooth + elif [ "$BLUETOOTH" = "list" -o "$BLUETOOTH" = "List" -o "$BLUETOOTH" = "LIST" ] ; then + echo "bluetooth api list:" + list_bluetooth else echo "connecting dev:${DEVICE} type:${DEVICETYPE} serial:${DEVICESERIALNUMBER} to bluetooth device:${BLUETOOTH}" connect_bluetooth