forked from nawar/kodi-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsend_YouTube_link_to_Kodi-GTK3.py
More file actions
242 lines (221 loc) · 11.3 KB
/
send_YouTube_link_to_Kodi-GTK3.py
File metadata and controls
242 lines (221 loc) · 11.3 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
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
#!/usr/bin/env python3
## License: GPL (c) 2016
##
## Update of my simple script for controlling Kodi from the computer
## my first intent with GTK3 and I use it to learn about GTK3 and python3
## Tomas Kaluza
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GObject, GLib, Gio
## https://media.readthedocs.org/pdf/python-gtk-3-tutorial/latest/python-gtk-3-tutorial.pdf
## page 82
from gi.repository.GdkPixbuf import Pixbuf
## Use the following if you want to use Gtk.Clipboard - obsolete Gdk is used already for keyboard shortcuts (alt d)
#from gi.repository import Gtk, Gdk
import subprocess
class GridWindow(Gtk.Window):
def __init__(self):
try:
self.clipboard_value=subprocess.check_output(["xclip", "-o"])
## Use the following if you want to use Gtk.Clipboard
#self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
#self.clipboard_value = self.clipboard.wait_for_text()
except:
#self.clipboard_value="b''"
self.clipboard_value = bytes()
try:
self.clipboard_value = (self.clipboard_value.decode('ascii'))
## Following works as well,but not sure it it is needed at all
#self.clipboard_value=(clipboard_value.decode('utf-8'))
## Use the following if you want to use Gtk.Clipboard
#self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
#self.clipboard_value = self.clipboard.wait_for_text()
except:
#self.clipboard_value="b''"
self.clipboard_value = bytes()
Gtk.Window.__init__(self, title="Send YouTube link to Kodi using kodi-cli")
#self.set_title("Test using the properties")
self.set_default_size(200, 100);
## Following works when the app is running under X11
## self.set_default_icon_name kicks in when set_icon_name doesn't fine the icon in the theme
## All three follwing worked under Gnome3 - X11
## https://lazka.github.io/pgi-docs/Gtk-3.0/classes/Window.html#Gtk.Window.set_icon_list
#pixbuf24 = Gtk.IconTheme.get_default().load_icon(icon, 24, 0)
#pixbuf32 = Gtk.IconTheme.get_default().load_icon(icon, 32, 0)
#pixbuf48 = Gtk.IconTheme.get_default().load_icon(icon, 48, 0)
#pixbuf64 = Gtk.IconTheme.get_default().load_icon(icon, 64, 0)
#pixbuf96 = Gtk.IconTheme.get_default().load_icon(icon, 96, 0)
#self.set_icon_list([pixbuf24, pixbuf32, pixbuf48, pixbuf64, pixbuf96]);
## https://lazka.github.io/pgi-docs/Gtk-3.0/classes/Window.html#Gtk.Window.set_default_icon_list
## https://lazka.github.io/pgi-docs/Gtk-3.0/classes/Window.html#Gtk.Window.set_icon_from_file
try:
self.set_icon_from_file("/usr/local/share/icons/scalable/apps/send_to_kodi.svg")
except:
self.set_icon_name("youtube")
self.set_default_icon_name("video-display")
## Fix Setting correct icon and application name in gnome3 Dash and Wayland.
## must match the exec= in the .desktop file
## Modified from here:
## https://github.com/michaldaniel/Ebook-Viewer/issues/19
## https://github.com/michaldaniel/Ebook-Viewer/pull/21/files
GLib.set_prgname('send_YouTube_link_to_Kodi')
self.set_border_width(8)
grid = Gtk.Grid()
grid.set_row_spacing(10)
grid.set_column_spacing(10)
self.add(grid)
self.youtube_entry = Gtk.Entry(text=self.clipboard_value)
#youtube_entry.set_text("Hello World")
self.progressbar = Gtk.ProgressBar(text="Kodi is not playing at the moment")
self.progresseventbox = Gtk.EventBox()
self.progressbar.set_show_text(True)
self.add_to_playlist = Gtk.Button(label="Add to playlist")
self.toggle_fullscreen = Gtk.Button(label="Toggle fullscreen")
self.toggle_playpause = Gtk.Button(label="Toggle Play / Pause")
self.play_now = Gtk.Button(label="Play link now")
self.output_text = Gtk.Label(self, label="kodi-cli output")
#output_text = Gtk.Label(output)
#grid.add(youtube_entry)
grid.attach(self.youtube_entry, 0, 0, 4, 1)
# Let's add the eventbox and inside the progress bar instead of just adding the progress bar
#grid.attach_next_to(self.progressbar, self.youtube_entry, Gtk.PositionType.BOTTOM, 4, 1)
grid.attach_next_to(self.progresseventbox, self.youtube_entry, Gtk.PositionType.BOTTOM, 4, 1)
self.progresseventbox.add(self.progressbar)
pbsize = self.progressbar.get_allocation()
print("here I am")
print(pbsize)
print(pbsize.width)
#print(event.x)
#self.progressbar.set_fraction(0.20)
grid.attach_next_to(self.add_to_playlist, self.progresseventbox, Gtk.PositionType.BOTTOM, 1, 1)
grid.attach_next_to(self.toggle_fullscreen, self.add_to_playlist, Gtk.PositionType.RIGHT, 1, 1)
grid.attach_next_to(self.toggle_playpause, self.toggle_fullscreen, Gtk.PositionType.RIGHT, 1, 1)
#grid.attach(button5, 3, 2, 1, 1)
grid.attach_next_to(self.play_now, self.toggle_playpause, Gtk.PositionType.RIGHT, 1, 1)
grid.attach_next_to(self.output_text, self.add_to_playlist, Gtk.PositionType.BOTTOM, 4, 1)
# Using the following line, we will catch the Enter key pressed
self.youtube_entry.connect("activate", self.on_play_now)
self.progresseventbox.connect("button-press-event", self.on_mouse_click)
self.add_to_playlist.connect("clicked", self.on_add_link)
self.toggle_fullscreen.connect("clicked", self.on_fullscreen_toggle)
self.toggle_playpause.connect("clicked", self.on_toggle_playpause)
self.play_now.connect("clicked", self.on_play_now)
try:
self.timeout_id = GObject.timeout_add(1000, self.on_get_percentage, None)
except:
pass
# Alt-D keyboard shortcut to select the text in the Entry box
accel = Gtk.AccelGroup()
accel.connect(Gdk.keyval_from_name('D'), Gdk.ModifierType.MOD1_MASK, 0, self.on_altd_pressed)
self.add_accel_group(accel)
#def on_mouse_click(progressbar, widget, event):
def on_mouse_click(self, widget, event):
# The _widget is used in sonata, but what does it mean?
#def on_mouse_click(self, _widget, event):
width = self.progresseventbox.get_allocated_width()
#allocation = progressbar.get_allocation()
#print(allocation)
#print(allocation.width)
#width = self.progressbar.get_allocated_width()
# is this correction because of the set_border_width or set row / column spacing?
# it is roughly 18 pt greater then the furthest point I can reach with the mouse
#width = width - 18
print(width)
#path = self.get_path_at_pos(event.x, event.y)
#print(path)
print(event.x, event.y)
percentage = event.x / width * 100
# does it get rounded in python3 ?
#percentage = int(percentage)
print(percentage)
percentage = str(percentage)
output = (subprocess.check_output(["kodi-cli", "-g", percentage]))
# Let's get some response from kodi-cli
output = (subprocess.check_output(["kodi-cli", "-g"]))
output = output.decode('ascii')
# http://stackoverflow.com/questions/1798465/python-remove-last-3-characters-of-a-string
self.output_text.set_text(output)
def on_get_percentage(self, user_data):
self.percentage = (subprocess.check_output(["kodi-cli","-r"]))
self.percentage = (self.percentage.decode('ascii'))
self.percentage = self.percentage.rstrip()
print("antes"+self.percentage+"después")
# Acording to following link, if not is the prefered way of finding out empty strings as they are "falsy"
# http://stackoverflow.com/questions/9573244/most-elegant-way-to-check-if-the-string-is-empty-in-python
if not self.percentage :
self.percentage = 0
print(self.percentage)
self.progressbar.set_fraction(0)
self.progressbar.set_text("Kodi is not playing at the moment")
return True
else:
pass
self.percentage = float(self.percentage)
#try:
# self.percentage = float(self.percentage)
#except:
# self.percentage = 0
self.percentage = int(self.percentage)
self.percentage = self.percentage/100
#type(self.percentage)
print(self.percentage)
# As this is a timeout function, return True so that it
# continues to get called
self.progressbar.set_fraction(self.percentage)
output = (subprocess.check_output(["kodi-cli", "-g"]))
output = output.decode('ascii')
# http://stackoverflow.com/questions/1798465/python-remove-last-3-characters-of-a-string
output = output[:-1]
output = output.lstrip("Playing from ")
self.progressbar.set_text(output)
#self.progressbar.set_show_text(True)
#print(output)
return True
def on_add_link(self, widget):
#output = (subprocess.check_output(["kodi-cli", "-q", self.clipboard_value]))
# let us use what is actually in the entry field and we are seeing, not what is in the clipper
output = (subprocess.check_output(["kodi-cli", "-q", self.youtube_entry.get_text()]))
output = (output.decode('ascii'))
self.output_text.set_text(output)
return True
def on_fullscreen_toggle(self, widget):
#output=(subprocess.call(["kodi-cli","-f"]))
output = (subprocess.check_output(["kodi-cli","-f"]))
output = (output.decode('ascii'))
self.output_text.set_text(output)
return True
def on_toggle_playpause(self, widget):
#output=(subprocess.call(["kodi-cli","-f"]))
output = (subprocess.check_output(["kodi-cli","-p"]))
output = (output.decode('ascii'))
self.output_text.set_text(output)
return True
def on_play_now(self, widget):
#output = (subprocess.check_output(["kodi-cli", "-y", self.clipboard_value]))
# let us use what is actually in the entry field and we are seeing, not what is in the clipper
output = (subprocess.check_output(["kodi-cli", "-y", self.youtube_entry.get_text()]))
print(output)
output = (output.decode('ascii'))
self.output_text.set_text(output)
# We need to put return True in order to keep the loop running after we call this function.
return True
def on_espace_press(self, widget, event):
#print(event.keyval)
# Following will quit the Window on pressing Espace
# we got the value from the previous line
if event.keyval == 65307 :
Gtk.main_quit()
def on_altd_pressed(self, *args):
# http://askubuntu.com/questions/655452/python-gtk3-keyboard-accelerators
# Let's try if alt D pressed is recognized
#print("alt D pressed")
self.clipboard = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
self.clipboard_value = self.clipboard.wait_for_text()
self.youtube_entry.set_text(self.clipboard_value)
# Let's try to give it focus as well:
self.set_focus(self.youtube_entry)
win = GridWindow()
win.connect("delete-event", Gtk.main_quit)
win.connect('key-press-event', win.on_espace_press)
win.show_all()
Gtk.main()