-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adroit.py
552 lines (496 loc) · 19.1 KB
/
Adroit.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
from __future__ import unicode_literals
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.image import Image
from kivy.uix.screenmanager import Screen
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from vlcsmart import smartVlc, smartPauseStopped
from kivy.core.window import Window
from threading import Thread
from BasicInfo import getName, getNamePath
from lyrics import getLyrics
import os
import sys
from queue import Queue
from subtitle import Subtitle
from telegram import *
from time import sleep
from kivy.properties import *
from kivymd.theming import ThemeManager
from kivymd import snackbar as Snackbar
from kivy.factory import Factory
import requests
import smartPlaylist
from kivymd.toolbar import Toolbar
from kivymd.selectioncontrols import MDSwitch
from kivymd.navigationdrawer import NavigationDrawer
from kivymd.selectioncontrols import MDCheckbox
from kivymd.selectioncontrols import MDCheckbox
from kivymd.textfields import MDTextField
from youtube import ytubePlayer
import logging
SONG_LYRICS = False
LYRICS_FLAG = False # Will be true if SONG_LYRICS is turned to True
SONG_PATH = ''
AUTOPLAY = True
TELE_TOKEN = ''
autoplayTrueAtSomeTime = False
class HoverBehavior(object):
"""Hover behavior.
:Events:
`on_enter`
Fired when mouse enter the bbox of the widget.
`on_leave`
Fired when the mouse exit the widget
"""
hovered = BooleanProperty(False)
border_point = ObjectProperty(None)
'''Contains the last relevant point received by the Hoverable. This can
be used in `on_enter` or `on_leave` in order to know where was dispatched the event.
'''
def __init__(self, **kwargs):
self.register_event_type('on_enter')
self.register_event_type('on_leave')
Window.bind(mouse_pos=self.on_mouse_pos)
super(HoverBehavior, self).__init__(**kwargs)
def on_mouse_pos(self, *args):
if not self.get_root_window():
return # do proceed if I'm not displayed <=> If have no parent
pos = args[1]
# Next line to_widget allow to compensate for relative layout
inside = self.collide_point(*self.to_widget(*pos))
if self.hovered == inside:
# We have already done what was needed
return
self.border_point = pos
self.hovered = inside
if inside:
self.dispatch('on_enter')
else:
self.dispatch('on_leave')
def on_enter(self):
pass
def on_leave(self):
pass
Factory.register('HoverBehavior', HoverBehavior)
class ImageButton(ButtonBehavior, Image, HoverBehavior):
def __init__(self, **kwargs):
super(ImageButton, self).__init__(**kwargs)
self.flag = 0
self.source = 'atlas://data/smartPauseRed/off'
# self.source = 'data/mid.jpg'
self.allow_stretch = True
def on_press(self):
if self.flag == 0:
# self.pos_hint={'x': .075, 'y': .7}
self.source = 'atlas://data/smartPauseRed/on'
# self.source = 'data/on.png'
self.flag = 1
else:
# self.pos_hint={'x': .075, 'y': .7}
self.source = 'atlas://data/smartPauseRed/off'
# self.source = 'data/off.png'
self.flag = 0
def on_enter(self, *args):
if self.flag == 0:
self.source = 'atlas://data/smartPauseRed/offhover'
# self.source = 'data/mid.jpg'
else:
self.source = 'atlas://data/smartPauseRed/onhover'
# self.source = 'data/mid.jpg'
def on_leave(self, *args):
if self.flag == 0:
self.source = 'atlas://data/smartPauseRed/off'
# self.source = 'data/off.png'
else:
self.source = 'atlas://data/smartPauseRed/on'
# self.source = 'data/on.png'
class FlatTextInput2(TextInput):
def __init__(self, **kargs):
if 'background_color' not in kargs.keys():
kargs['background_color'] = [0, 0, 0, 0]
super(FlatTextInput2, self).__init__(**kargs)
class Search(FloatLayout):
def __init__(self, **kwargs):
super(Search, self).__init__(**kwargs)
self.flag = 0
# user_input = TextInput()
self.user_input = MDTextField(multiline=False, font_size=23)
self.user_input.hint_text = 'Song Name'
self.add_widget(self.user_input)
self.user_input.size_hint = (0.70, 0.115)
self.user_input.pos_hint = {'x': .090, 'y': .47}
name = getName()
print "Current ", name
if name != '':
name = name.strip()
if name[-4:] == 'webm':
name = name[:-1]
name = name[:-4]
self.user_input.text = name
sleep(0.01) # To make the next line always work.
self.user_input.cursor = (0, 0)
def update(self):
status = getStatus()
if status:
name = getName()
name = name.strip()
if name[-4:] == 'webm':
name = name[:-1]
name = name[:-4]
self.user_input.text = name
sleep(0.01) # To make the next line always work.
self.user_input.cursor = (0, 0)
else:
try:
name = requests.get('http://127.0.0.1:5000/getSong').text
name = name.strip("'b")
self.user_input.text = name
except:
pass
# Declare both screens
class LandingScreen(Screen):
def __init__(self, *args, **kwargs):
super(LandingScreen, self).__init__(*args, **kwargs)
self.smartPause = False
self.curr_player = None
self.sub = False
self.subname = ''
self.telegram = False
self.showLyrics = SONG_LYRICS
def controlSmartPause(self, value, root):
if value == 0: # It is going to onpress of ImageButton class first
self.smartPause = True
print "Smart Pause on."
root.wmplabelid.color = (0, 0, 0, 1)
root.vlclabelid.color = (0, 0, 0, 1)
root.ytlabel.color = (0, 0, 0, 1)
self.player = smartVlc()
else:
stopthread = Thread(target=self.player.stop)
stopthread.daemon = True
stopthread.start()
stopthread.join()
root.vid.active = False
root.wid.active = False
root.wmplabelid.color = (0, 0, 0, 0.6)
root.vlclabelid.color = (0, 0, 0, 0.6)
root.ytlabel.color = (0, 0, 0, 0.6)
self.smartPause = False
print "Smart Pause is off."
def telegramHelper(self, instance):
try:
self.tele.start()
except VLCConnectionError:
print "Telegram Unable to connect to VLC"
sleep(0.01)
instance.active = False
except NetworkError:
print "Telegram Unable to connect to Internet."
sleep(0.01)
instance.active = False
except Exception as e:
print "Telegram Error", str(e)
instance.active = False
def Switch_on_Telegram(self, instance, value):
if value is True:
print("Trying to switch on Telegram")
try:
self.tele = Telegram()
self.telegram = True
print "Switched on."
telethread = Thread(target=self.telegramHelper, args=(instance,))
telethread.daemon = True
telethread.start()
except VLCConnectionError:
print "Unable to connect to VLC."
print "Ensure it is running in telnet mode."
instance.active = False
else:
if self.telegram:
self.tele.stop()
self.telegram = False
print("Switch Off Telegram")
def Switch_on_VLC(self, instance, value, instance2, instance3):
print "VLC switched."
sleep(0.01)
if self.smartPause and value is True:
print("Switch On VLC")
self.curr_player = 'vlc'
instance2.active = False
sleep(0.01)
self.player = smartVlc()
val = self.player.connect('vlc')
if not val:
self.player.stop()
instance.active = False
else:
vthread = Thread(target=self.player.start)
vthread.daemon = True
vthread.start()
elif self.smartPause and value is False:
print("Switch Off VLC")
if self.curr_player == 'vlc':
try:
self.player.stop()
except:
pass
self.curr_player = None
elif self.smartPause is False:
instance.active = False
if self.curr_player == 'vlc':
print "Switch off VLC."
try:
self.player.stop()
except:
pass
self.curr_player = None
def Switch_on_YouTube(self, instance, value, instance2, instance3):
if self.smartPause and value is True:
print "Switch on youtube"
self.curr_player = 'ytube'
instance2.active = False
instance3.active = False
sleep(0.01)
self.player.connect('ytube')
y2thread = Thread(target=self.player.start)
y2thread.daemon = True
y2thread.start()
elif self.smartPause and value is True:
print "Switch off youtube"
if self.curr_player == 'ytube':
self.player.stop()
self.curr_player = None
elif self.smartPause is False:
instance.active = False
if self.curr_player == 'ytube':
print "Switch off youtube."
self.player.stop()
self.curr_player = None
elif value is False:
if self.curr_player == 'ytube':
print "Switch off YouTube."
self.player.stop()
self.curr_player = None
def Switch_on_WMP(self, instance, value, instance2, instance3):
if self.smartPause and value is True:
print("Switch On WMP")
self.curr_player = 'wmp'
instance2.active = False
sleep(0.01)
self.player = smartVlc()
self.player.connect('wmp')
v2thread = Thread(target=self.player.start)
v2thread.daemon = True
v2thread.start()
elif self.smartPause and value is False:
print "Switch off WMP"
if self.curr_player == 'wmp':
try:
self.player.stop()
except:
pass
self.curr_player = None
elif self.smartPause is False:
instance.active = False
if self.curr_player == 'wmp':
print "Switch off WMP."
try:
self.player.stop()
except:
pass
self.curr_player = None
def Switch_on_Prediction(self, instance, value):
print value
def downloadsubhelper(self, name, path):
q4 = Queue()
new = Subtitle(name, path, q4)
subReturn = q4.get()
if not subReturn[0]:
Snackbar.make("Unable to connect to Internet.")
return
try:
q2 = Queue()
sthread = Thread(target=new.download, args=(q2,))
sthread.daemon = True
sthread.start()
sthread.join(timeout=60)
except:
print "Can't download Subtitle."
Snackbar.make("Subtitle not Downloaded.")
return
result = None
try:
result = q2.get()
except:
pass
if result:
print "Subtitle Downloaded"
self.sub = True
Snackbar.make("Subtitle Downloaded Successfully :-)")
else:
print 'Subtitle not Downloaded.'
Snackbar.make("Subtitle not found, Sorry!")
self.sub = False
def downloadsub(self):
name, path = getNamePath()
print name, path
if name == '':
return
if name == self.subname and self.sub:
print "Subtitle Already Downloaded."
Snackbar.make("Subtitle Already Downloaded")
return
if name != self.subname:
self.sub = False
Snackbar.make("Searching for Subtitle")
self.subname = name
subthread = Thread(target=self.downloadsubhelper, args=(name, path))
subthread.daemon = True
try:
subthread.start()
except Exception as e:
print str(e)
class LyricsScreen(Screen):
def __init__(self, *args, **kwargs):
super(LyricsScreen, self).__init__(*args, **kwargs)
self.lyrics = ""
self.song = ''
def loadLyricshelper(self, song):
q = Queue()
getLyrics(song, q, SONG_LYRICS)
self.lyrics = q.get()
self.lyrics = self.lyrics.encode('utf-8')
self.lyrics2.text = self.lyrics
self.lyrics2.cursor = (0, 0)
def loadLyrics(self, song):
global LYRICS_FLAG
if (self.song == song and LYRICS_FLAG is False) and self.lyrics != 'Sorry, Lyrics not found.':
return
else:
LYRICS_FLAG = True
self.song = song
self.lyrics2.text = "Searching Lyrics for " + song + " ..."
print "Loading Lyrics for " + song + " ..."
threadl = Thread(target=self.loadLyricshelper, args=(song,))
threadl.daemon = True
threadl.start()
class HelpScreen(Screen):
def __init__(self, *args, **kwargs):
super(HelpScreen, self).__init__(*args, **kwargs)
self.help_text = '''\nAdroit enhances your media viewing experience with its awesome UI and loads of features. :-)
Have you ever thought how nice it would be if your player can sense you looking at the screen and pause when you are looking away. Adroit lets you do that simply by enabling smart pause.\
You can use the smart pause for VLC, WMP as well as whille watching videos on youtube.
Doesn't it becomes a nuisance when you are doing something important and the playlist stops? Not anymore. Adroit takes over your playlist and never let the music stop. If the current playing song\
is last in the playlist it adds a new song based on your past behaviour. Adroit becomes intellligent with time as it understands you better. Based on what kind of songs you listen based on day and time of\
day it automatically tunes it recommendations. Don't like the song? Just skip it and Adroit will know.
You can use the lyrics feature to instantly get the lyrics of whatever song you want saving you hassle of searching on the internet.
You can also download the subtitle (powered by www.OpenSubtitles.org of movie or TV series episode you are playing. And as the subtitles are retrieved with the help of the hash you can rest asure about its syncing.
Not only this you can also turn on the Telegram feature of Adroit and use the telegram messenger in your mobile to control your media remotely by just chatting with Adroit.\
You can also send lyrics to the bot to get the lyrics of whatever song is being played locally or on youtube.
Never thought it would be this simple, right?
Developer:- Tushar Tripathi
Designer:- Harshil Chaudhary
BITS Pilani'''
class SettingsScreen(Screen):
def __init__(self, *args, **kwargs):
super(SettingsScreen, self).__init__(*args, **kwargs)
self.teletoken = TELE_TOKEN
self.path = SONG_PATH
self.autoplay = AUTOPLAY
self.lyrics = SONG_LYRICS
def save(self, token, path, autop, lyrics):
token = token.strip()
path = path.strip()
global TELE_TOKEN, AUTOPLAY, SONG_PATH, SONG_LYRICS, LYRICS_FLAG, autoplayTrueAtSomeTime
if token != self.teletoken:
self.teletoken = token
TELE_TOKEN = token
with open('data/token.txt', 'w') as hand:
hand.write(self.teletoken)
if path != self.path:
self.path = path
SONG_PATH = path
autoPlayObj.stop
smartPlay = Thread(target=autoPlayObj.main)
smartPlay.daemon = True
smartPlay.start()
with open('data/songfolder.txt', 'w') as hand:
hand.write(self.path)
if lyrics != self.lyrics:
self.lyrics = lyrics
if SONG_LYRICS is False and lyrics is True:
LYRICS_FLAG = True
SONG_LYRICS = lyrics
with open('data/songlyrics.txt', 'w') as hand:
hand.write(str(self.lyrics))
if autop != self.autoplay:
self.autoplay = autop
AUTOPLAY = autop
if AUTOPLAY is True:
smartPlay = Thread(target=autoPlayObj.main)
smartPlay.daemon = True
smartPlay.start()
else:
autoPlayObj.stop()
with open('data/autoplay.txt', 'w') as hand:
hand.write(str(self.autoplay))
class AdroitApp(App):
theme_cls = ThemeManager()
theme_cls.primary_palette = 'Red'
# def build(self):
# Window.borderless = True
def dataloader():
global TELE_TOKEN, AUTOPLAY, SONG_PATH, SONG_LYRICS
if not os.path.exists('data/token.txt'):
with open('data/token.txt', 'w') as hand:
hand.write('')
if not os.path.exists('data/songlyrics.txt'):
with open('data/songlyrics.txt', 'w') as hand:
hand.write('False')
if not os.path.exists('data/autoplay.txt'):
with open('data/autoplay.txt', 'w') as hand:
hand.write('True')
if not os.path.exists('data/songfolder.txt'):
with open('data/songfolder.txt', 'w') as hand:
hand.write('')
with open('data/token.txt') as hand:
for line in hand:
TELE_TOKEN = line.strip()
break
with open('data/songlyrics.txt') as hand:
for line in hand:
if line.strip().lower() == 'false':
SONG_LYRICS = False
else:
SONG_LYRICS = True
break
with open('data/autoplay.txt') as hand:
for line in hand:
if line.strip().lower() == 'false':
AUTOPLAY = False
else:
AUTOPLAY = True
break
with open('data/songfolder.txt') as hand:
for line in hand:
SONG_PATH = line.strip()
break
if __name__ == '__main__':
sys.stdout = open('logger.log', 'w')
logging.basicConfig(filename='logger.log', level=logging.DEBUG)
ytube = ytubePlayer()
ythread = Thread(target=ytube.start)
ythread.daemon = True
dataloader()
ythread.start()
# Playlist Prediction
autoPlayObj = smartPlaylist.PlayPrediction()
if AUTOPLAY:
smartPlay = Thread(target=autoPlayObj.main)
smartPlay.daemon = True
smartPlay.start()
AdroitApp().run()
ytube.stop()