-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgroku.go
226 lines (192 loc) · 4.75 KB
/
groku.go
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package main
import (
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"strings"
"time"
)
var CONFIG string
const (
VERSION = "0.4.1"
USAGE = `usage: groku [--version] [--help] <command> [<args>]
CLI remote for your Roku
Commands:
home Return to the home screen
rev Reverse
fwd Fast Forward
select Select
left Left
right Right
up Up
down Down
back Back
info Info
backspace Backspace
enter Enter
search Search
replay Replay
play Play
pause Pause
discover Discover a roku on your local network
text Send text to the Roku
apps List installed apps on your Roku
app Launch specified app
`
)
type dictonary struct {
XMLName xml.Name `xml:"apps"`
Apps []app `xml:"app"`
}
type app struct {
Name string `xml:",chardata"`
ID string `xml:"id,attr"`
}
type grokuConfig struct {
Address string `json:"address"`
Timestamp int64 `json:"timestamp"`
}
func main() {
CONFIG = fmt.Sprintf("%s/groku.json", os.TempDir())
if len(os.Args) == 1 || os.Args[1] == "--help" || os.Args[1] == "-help" ||
os.Args[1] == "--h" || os.Args[1] == "-h" || os.Args[1] == "help" {
fmt.Println(USAGE)
os.Exit(0)
}
if len(os.Args) == 2 && (os.Args[1] == "-v" || os.Args[1] == "--version" ||
os.Args[1] == "--version") {
fmt.Printf("groku version %s\n", VERSION)
os.Exit(0)
}
switch os.Args[1] {
case "home", "rev", "fwd", "select", "left", "right", "down", "up",
"back", "info", "backspace", "enter", "search":
http.PostForm(fmt.Sprintf("%vkeypress/%v", getRokuAddress(), os.Args[1]), nil)
os.Exit(0)
case "replay":
http.PostForm(fmt.Sprintf("%vkeypress/%v", getRokuAddress(), "InstantReplay"), nil)
os.Exit(0)
case "play", "pause":
http.PostForm(fmt.Sprintf("%vkeypress/%v", getRokuAddress(), "Play"), nil)
os.Exit(0)
case "discover":
fmt.Println("Found roku at", getRokuAddress())
os.Exit(0)
case "text":
if len(os.Args) < 3 {
fmt.Println(USAGE)
os.Exit(1)
}
roku := getRokuAddress()
for _, c := range os.Args[2] {
http.PostForm(fmt.Sprintf("%skeypress/Lit_%s", roku, url.QueryEscape(string(c))), nil)
}
os.Exit(0)
case "apps":
dict := queryApps()
for _, a := range dict.Apps {
fmt.Println(a.Name)
}
os.Exit(0)
case "app":
if len(os.Args) < 3 {
fmt.Println(USAGE)
os.Exit(1)
}
dict := queryApps()
for _, a := range dict.Apps {
if a.Name == os.Args[2] {
http.PostForm(fmt.Sprintf("%vlaunch/%v", getRokuAddress(), a.ID), nil)
os.Exit(0)
}
}
fmt.Printf("App %q not found\n", os.Args[2])
os.Exit(1)
default:
fmt.Println(USAGE)
os.Exit(1)
}
}
func queryApps() dictonary {
resp, err := http.Get(fmt.Sprintf("%squery/apps", getRokuAddress()))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer resp.Body.Close()
var dict dictonary
if err := xml.NewDecoder(resp.Body).Decode(&dict); err != nil {
fmt.Println(err)
os.Exit(1)
}
return dict
}
func findRoku() string {
ssdp, err := net.ResolveUDPAddr("udp", "239.255.255.250:1900")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
addr, err := net.ResolveUDPAddr("udp", ":0")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
socket, err := net.ListenUDP("udp", addr)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
_, err = socket.WriteToUDP([]byte("M-SEARCH * HTTP/1.1\r\n"+
"HOST: 239.255.255.250:1900\r\n"+
"MAN: \"ssdp:discover\"\r\n"+
"ST: roku:ecp\r\n"+
"MX: 3 \r\n\r\n"), ssdp)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
answerBytes := make([]byte, 1024)
err = socket.SetReadDeadline(time.Now().Add(3 * time.Second))
if err != nil {
fmt.Println(err)
os.Exit(1)
}
_, _, err = socket.ReadFromUDP(answerBytes[:])
if err != nil {
fmt.Println("Could not find your Roku!")
os.Exit(1)
}
ret := strings.Split(string(answerBytes), "\r\n")
location := strings.TrimPrefix(ret[6], "LOCATION: ")
return location
}
func getRokuAddress() string {
var configFile *os.File
var config grokuConfig
configFile, err := os.Open(CONFIG)
// the config file doesn't exist, but that's okay
if err != nil {
config.Address = findRoku()
config.Timestamp = time.Now().Unix()
} else {
// the config file exists
if err := json.NewDecoder(configFile).Decode(&config); err != nil {
config.Address = findRoku()
}
//if the config file is over 60 seconds old, then replace it
if config.Timestamp == 0 || time.Now().Unix()-config.Timestamp > 60 {
config.Address = findRoku()
config.Timestamp = time.Now().Unix()
}
}
if b, err := json.Marshal(config); err == nil {
ioutil.WriteFile(CONFIG, b, os.ModePerm)
}
return config.Address
}