-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainScene.py
333 lines (259 loc) · 8.28 KB
/
MainScene.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
from PySide.QtGui import *
from PySide.QtCore import *
from Widgets import *
import threading
import urllib2
import time
IMG_WIDTH = 480
IMG_HEIGHT = 360
class MainWindow(QFrame):
newPicture = Signal(str)
def __init__(self, win):
super(MainWindow, self).__init__(win)
self.win = win
self.initUI()
def initUI(self):
self.layout = QVBoxLayout()
self.setLayout(self.layout)
self.createWidgets()
# create settings
self.songs = []
self.queue_songs = []
self.currentVolume = 50
self.currentSong = ""
self.isLoading = False
self.isPlaying = False
self.isMute = False
# set Functions
def setBackend(self, bg):
self.backend = bg
# get last volume
self.currentVolume = int(self.backend.info.data['Player']['volume'])
self.volume.setValue(self.currentVolume)
self.newPicture.connect(self.setImage)
def setImage(self, imglink):
imglink = str(imglink)
data = urllib2.urlopen( imglink ).read()
image = QImage()
image.loadFromData(data)
pixmap = QPixmap(image).scaled(
IMG_WIDTH, IMG_HEIGHT)
self.song_image.setPixmap( pixmap )
def startThread(self, func, *args):
t = threading.Thread(target=func,args=args)
t.daemon = True
t.start()
# Create Widgets
def createWidgets(self):
first_layer = QHBoxLayout()
top_layer = QHBoxLayout()
# First Layer
self.search_bar = SearchBar()
self.search_btn = SearchButton("Search")
self.search_bar.returnPressed.connect(self.addToQueue)
self.search_btn.clicked.connect(self.addToQueue)
first_layer.addWidget(self.search_bar)
first_layer.addWidget(self.search_btn)
# Top layer
# buttons
self.start_btn = ImageButton()
self.back_btn = ImageButton()
self.skip_btn = ImageButton()
self.start_btn.setImage("resources/pictures/pause.png",[48,48])
self.back_btn.setImage("resources/pictures/rewind.png",[48,48])
self.skip_btn.setImage("resources/pictures/forward.png",[48,48])
self.start_btn.clicked.connect(self.changeState)
self.back_btn.clicked.connect(self.rewind)
self.skip_btn.clicked.connect(self.forward)
top_layer.addWidget(self.back_btn)
top_layer.addWidget(self.start_btn)
top_layer.addWidget(self.skip_btn)
# remove button
self.remove_btn = SearchButton("Remove Song")
self.remove_btn.clicked.connect(self.removeSong)
remove_layout = QHBoxLayout()
remove_layout.addStretch(1)
remove_layout.addWidget(self.remove_btn)
# volume
self.volume = VolumeSlider()
self.currentVolume = 50
self.volume.setRange(0,100)
self.volume.setPageStep(1)
self.volume.setValue(50)
self.volume.setMinimumWidth(80)
self.volume.valueChanged.connect(self.changeVolume)
self.volume_btn = ImageButton()
self.volume_btn.setImage("resources/pictures/vol_on.png",[24,24])
self.volume_btn.clicked.connect(self.changeMute)
top_layer.addStretch(1)
top_layer.addWidget(self.volume_btn)
top_layer.addWidget(self.volume)
# the Rest
self.title = SongTitle("Initializing...")
self.song_image = ImageButton()
self.song_image.setAlignment(Qt.AlignCenter)
# add to layout
self.layout.addLayout(first_layer)
self.layout.addLayout(top_layer)
self.layout.addLayout(remove_layout)
self.layout.addWidget(self.title)
self.layout.addWidget(self.song_image)
# Stream actions
def newStream(self):
# For rewind
if self.songs != []:
if self.backend.place < len(self.songs):
info = self.songs[self.backend.place]
self.backend.place += 1
self.setupStream(info)
return True
# For songs in queue
if self.queue_songs != []:
info = self.queue_songs[-1]
self.songs.append(info)
self.queue_songs.pop(-1)
self.backend.place += 1
self.setupStream(info)
return True
# For normal operations
else:
url = self.backend.getNextSong()
info = self.backend.yi.getInfo(url)
self.backend.place += 1
self.songs.append(info)
self.setupStream(info)
def setupStream(self, info):
if self.isLoading:
return False
self.isLoading = True
self.currentSong = info[1].split('/')[-2]
# Set info
self.title._setText( info[0] )
if bool(self.backend.info.data['Player']['getimage']):
self.newPicture.emit( info[1] )
self.backend.stream( info[-1] )
# When stream is done, move on
self.backend.pipeline.connect('about-to-finish',
self.startStream)
# Get volume suppressor
surpressor = float(self.backend.info.data['Player']['vol_supp_ratio'])*1.0
# Keep setting stream volume (Bug with GStreamer)
for i in range(10):
self.backend.setVolume( self.currentVolume / surpressor / 100.0 )
time.sleep(0.1)
self.isPlaying = True
self.isLoading = False
def startStream(self, *args):
self.title._setText("Loading...")
self.startThread(self.newStream)
def rewind(self):
# Dont Spam
if self.isLoading:
return False
# Rewind array
if self.backend.place != -1:
if self.backend.place == 1:
self.backend.place = 0
else:
self.backend.place -= 2
self.startStream()
def forward(self):
# Dont spam
if self.isLoading:
return False
self.startStream()
# update functions
def changeVolume(self, val):
# supress volume because its originally loud
surpressor = float(self.backend.info.data['Player']['vol_supp_ratio'])*1.0
if not self.isMute:
self.currentVolume = val
self.backend.info.changeValue('Player','volume',self.currentVolume)
self.backend.setVolume( val / surpressor / 100.0 )
def changeMute(self):
if self.isMute:
self.isMute = False
self.volume_btn.setImage("resources/pictures/vol_on.png",[24,24])
surpressor = float(self.backend.info.data['Player']['vol_supp_ratio'])*1.0
self.backend.setVolume( self.currentVolume / surpressor / 100.0 )
else:
self.isMute = True
self.volume_btn.setImage("resources/pictures/vol_off.png",[24,24])
self.backend.setVolume(0.0)
def changeState(self):
if self.isPlaying:
try:
self.backend.pause()
self.start_btn.setImage("resources/pictures/play.png",[48,48])
self.isPlaying = False
except Exception as e:
print str(e)
else:
try:
self.backend.play()
self.start_btn.setImage("resources/pictures/pause.png",[48,48])
self.isPlaying = True
except:
pass
# Playlist functions
def addToQueue(self, *args):
self.startThread(self.QueueInfo)
def QueueInfo(self):
text = str(self.search_bar.text())
self.search_bar.clear()
# No whitespace / empty search
if text == '' or text.isspace():
return False
# Get info and add to queue
self.title._setText("Loading search...")
video_id = self.backend.yi.getSearchResults( text )
info = self.backend.yi.getInfo( video_id )
self.queue_songs.append( info )
# Get all video ids for reference
all_ids = []
for x in self.backend.urls:
# Get only video id
_id = x
if 'watch?v' in _id:
_id = _id.split('watch?v=')[-1]
if '&' in _id:
_id = _id.split('&')[0]
all_ids.append(_id)
# If not already in list, add to database
if video_id not in all_ids:
# Add to backup file
path = self.backend.info.data['Player']['backup']
with open(os.path.join('resources',path), 'a') as f:
f.write("\nhttps://www.youtube.com/watch?v=" + video_id)
# Start
self.startThread(self.newStream)
def removeSong(self, *args):
self.title._setText("Removing Song...")
# Get current list
path = self.backend.info.data['Player']['backup']
with open( os.path.join('resources',path), 'r' ) as f:
current_list = f.read().splitlines()
for x in current_list:
if x == '':
current_list.pop(current_list.index(x))
# Find song and remove it
for song in current_list:
# Get only their id
_id = song
if 'watch?v' in _id:
_id = _id.split('watch?v=')[-1]
if '&' in _id:
_id = _id.split('&')[0]
# If Video_id matches, remove song
if _id == self.currentSong:
current_list.pop( current_list.index(song) )
# Rewrite list
with open( os.path.join('resources',path), 'w' ) as f:
for song in current_list:
if song == current_list[-1]:
f.write(song)
else:
f.write(song + '\n')
# Remove from list and Goto next song
self.songs.pop(-1)
self.startThread(self.newStream)