-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote.py
386 lines (325 loc) · 15.3 KB
/
remote.py
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import tkinter as tk
from tkinter import ttk
import requests
import socket
import re
import time
from html import unescape
#Settings > System > Advanced system settings > Control by mobile apps -> Enable is required to be enabled on the Roku device
# This is a simple Roku remote control app using the External Control Protocol (ECP).
class RokuRemoteApp:
def __init__(self, master):
self.master = master
self.master.title("Roku Remote")
self.discovered_rokus = []
self.selected_roku_ip = tk.StringVar(value="")
self.installed_channels = {}
self.create_widgets()
# ---- KEY BINDINGS FOR ARROW KEYS & SPACE BAR ----
self.master.bind("<Up>", lambda e: self.send_keypress("Up"))
self.master.bind("<Down>", lambda e: self.send_keypress("Down"))
self.master.bind("<Left>", lambda e: self.send_keypress("Left"))
self.master.bind("<Right>", lambda e: self.send_keypress("Right"))
self.master.bind("<space>", lambda e: self.send_keypress("Select"))
def create_widgets(self):
top_frame = tk.Frame(self.master)
top_frame.pack(pady=5, fill=tk.X)
scan_button = tk.Button(top_frame, text="Scan for Rokus", command=self.scan_for_rokus)
scan_button.pack(side=tk.LEFT, padx=5)
self.roku_combo = ttk.Combobox(
top_frame,
textvariable=self.selected_roku_ip,
width=30,
state="readonly"
)
self.roku_combo.configure(takefocus=False)
self.roku_combo.set("No Rokus discovered")
self.roku_combo.pack(side=tk.LEFT, padx=5, fill=tk.X, expand=True)
self.roku_combo.bind("<<ComboboxSelected>>", self.on_roku_selected)
remote_frame = tk.LabelFrame(self.master, text="Remote Control")
remote_frame.pack(pady=10, fill=tk.X)
top_buttons_frame = tk.Frame(remote_frame)
top_buttons_frame.pack()
tk.Button(top_buttons_frame, text="Home", width=8,
command=lambda: self.send_keypress("Home")).pack(side=tk.LEFT, padx=5)
tk.Button(top_buttons_frame, text="Back", width=8,
command=lambda: self.send_keypress("Back")).pack(side=tk.LEFT, padx=5)
dpad_frame = tk.Frame(remote_frame)
dpad_frame.pack(pady=10)
tk.Button(dpad_frame, text="Up", width=8,
command=lambda: self.send_keypress("Up")).grid(row=0, column=1, pady=5)
tk.Button(dpad_frame, text="Left", width=8,
command=lambda: self.send_keypress("Left")).grid(row=1, column=0, padx=5)
tk.Button(dpad_frame, text="OK", width=8,
command=lambda: self.send_keypress("Select")).grid(row=1, column=1, padx=5)
tk.Button(dpad_frame, text="Right", width=8,
command=lambda: self.send_keypress("Right")).grid(row=1, column=2, padx=5)
tk.Button(dpad_frame, text="Down", width=8,
command=lambda: self.send_keypress("Down")).grid(row=2, column=1, pady=5)
extra_buttons_frame = tk.Frame(remote_frame)
extra_buttons_frame.pack()
tk.Button(extra_buttons_frame, text="Info", width=8,
command=lambda: self.send_keypress("Info")).pack(side=tk.LEFT, padx=5)
tk.Button(extra_buttons_frame, text="Star", width=8,
command=lambda: self.send_keypress("Star")).pack(side=tk.LEFT, padx=5)
tk.Button(extra_buttons_frame, text="Replay", width=8,
command=lambda: self.send_keypress("InstantReplay")).pack(side=tk.LEFT, padx=5)
transport_frame = tk.Frame(remote_frame)
transport_frame.pack(pady=10)
tk.Button(transport_frame, text="Rev", width=8,
command=lambda: self.send_keypress("Rev")).pack(side=tk.LEFT, padx=5)
tk.Button(transport_frame, text="Play/Pause", width=8,
command=lambda: self.send_keypress("Play")).pack(side=tk.LEFT, padx=5)
tk.Button(transport_frame, text="Fwd", width=8,
command=lambda: self.send_keypress("Fwd")).pack(side=tk.LEFT, padx=5)
volume_frame = tk.Frame(remote_frame)
volume_frame.pack(pady=10)
tk.Button(volume_frame, text="Vol +", width=8,
command=lambda: self.send_keypress("VolumeUp")).pack(side=tk.LEFT, padx=5)
tk.Button(volume_frame, text="Vol -", width=8,
command=lambda: self.send_keypress("VolumeDown")).pack(side=tk.LEFT, padx=5)
tk.Button(volume_frame, text="Mute", width=8,
command=lambda: self.send_keypress("VolumeMute")).pack(side=tk.LEFT, padx=5)
adv_frame = tk.LabelFrame(self.master, text="Additional Options")
adv_frame.pack(pady=10, fill=tk.X)
adv_btns_frame = tk.Frame(adv_frame)
adv_btns_frame.pack(pady=5)
tk.Button(adv_btns_frame, text="Get Device Info", command=self.get_device_info).pack(side=tk.LEFT, padx=5)
tk.Button(adv_btns_frame, text="Refresh Apps", command=self.refresh_apps).pack(side=tk.LEFT, padx=5)
channel_launch_frame = tk.Frame(adv_frame)
channel_launch_frame.pack(pady=5, fill=tk.X)
self.channel_list_var = tk.StringVar()
self.channel_combo = ttk.Combobox(
channel_launch_frame,
textvariable=self.channel_list_var,
width=30,
state="readonly"
)
self.channel_combo.set("No Apps loaded")
self.channel_combo.pack(side=tk.LEFT, padx=5, fill=tk.X, expand=True)
# Bind the selection event to unfocus the combobox
self.channel_combo.bind("<<ComboboxSelected>>", self.on_channel_selected)
launch_button = tk.Button(channel_launch_frame, text="Launch App", command=self.launch_selected_channel)
launch_button.pack(side=tk.LEFT, padx=5)
self.log_text = tk.Text(self.master, height=6, width=60)
self.log_text.pack(pady=5)
# ------------------- Roku Selection and Active App -------------------
def on_roku_selected(self, event):
combo_str = self.roku_combo.get()
ip_match = re.search(r"(\d+\.\d+\.\d+\.\d+)", combo_str)
if ip_match:
ip = ip_match.group(1)
self.selected_roku_ip.set(ip)
self.log(f"Selected Roku IP: {ip}")
self.refresh_apps()
self.set_active_app_in_combo()
else:
self.log("No valid IP selected.")
self.master.focus_set()
def set_active_app_in_combo(self):
ip = self.selected_roku_ip.get().strip()
if not ip or ip.startswith("No Rokus"):
return
try:
url = f"http://{ip}:8060/query/active-app"
r = requests.get(url, timeout=2)
if r.status_code == 200:
self.log("Active-app response:\n" + r.text)
# More flexible pattern to capture <app> with or without an id:
match = re.search(r"<app([^>]*)>([^<]+)</app>", r.text)
if match:
attr_str = match.group(1) # e.g. ' id="12" something="..."'
app_name = match.group(2)
# Attempt to find an id="...":
id_match = re.search(r'id="([^"]+)"', attr_str)
if id_match:
active_app_id = id_match.group(1)
else:
active_app_id = None
if active_app_id:
self.log(f"Active app parsed: {app_name} ({active_app_id})")
else:
self.log(f"Active app parsed (no id): {app_name}")
if active_app_id and active_app_id in self.installed_channels:
combo_str = f"{self.installed_channels[active_app_id]} ({active_app_id})"
self.channel_combo.set(combo_str)
else:
if active_app_id:
self.channel_combo.set(f"{app_name} ({active_app_id})")
else:
self.channel_combo.set(app_name)
else:
self.log("Could not parse active-app response.")
else:
self.log(f"Active app request error: HTTP {r.status_code}")
except requests.RequestException as e:
self.log(f"Error retrieving active app: {e}")
def on_channel_selected(self, event):
self.master.focus_set()
def send_keypress(self, keypress):
ip = self.selected_roku_ip.get().strip()
if not ip or ip.startswith("No Rokus"):
self.log("No Roku selected.")
return
url = f"http://{ip}:8060/keypress/{keypress}"
try:
r = requests.post(url, timeout=2)
if r.status_code == 200:
self.log(f"Sent keypress '{keypress}' to {ip}")
else:
self.log(f"Keypress '{keypress}' error: HTTP {r.status_code}")
except requests.exceptions.RequestException as e:
self.log(f"Error sending {keypress} to {ip}: {e}")
def get_device_info(self):
ip = self.selected_roku_ip.get().strip()
if not ip or ip.startswith("No Rokus"):
self.log("No Roku selected.")
return
url = f"http://{ip}:8060/query/device-info"
try:
r = requests.get(url, timeout=2)
if r.status_code == 200:
self.log("Device Info:\n" + r.text)
else:
self.log(f"Device info request error: HTTP {r.status_code}")
except requests.exceptions.RequestException as e:
self.log(f"Error retrieving device info: {e}")
def refresh_apps(self):
self.get_installed_channels()
self.set_active_app_in_combo()
def get_installed_channels(self):
ip = self.selected_roku_ip.get().strip()
if not ip or ip.startswith("No Rokus"):
self.log("No Roku selected.")
return
url = f"http://{ip}:8060/query/apps"
try:
r = requests.get(url, timeout=3)
if r.status_code == 200:
apps_xml = r.text
self.installed_channels = self.parse_roku_apps_xml(apps_xml)
if self.installed_channels:
combo_values = [
f"{name} ({app_id})" for app_id, name in self.installed_channels.items()
]
self.channel_combo['values'] = combo_values
self.channel_combo.set("Select an app to launch")
self.log(f"Loaded {len(self.installed_channels)} apps.")
else:
self.channel_combo['values'] = []
self.channel_combo.set("No apps found")
self.log("No apps found on device.")
else:
self.log(f"Error: HTTP {r.status_code} retrieving apps.")
except requests.exceptions.RequestException as e:
self.log(f"Error retrieving apps: {e}")
def parse_roku_apps_xml(self, xml_text):
pattern = re.compile(r'<app id="([^"]+)"[^>]*>([^<]+)</app>')
apps = {}
for match in pattern.finditer(xml_text):
app_id = match.group(1)
app_name = unescape(match.group(2))
apps[app_id] = app_name
return apps
def launch_selected_channel(self):
ip = self.selected_roku_ip.get().strip()
if not ip or ip.startswith("No Rokus"):
self.log("No Roku selected.")
return
combo_str = self.channel_combo.get()
match = re.search(r"\(([^)]+)\)$", combo_str)
if not match:
self.log("Please select a valid channel.")
return
app_id = match.group(1)
url = f"http://{ip}:8060/launch/{app_id}"
try:
r = requests.post(url, timeout=3)
if r.status_code == 200:
self.log(f"Launched channel: {combo_str}")
time.sleep(1) # Small delay so Roku can switch
self.set_active_app_in_combo()
else:
self.log(f"Error launching channel (HTTP {r.status_code})")
except requests.exceptions.RequestException as e:
self.log(f"Error launching channel {app_id}: {e}")
def scan_for_rokus(self):
self.discovered_rokus = self.ssdp_discover_roku()
if not self.discovered_rokus:
self.roku_combo['values'] = []
self.roku_combo.set("No Rokus discovered")
self.log("No Rokus discovered.")
return
combo_values = []
for friendly_name, ip in self.discovered_rokus:
combo_values.append(f"{friendly_name} - {ip}")
self.roku_combo['values'] = combo_values
self.roku_combo.current(0)
first_ip = self.discovered_rokus[0][1]
self.selected_roku_ip.set(first_ip)
self.log(f"Discovered {len(self.discovered_rokus)} Roku(s). Selected: {first_ip}")
self.refresh_apps()
def ssdp_discover_roku(self, timeout=3):
group = ("239.255.255.250", 1900)
message = (
"M-SEARCH * HTTP/1.1\r\n"
"HOST: 239.255.255.250:1900\r\n"
'MAN: "ssdp:discover"\r\n'
"MX: 2\r\n"
"ST: roku:ecp\r\n\r\n"
)
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.settimeout(timeout)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
discovered = {}
try:
sock.sendto(message.encode("utf-8"), group)
start_time = time.time()
while True:
if time.time() - start_time > timeout:
break
try:
data, _ = sock.recvfrom(65507)
response = data.decode("utf-8", errors="replace")
location_match = re.search(r"(?i)location:\s*(http://[^/]+)", response)
if location_match:
location_url = location_match.group(1).strip()
ip_match = re.search(r"http://([\d\.]+):8060", location_url)
if ip_match:
ip_addr = ip_match.group(1)
if ip_addr not in discovered:
friendly_name = self.get_roku_friendly_name(ip_addr)
discovered[ip_addr] = friendly_name
except socket.timeout:
break
except Exception as e:
self.log(f"SSDP discovery error: {e}")
break
finally:
sock.close()
results = []
for ip_addr, name in discovered.items():
if not name:
name = f"Roku at {ip_addr}"
results.append((name, ip_addr))
return results
def get_roku_friendly_name(self, ip):
try:
url = f"http://{ip}:8060/query/device-info"
resp = requests.get(url, timeout=2)
if resp.status_code == 200:
match = re.search(r"<friendly-device-name>([^<]+)</friendly-device-name>", resp.text)
if match:
return match.group(1).strip()
except Exception:
pass
return None
def log(self, message):
self.log_text.insert(tk.END, message + "\n")
self.log_text.see(tk.END)
def main():
root = tk.Tk()
app = RokuRemoteApp(root)
root.mainloop()
if __name__ == "__main__":
main()