Skip to content

Instantly share code, notes, and snippets.

@jwenerd
Last active February 21, 2024 19:05
Show Gist options
  • Save jwenerd/2bc7ab3f2e69b858a8357b4de2c1f44f to your computer and use it in GitHub Desktop.
Save jwenerd/2bc7ab3f2e69b858a8357b4de2c1f44f to your computer and use it in GitHub Desktop.
Roku Play Pause
# assuming you are on macOS with zsh;
# assumes your roku is also setup to use Airplay (easier method to discover instead ARP discovery which requires sudo)
# requirements: gnu-coreutils timeout; curl;
# `brew install coreutils curl`
#
# usage:
# roku-keypress [remote-button (default = play if not specified)]
#
# for all available keypress values see
# https://developer.roku.com/docs/developer-program/dev-tools/external-control-api.md#keypress-key-values
#
# output:
# the current state of the roku
function roku-keypress() {
local ROKU_NAME='jRok'
function __run_with_timeout() {
gtimeout --foreground 0.25s "$@"
}
function _roku_dns_local() {
__run_with_timeout dns-sd -L "${ROKU_NAME}" _airplay local. | grep 'reached' | tr ' ' '\n' | grep '.local.' | awk '{gsub(/:7000/,"")}1' | tail -1
}
function _roku_ip() {
local domain=$(_roku_dns_local)
[[ -z "${domain}" ]] &&
echo '' && return 1;
# TODO: cache this output on simple file store for a little, the delay from running dns-sd can feel laggy
local output=$(__run_with_timeout dns-sd -Gv4v6 $(_roku_dns_local))
echo $output | awk '{print $6}' | tail -1
}
local ip=$(_roku_ip)
if [[ -z "${ip}" ]]; then
echo "⚠️ unable to find device ($ROKU_NAME)"
return 1
fi
local key=${1:-"play"}
curl -d '' "http://$ip:8060/keypress/$key"
echo ""
echo -n '📺 ' && curl -s "http://$ip:8060/query/media-player" | xmllint --xpath '/player/@state' -
}
roku-keypress $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment