forked from cliftonts/rokugtk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rokugtk.py
executable file
·368 lines (303 loc) · 11.7 KB
/
rokugtk.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Import modules
import socket
import os
import sys
import requests
import urllib.request, urllib.parse, urllib.error
import time
import ssdp
import gi
if sys.version_info >= (3,0):
import urllib.request
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk as Gtk
else:
import urllib.request, urllib.error, urllib.parse
import pyGtk
gi.require_version('Gtk', '3.0')
import Gtk
#Initialise variables
version = "0.4.0" #Also change in snapcraft.yaml and setup/gui/rokuGtk.desktop
ip = ""
def get_resource_path(rel_path):
"""https://stackoverflow.com/questions/4416336/adding-a-program-icon-in-python-Gtk"""
dir_of_py_file = os.path.dirname(__file__)
rel_path_to_resource = os.path.join(dir_of_py_file, rel_path)
abs_path_to_resource = os.path.abspath(rel_path_to_resource)
return abs_path_to_resource
class Application():
#Initialise window
def __init__(self):
self.window = Gtk.Window()
self.window.connect("destroy", Gtk.main_quit) # no parenthesis; you pass functions to connect
self.window.set_title("RokuGtk - " + version)
self.window.set_icon_from_file(get_resource_path("rokugtk.png"))
self.create_widgets()
self.connect_signals()
self.window.show_all()
response = ssdp.discover("roku:ecp")
if len(response) == 0:
find()
else:
global ip
ip = response[0].location.encode()
print(ip)
Gtk.main()
def create_widgets(self):
self.vbox = Gtk.VBox(spacing=10)
#Add buttons
#First row
self.hbox_1 = Gtk.HBox(spacing=10)
self.button_back = Gtk.Button.new_with_label("Back")
self.button_back.set_size_request(1,4)
self.hbox_1.pack_start(self.button_back, True, True, 0)
self.button_up = Gtk.Button.new_with_label("Up")
self.button_up.set_size_request(1,4)
self.hbox_1.pack_start(self.button_up, True, True, 0)
self.button_home = Gtk.Button.new_with_label("Home")
self.button_home.set_size_request(1,4)
self.hbox_1.pack_start(self.button_home, True, True, 0)
#Second row
self.hbox_2 = Gtk.HBox(spacing=10)
self.button_left = Gtk.Button.new_with_label("Left")
self.button_left.set_size_request(1,4)
self.hbox_2.pack_start(self.button_left, True, True, 0)
self.button_select = Gtk.Button.new_with_label("Select")
self.button_select.set_size_request(1,4)
self.hbox_2.pack_start(self.button_select, True, True, 0)
self.button_right = Gtk.Button.new_with_label("Right")
self.button_right.set_size_request(1,4)
self.hbox_2.pack_start(self.button_right, True, True, 0)
#Third row
self.hbox_3 = Gtk.HBox(spacing=10)
self.button_reverse = Gtk.Button.new_with_label("Reverse")
self.button_reverse.set_size_request(1,4)
self.hbox_3.pack_start(self.button_reverse, True, True, 0)
self.button_down = Gtk.Button.new_with_label("Down")
self.button_down.set_size_request(1,4)
self.hbox_3.pack_start(self.button_down, True, True, 0)
self.button_forward = Gtk.Button.new_with_label("Forward")
self.button_forward.set_size_request(1,4)
self.hbox_3.pack_start(self.button_forward, True, True, 0)
#Fourth row
self.hbox_4 = Gtk.HBox(spacing=10)
self.button_play = Gtk.Button.new_with_label("Play")
self.button_play.set_size_request(1,4)
self.hbox_4.pack_start(self.button_play, True, True, 0)
self.button_reload = Gtk.Button.new_with_label("Reload")
self.button_reload.set_size_request(1,4)
self.hbox_4.pack_start(self.button_reload, True, True, 0)
#Fifth row
self.hbox_5 = Gtk.HBox(spacing=10)
self.button_info = Gtk.Button.new_with_label("Info")
self.button_info.set_size_request(1,4)
self.hbox_5.pack_start(self.button_info, True, True, 0)
self.button_search = Gtk.Button.new_with_label("Search")
self.button_search.set_size_request(1,4)
self.hbox_5.pack_start(self.button_search, True, True, 0)
self.button_about = Gtk.Button.new_with_label("About")
self.button_about.set_size_request(1,4)
self.hbox_5.pack_start(self.button_about, True, True, 0)
self.vbox.pack_start(self.hbox_1, True, True, 0)
self.vbox.pack_start(self.hbox_2, True, True, 0)
self.vbox.pack_start(self.hbox_3, True, True, 0)
self.vbox.pack_start(self.hbox_4, True, True, 0)
self.vbox.pack_start(self.hbox_5, True, True, 0)
self.window.add(self.vbox)
self.window.set_size_request(200, 200)
def connect_signals(self):
self.button_back.connect("clicked", self.callback_back)
self.button_up.connect("clicked", self.callback_up)
self.button_home.connect("clicked", self.callback_home)
self.button_left.connect("clicked", self.callback_left)
self.button_select.connect("clicked", self.callback_select)
self.button_right.connect("clicked", self.callback_right)
self.button_reverse.connect("clicked", self.callback_reverse)
self.button_down.connect("clicked", self.callback_down)
self.button_forward.connect("clicked", self.callback_forward)
self.button_play.connect("clicked", self.callback_play)
self.button_reload.connect("clicked", self.callback_reload)
self.button_info.connect("clicked", self.callback_info)
self.button_search.connect("clicked", self.callback_search)
self.button_about.connect("clicked", self.callback_about)
def callback_back(self, widget, callback_data=None):
cmd = "http://" + ip + ":8060/keypress/Back"
send(cmd)
def callback_up(self, widget, callback_data=None):
cmd = "http://" + ip + ":8060/keypress/Up"
send(cmd)
def callback_home(self, widget, callback_data=None):
cmd = "http://" + ip + ":8060/keypress/Home"
send(cmd)
def callback_left(self, widget, callback_data=None):
cmd = "http://" + ip + ":8060/keypress/Left"
send(cmd)
def callback_select(self, widget, callback_data=None):
cmd = "http://" + ip + ":8060/keypress/Select"
send(cmd)
def callback_right(self, widget, callback_data=None):
cmd = "http://" + ip + ":8060/keypress/Right"
send(cmd)
def callback_reverse(self, widget, callback_data=None):
cmd = "http://" + ip + ":8060/keypress/Rev"
send(cmd)
def callback_down(self, widget, callback_data=None):
cmd = "http://" + ip + ":8060/keypress/Down"
send(cmd)
def callback_forward(self, widget, callback_data=None):
cmd = "http://" + ip + ":8060/keypress/Fwd"
send(cmd)
def callback_play(self, widget, callback_data=None):
cmd = "http://" + ip + ":8060/keypress/Play"
send(cmd)
def callback_reload(self, widget, callback_data=None):
cmd = "http://" + ip + ":8060/keypress/InstantReplay"
send(cmd)
def callback_info(self, widget, callback_data=None):
cmd = "http://" + ip + ":8060/keypress/Info"
send(cmd)
def callback_search(self, widget, callback_data=None):
search = EntryDialog(parent=None,
flags=0,
type=Gtk.MESSAGE_INFO,
buttons=Gtk.BUTTONS_OK,
message_format=None)
search.set_markup("Search")
searchfor = search.run()
search.destroy()
if searchfor !="None":
keyboard(ip, searchfor)
def callback_about(self, widget, callback_data=None):
aboutdialog = Gtk.AboutDialog()
# lists of authors and documenters (will be used later)
authors = ["Gareth France"]
# we fill in the aboutdialog
aboutdialog.set_program_name("RokuGtk " + version)
aboutdialog.set_comments("RokuGtk is a remote control application for controlling Roku and Now TV set top boxes.\n\nIf you have found this program to be useful please consider making a small donation towards future development.\nPaypal:- [email protected]\nPPPay.com:- [email protected]\n\n A massive thank you to kyrofa, elopio and Mark Shuttleworth for their help in making the snap version possible.")
aboutdialog.set_authors(authors)
aboutdialog.set_website("https://github.com/cliftonts/rokuGtk")
aboutdialog.set_website_label("Report issues on GitHub")
# we do not want to show the title, which by default would be "About AboutDialog Example"
# we have to reset the title of the messagedialog window after setting
# the program name
aboutdialog.set_title("")
# to close the aboutdialog when "close" is clicked we connect the
# "response" signal to on_close
aboutdialog.connect("response", self.on_close)
# show the aboutdialog
aboutdialog.show()
# destroy the aboutdialog
def on_close(self, action, parameter):
action.destroy()
def send(url):
payload = {'': ''}
try:
# POST with form-encoded data
r = requests.post(url, data=payload)
# Response, status etc
#r.text
#r.status_code
except:
message = Gtk.MessageDialog(parent=None,
flags=0,
type=Gtk.MESSAGE_WARNING,
buttons=Gtk.BUTTONS_OK,
message_format=None)
message.set_markup("Roku not found!\nPlease try again.")
message.run()
quit()
def find():
global ip
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8",80))
ip = s.getsockname()[0]
s.close()
ipsplit = ip.split(".")
ip = ""
for i in range(0, 3):
ip = ip + ipsplit[i] + "."
for i in range(1,257):
try:
url = 'http://' + ip + str(i) + ':8060'
print(("Attempting - " + ip + str(i)))
r = urllib.request.urlopen(url, timeout=0.2)
html=r.read()
if b"Roku" in html:
print ("Roku found!")
print((ip + str(i)))
ip = ip + str(i)
break
except:
pass
if ip[-1:] == ".":
message = Gtk.MessageDialog(parent=None,
# flags=0,
message_type=Gtk.MessageType.ERROR,
buttons=Gtk.ButtonsType.OK,
text="Roku not found!\nPlease try again.")
message.run()
quit()
class EntryDialog(Gtk.MessageDialog):
def __init__(self, *args, **kwargs):
'''
Creates a new EntryDialog. Takes all the arguments of the usual
MessageDialog constructor plus one optional named argument
"default_value" to specify the initial contents of the entry.
'''
if 'default_value' in kwargs:
default_value = kwargs['default_value']
del kwargs['default_value']
else:
default_value = ''
super(EntryDialog, self).__init__(*args, **kwargs)
entry = Gtk.Entry()
entry.set_text(str(default_value))
entry.connect("activate",
lambda ent, dlg, resp: dlg.response(resp),
self, Gtk.RESPONSE_OK)
self.vbox.pack_end(entry, True, True, 0)
self.vbox.show_all()
self.entry = entry
def set_value(self, text):
self.entry.set_text(text)
def run(self):
result = super(EntryDialog, self).run()
if result == Gtk.RESPONSE_OK:
text = self.entry.get_text()
else:
text = None
return text
def keyboard(ip, keyin):
#urllib.quote('/test', safe='')
#networkCall("POST", "http://" + ip + ":8060/keypress/Backspace");
#if sys.version_info < (3,0):
for i in keyin:
if sys.version_info >= (3,0):
url = "http://" + ip + ":8060/keypress/Lit_" + urllib.parse.quote(i, safe='');
else:
url = "http://" + ip + ":8060/keypress/Lit_" + urllib.parse.quote(i, safe='');
payload = {'': ''}
try:
# POST with form-encoded data
r = requests.post(url, data=payload)
# Response, status etc
#r.text
#r.status_code
except:
message = Gtk.MessageDialog(parent=None,
flags=0,
message_type=Gtk.MessageType.WARNING,
buttons=Gtk.ButtonsType.OK,
text=None)
message.set_markup("Roku not found!\nPlease try again.")
message.run()
quit()
time.sleep(0.2)
#if sys.version_info < (3,0):
#This routine will allow for any code to run before quitting.
def quitting(tmp, tmp2):
Gtk.main_quit()
if __name__ == "__main__":
app = Application()