-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroku-control
More file actions
executable file
·119 lines (98 loc) · 3.17 KB
/
roku-control
File metadata and controls
executable file
·119 lines (98 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash
## -------------------------------------------------------------------
## Roku Control 1.0.0
##
## This is free software: you are free to change and redistribute it.
## There is NO WARRANTY, to the extent permitted by law.
## -------------------------------------------------------------------
### Interact with a roku device.
### Usage:
### roku-control [options]
### roku-control on
### roku-control off
### roku-control press-button <button>
### roku-control yt-play <url>
### roku-control complete <args>...
###
### Options:
### -h, --help Show help options.
### -v, --version Print program version.
function getCompletions() {
if [[ ${args[<args>,#]} -eq 2 ]]; then
compgen -W "on off press-button yt-play" -- "${args[<args>,1]}"
elif [[ ${args[<args>,#]} -eq 3 ]]; then
if [[ ${args[<args>,1]} -eq "press-button" ]]; then
compgen -W "$BUTTONS" -- "${args[<args>,2]}"
fi
fi
}
help=$(grep "^### " "$0" | cut -c 5-)
version=$(grep "^## " "$0" | cut -c 4-)
eval "$(docopts -A args -h "$help" -V "$version" : "$@")"
. $(dirname $0)/scriptsConfig
if [ -z "$ROKU_URL" ]; then
echo 'Please set $ROKU_URL !!'
exit 1
fi
DEVICE_IS_ON_VALUE="PowerOn"
BUTTONS="Home Rev Fwd Play Select Left Right Down Up Back InstantReplay Info Backspace Search Enter VolumnDown VolumeMute VolumnUp
PowerOn PowerOff ChannelUp ChannelDown InputTuner InputHDMI1 InputHDMI2 InputHDMI3 InputHDMI4 InputAV1"
YT_CHANNEL=837
function checkIfOn() {
curl "$ROKU_URL/query/device-info" 2>/dev/null | grep power-mode | awk -F "[<>]" '{print $3}'
}
function pressButton() {
curl -d "" "$ROKU_URL/keypress/$1" 2>/dev/null
}
# Usage: playOnChannel channelId mediaType contentId
function playOnChannel() {
curl -d "" "$ROKU_URL/launch/$1?mediaType=$2&contentId=$3"
}
function turnOn() {
pressButton PowerOn
}
function turnOff() {
pressButton PowerOff
}
function playOnYoutube() {
playOnChannel $YT_CHANNEL app "$1"
}
function ensureDeviceOn() {
if [ "$(checkIfOn)" != "$DEVICE_IS_ON_VALUE" ]; then
echo "Turning device on"
turnOn
loop_counter=0
echo "Waiting for device to boot"
while [ $loop_counter -lt 15 -a "$(checkIfOn)" != "$DEVICE_IS_ON_VALUE" ]; do
sleep 1
loop_counter=$((loop_counter+1))
done
if [ "$(checkIfOn)" == "$DEVICE_IS_ON_VALUE" ]; then
echo "Device booted"
else
echo "Device would not boot"
exit 1;
fi
else
echo "Device is on"
fi
}
if [ "${args[on]}" == "true" ]; then
echo "Turning on"
turnOn
elif [ "${args[off]}" == "true" ]; then
echo "Turning off"
turnOff
elif [ "${args[yt-play]}" == "true" ]; then
ensureDeviceOn
video_id="$(echo "${args[<url>]}" | sed 's|^https\?://\(www.\)\?youtu.\?be\(.com\)\?/\(watch?v=\)\?||')"
echo "Playing $video_id on YouTube"
playOnYoutube "$video_id"
elif [ "${args[press-button]}" == "true" ]; then
echo "Pressing ${args[<button>]}"
pressButton "${args[<button>]}"
elif [ "${args[complete]}" == "true" ]; then
getCompletions
else
echo "idk what you want from me man"
fi