/
interface.py
415 lines (363 loc) · 17.9 KB
/
interface.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import sys,os
from downloader import download
from downloader import download_pafy
from combo_box import ComboDemo
from translucid_button import TranslucidButton
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QLineEdit
from PyQt5.QtWidgets import QPushButton, QLabel, QFrame
from PyQt5.QtGui import QIcon, QPixmap, QPalette, QColor
from graphical_object import GraphicalObject
import pafy
import random
import string
def read_css(fname):
with open(fname) as f:
content = f.read()
return content
class Interface:
def end_of_play_list(self):
pass
def update_title(self,title):
print("----------")
print(title)
print("----------")
class GraphicalInterface(QMainWindow,Interface):
def __init__(self, video_player, debug = False):
self.__app = QApplication(sys.argv)
super().__init__()
self.__video_player = video_player
#TODO get in .config the value of the volume by default
self.__video_player.set_volume(100)
# Property of the main window
self.__width = 0
self.__height = 0
# List of all derivated object from the following QWidgets
self.__palette = None
self.__logo_image = None
self.__previousbtn_image = None
self.__skipbtn_image = None
self.__playbtn_image = None
self.__pausebtn_image = None
# List of all the QWidget present in the main window
self.__videotitle = None
self.__searchbtn = None
self.__searchbar = None
self.__mainbox = None
self.__modebox = None
self.__logo = None
self.__video_reader = None
self.__previousbtn_audio = None
self.__playbtn_audio = None
self.__skipbtn_audio = None
self.__previousbtn_video = None
self.__playbtn_video = None
self.__skipbtn_video = None
self.__buttonbar_video = None
# List of all the GraphicalObject in the main window
self.__header = None
self.__footer = None
self.__body = None
self.__gr_videotitle = None
self.__gr_searchbar = None
self.__gr_searchbtn = None
self.__gr_modebox = None
self.__gr_logo = None
self.__gr_video_reader = None
self.__gr_buttonbar_video = None
self.__gr_previousbtn_audio = None
self.__gr_playbtn_audio = None
self.__gr_skipbtn_audio = None
self.__gr_previousbtn_video = None
self.__gr_playbtn_video = None
self.__gr_skipbtn_video = None
self.__object_list = []
self.__debug = debug
def main(self):
"""
Main loop of the graphic interface
"""
self.resize(640, 480)
self.move(0, 0)
self.setWindowTitle('Youtube Reader')
self.setWindowIcon(QIcon('resources/icon.svg'))
content = read_css("./css/main.css")
self.setStyleSheet(content)
self.__mainbox = GraphicalObject(self, width = 640, height = 480, pos_x = 0, pos_y = 0)
self.__header = GraphicalObject(None, width = 100, height = 10, pos_x = 0, pos_y = 0, parent = self.__mainbox)
self.__searchbar = QLineEdit(self)
self.__searchbar.returnPressed.connect(self.handle_research)
self.__gr_searchbar = GraphicalObject(self.__searchbar, width = 70, height = 60, pos_x = 15, pos_y = 20, parent = self.__header)
self.__searchbtn = QPushButton('Search', self)
self.__searchbtn.clicked.connect(self.handle_research)
self.__gr_searchbtn = GraphicalObject(self.__searchbtn, width = 10, height = 60, pos_x = 87, pos_y = 20, parent = self.__header)
self.__logo_image = QPixmap('resources/logo.png')
self.__logo = QLabel(self)
self.__logo.setScaledContents(True)
self.__logo.setPixmap(self.__logo_image)
self.__gr_logo = GraphicalObject(self.__logo, width = 15, height = 60, pos_x = 0, pos_y = 20, parent = self.__header)
self.__body = GraphicalObject(None, width = 100, height = 80, pos_x = 0, pos_y = 10, parent = self.__mainbox)
self.create_reader()
self.set_player_mode(self.__video_player.get_mode())
self.__footer = GraphicalObject(None, width = 100, height = 10, pos_x = 0, pos_y = 90, parent = self.__mainbox)
self.__modebox = ComboDemo(self, self)
self.__modebox.init_combo_box(self.__video_player.get_mode())
self.__gr_modebox = GraphicalObject(self.__modebox, width = 20, height = 100, pos_x = 80, pos_y = 20, parent = self.__footer)
self.show()
self.__app.exec_()
self.__video_player.stop_stream()
def handle_research(self):
my_string = self.__searchbar.text()
self.__searchbar.clear()
if "www.youtube.com/" in my_string:
self.__video_player.add_url(my_string)
else:
print("Search functionnality not implemented yet. Put url please!")
def create_reader(self):
# code from github.com/devos50/vlc-pyqt5-example.git
# Video UI
if sys.platform == "darwin" :
from PyQt5.QtWidgets import QMacCocoaViewContainer
self.__video_reader = QMacCocoaViewContainer(0)
else:
self.__video_reader = QFrame(self)
self.__palette = self.__video_reader.palette()
self.__palette.setColor (QPalette.Window,
QColor(0,0,0))
self.__video_reader.setPalette(self.__palette)
self.__video_reader.setAutoFillBackground(True)
self.__gr_video_reader = GraphicalObject(self.__video_reader, width = 80, height = 80, pos_x = 10, pos_y = 10, parent = self.__body)
self.__videotitle = QLabel("No Video!", self)
self.__videotitle.setWordWrap(True)
self.__gr_videotitle = GraphicalObject(self.__videotitle, width = 80, height = 10, pos_x = 10, pos_y = 90, parent = self.__body)
number_of_button = 3
btn_height = 7
btn_width = 7
ui_origin_x = 0
ui_origin_y = 100 - btn_height
self.__buttonbar_video = QLabel(self)
self.__buttonbar_video.setStyleSheet("QLabel { background-color : white; color : blue; }");
self.__gr_buttonbar_video = GraphicalObject(self.__buttonbar_video, width = 100, height = btn_height, pos_x = 0, pos_y = ui_origin_y, parent = self.__gr_video_reader)
self.__previousbtn_video = TranslucidButton(self)
self.__previousbtn_image = QPixmap('resources/back.svg')
self.__previousbtn_video.setScaledContents(True)
self.__previousbtn_video.setPixmap(self.__previousbtn_image)
self.__gr_previousbtn_video = GraphicalObject(self.__previousbtn_video, width = btn_width, height = btn_height, pos_x = ui_origin_x+0*btn_width, pos_y = ui_origin_y, parent = self.__gr_video_reader)
self.__skipbtn_video = TranslucidButton(self)
self.__skipbtn_image = QPixmap('resources/skip.svg')
self.__skipbtn_video.setScaledContents(True)
self.__skipbtn_video.setPixmap(self.__skipbtn_image)
self.__gr_skipbtn_video = GraphicalObject(self.__skipbtn_video, width = btn_width, height = btn_height, pos_x = ui_origin_x+2*btn_width, pos_y = ui_origin_y, parent = self.__gr_video_reader)
self.__playbtn_video = TranslucidButton(self)
self.__playbtn_video.clicked.connect(self.video_play_pause)
self.__playbtn_image = QPixmap('resources/play.svg')
self.__playbtn_video.setScaledContents(True)
self.__playbtn_video.setPixmap(self.__playbtn_image)
self.__gr_playbtn_video = GraphicalObject(self.__playbtn_video, width = btn_width, height = btn_height, pos_x = ui_origin_x+1*btn_width, pos_y = ui_origin_y, parent = self.__gr_video_reader)
# Audio UI
number_of_button = 3
btn_height = 5
btn_width = 10
ui_origin_x = 30
ui_origin_y = 50 - (btn_height/2)
self.__previousbtn_audio = QPushButton('Previous', self)
#clicked.connect(self.handle_research)
self.__gr_previousbtn_audio = GraphicalObject(self.__previousbtn_audio, width = btn_width, height = btn_height, pos_x = ui_origin_x+(0*(100-2*ui_origin_x))/number_of_button, pos_y = ui_origin_y, parent = self.__body)
self.__playbtn_audio = QPushButton('Play', self)
self.__playbtn_audio.clicked.connect(self.audio_play_pause)
self.__gr_playbtn_audio = GraphicalObject(self.__playbtn_audio, width = btn_width, height = btn_height, pos_x = ui_origin_x+(1*(100-2*ui_origin_x))/number_of_button, pos_y = ui_origin_y, parent = self.__body)
self.__skipbtn_audio = QPushButton('Skip', self)
self.__skipbtn_audio.clicked.connect(self.__video_player.skip)
self.__gr_skipbtn_audio = GraphicalObject(self.__skipbtn_audio, width = btn_width, height = btn_height, pos_x = ui_origin_x+(2*(100-2*ui_origin_x))/number_of_button, pos_y = ui_origin_y, parent = self.__body)
def set_player_mode(self, value):
self.__video_player.set_mode(value)
if value == 'Video':
self.__video_reader.show()
self.__previousbtn_audio.hide()
self.__playbtn_audio.hide()
self.__skipbtn_audio.hide()
else:
self.__video_reader.hide()
self.__previousbtn_audio.show()
self.__playbtn_audio.show()
self.__skipbtn_audio.show()
def end_of_play_list(self):
print("End of stream")
self.__palette.setColor (QPalette.Window,
QColor(0,0,0))
self.__video_reader.setPalette(self.__palette)
def update_title(self, title):
self.__videotitle.setText(title)
def audio_play_pause(self):
if self.__video_player.is_running():
self.pause_start()
else:
self.__playbtn_audio.setText('Pause')
self.__video_player.play_stream()
def video_play_pause(self):
print("Play pause")
if self.__video_player.is_running():
self.pause_start()
else :
print("Launching the stream")
if sys.platform.startswith('linux'): # for Linux using the X Server
self.__video_player.get_player().set_xwindow(self.__video_reader.winId())
elif sys.platform == "win32": # for Windows
self.__video_player.get_player().set_hwnd(self.__video_reader.winId())
elif sys.platform == "darwin": # for MacOS
self.__video_player.get_player().set_nsobject(int(self.__video_reader.winId()))
#TODO LAPALETTE
self.__video_player.play_stream()
if self.__video_player.is_playing():
self.__palette.setColor (QPalette.Window,
QColor(255,255,255))
self.__video_reader.setPalette(self.__palette)
self.__video_reader.setAutoFillBackground(True)
def pause_start(self):
if self.__video_player.is_paused():
print("Unpausing the player")
self.__playbtn_audio.setText('Pause')
self.__video_player.resume_stream()
elif self.__video_player.is_playing():
print("Pausing the player")
self.__playbtn_audio.setText('Play')
self.__video_player.pause_stream()
else:
print("Restarting the stream")
self.__playbtn_audio.setText('Pause')
self.__video_player.play_stream()
# Redefinition of the QMainWindow built-in methods
def mousePressEvent(self, event):
# Left click
if event.button() == 1:
if ((event.x() <= (self.__gr_video_reader.getRealWidth()+self.__gr_video_reader.getRealPosX())) and
(event.x() >= self.__gr_video_reader.getRealPosX()) and
(event.y() <= (self.__gr_video_reader.getRealHeight()+self.__gr_video_reader.getRealPosY())) and
(event.y() >= self.__gr_video_reader.getRealPosY()) and
self.__video_player.get_mode()=='Video'):
print("True")
if self.__video_player.is_running():
self.pause_start()
else :
print("Launching the stream")
if sys.platform.startswith('linux'): # for Linux using the X Server
self.__video_player.get_player().set_xwindow(self.__video_reader.winId())
elif sys.platform == "win32": # for Windows
self.__video_player.get_player().set_hwnd(self.__video_reader.winId())
elif sys.platform == "darwin": # for MacOS
self.__video_player.get_player().set_nsobject(int(self.__video_reader.winId()))
#TODO LAPALETTE
self.__video_player.play_stream()
if self.__video_player.is_playing():
self.__palette.setColor (QPalette.Window,
QColor(255,255,255))
self.__video_reader.setPalette(self.__palette)
self.__video_reader.setAutoFillBackground(True)
print("Plop X "+str(event.x())+" Plop Y "+str(event.y()))
print("Glob X "+str(event.globalX())+" Glob Y "+str(event.globalY()))
def resizeEvent (self, event):
print("OLD height: " + str(event.oldSize().height())+ " width: " + str(event.oldSize().width()))
print("NEW height: " + str(event.size().height())+ " width: " + str(event.oldSize().width()))
self.__width = event.size().width()
self.__height = event.size().height()
self.__mainbox.resize(event.size().width(), event.size().height())
class CommandLineInterface(Interface):
"""
This class provide a command line interface for the music player
"""
def __init__(self, video_player, debug=False):
#define the currently playing video url
self.__url = ""
#define the player to use for this interface
self.__video_player = video_player
#define the list of url to display for the next videos
self.__urls = []
#define if the user ask to quit the command line
self.__continue = True
def set_next_music(self):
self.__video_player.skip()
def get_current_music(self):
print(self.__url)
def __crypt(self):
rand_str = lambda n: ''.join([random.choice(string.ascii_letters+string.punctuation+string.digits) for i in range(n)])
rows, columns = os.popen('stty size', 'r').read().split()
print("")
# Now to generate a random string of length 10
for i in range(0,int(rows)):
s = rand_str(int(columns))
print(s)
def main(self):
sys.stdout.write(">>>")
while self.__continue :
command = input()
commands = command.split(" ")
if commands[0] == "url":
for i in commands [1:]:
self.__video_player.add_url(i)
elif commands[0] == "skip" :
self.set_next_music()
elif commands[0] == "stop" or commands[0] == "quit" or commands[0] == "exit":
self.__continue = False
self.__video_player.stop_stream()
print("Stopping the player")
return
elif commands[0] == "set":
self.__video_player.set_time(int(float(commands[1])))
elif commands[0] == "help":
print("The commands available are: ")
print(" url [list of urls]")
print(" stop")
print(" play")
print(" pause")
print(" skip")
print(" quality <quality> (only Best available)")
print(" mode <Video,Audio>")
print(" set <time in sec>")
print(" dl <file_name>")
print(" vol <volume>")
elif commands[0] == "dl":
if commands[1] == "current":
download_pafy(self.__video_player.get_current(),self.__video_player.get_mode())
elif commands[1] == "playlist":
playlist = self.__video_player.get_playlist()
for i in playlist:
download_pafy(i, self.__video_player.get_mode())
else :
download(commands[1],self.__video_player.get_mode())
elif commands[0] == "set":
#need to parse time over a minute to the format hh:mm:ss
self.__video_player.set_time(int(float(commands[1]))*10)
elif commands[0] == "quality":
self.__video_player.set_quality(commands[1])
elif commands[0] == "mode":
self.__video_player.set_mode(commands[1])
elif commands[0] == "pause":
self.__video_player.pause_stream()
elif commands[0] == "playlist":
self.__video_player.to_play()
elif commands[0] == "history":
self.__video_player.history()
elif commands[0] == "current":
self.__video_player.is_playing()
elif commands[0] == "time":
self.__video_player.get_time()
elif commands[0] == "play":
if self.__video_player.is_running():
if self.__video_player.is_paused():
print("Unpausing the player")
self.__video_player.resume_stream()
elif self.__video_player.is_playing():
print("The player is already playing. Verify your sound level.")
else:
self.__video_player.play_stream()
else :
self.__video_player.play_stream()
elif commands[0] == "vol":
self.__video_player.set_volume(int(commands[1]))
elif commands[0] == "crypt":
self.__crypt()
elif commands[0] == "":
pass
else :
print(commands[0])
print("Command not found!")
sys.stdout.write(">>>")