-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
executable file
·1454 lines (1233 loc) · 56.3 KB
/
main.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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
import sys, os, platform, deezloader.exceptions, json, threading, subprocess, inspect, random, traceback, re
from PyQt5.QtCore import QUrl, pyqtSignal, Qt # , QTimer
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
from PyQt5.QtGui import QIcon, QFont, QPixmap, QCursor, QPainter
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLabel, \
QLineEdit, QSlider, QProgressBar, QGroupBox, QScrollArea, QFrame, QCheckBox, QDialog, QTextEdit, QMenu
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtNetwork import QNetworkCookie
from PyQt5.QtSvg import QSvgRenderer
from configparser import ConfigParser
from importlib.util import module_from_spec, spec_from_file_location
config = ConfigParser()
APP_DEVMODE: bool = False
APP_NAME: str = "Deezium"
if platform.system() == 'Windows':
APP_DATA_PATH: str = "C:\\Program Files\\Deezium\\"
elif platform.system() == 'Linux':
APP_DATA_PATH: str = '/usr/share/deezium/'
else:
print('Sorry, but your Operating System is not supported.')
sys.exit()
if len(sys.argv) > 1:
if sys.argv[1] == '--dev':
print('[D> Devmode enabled!')
APP_DEVMODE = True
if not APP_DEVMODE:
import_spec = spec_from_file_location('api', APP_DATA_PATH + 'deezium_api.py')
api = module_from_spec(import_spec)
sys.modules['api'] = api
import_spec.loader.exec_module(api)
else:
import deezium_api as api
def create_colored_svg(svg_filename: str, text_color):
renderer = QSvgRenderer(svg_filename)
image_size = renderer.defaultSize()
image = QPixmap(image_size)
image.fill(Qt.transparent) # Ensure transparency
painter = QPainter(image)
renderer.render(painter)
painter.setCompositionMode(QPainter.CompositionMode_SourceIn)
painter.fillRect(image.rect(), text_color)
painter.end()
return image
class NoArlDialog(QDialog):
def __init__(self) -> None:
super().__init__()
self.main_layout = QVBoxLayout(self)
title = QLabel('No ARL could be imported')
title.setFont(QFont(title.font().family(), 12))
text1 = QLabel('While logging in no access token could be imported from your Deezer Account.')
text2 = QLabel('To properly use Deezium you must enter your access token:')
self.input = QLineEdit()
self.input.setMaxLength(192)
self.input.setPlaceholderText('Enter your access token here')
self.confirm_button = QPushButton('Continue')
self.confirm_button.setDisabled(True)
self.main_layout.addWidget(title)
self.main_layout.addWidget(text1)
self.main_layout.addWidget(text2)
self.main_layout.addWidget(self.input)
self.main_layout.addWidget(self.confirm_button)
self.input.textChanged(self.update_button)
self.confirm_button.clicked.connect(self.confirm)
def update_button(self):
self.confirm_button.setDisabled(not len(self.input.text()) == 192)
def confirm(self):
with open(os.path.expanduser('~/.config/deezium/arl.dat'), 'w') as f:
f.write(self.input.text())
def closeEvent(self, event) -> None:
self.reject()
super().closeEvent(event)
class ErrorDialog(QDialog):
def __init__(self, trb: str):
super().__init__()
self.main_layout = QVBoxLayout(self)
self.title = QLabel('Shit happens...')
self.title.setFont(QFont(self.title.font().family(), 18))
self.log = QTextEdit()
self.log.setReadOnly(True)
self.log.setHtml("<pre>{}</pre>".format(trb))
self.okay_button = QPushButton('Okay')
self.okay_button.clicked.connect(self.accept)
self.main_layout.addWidget(self.title)
self.main_layout.addWidget(self.log)
self.main_layout.addWidget(self.okay_button)
class QAlbumList(QWidget):
album_clicked = pyqtSignal(int)
artist_clicked = pyqtSignal(int)
def __init__(self, albumids: list[int], deezerclient: api.deezer.Client):
super().__init__()
self.albumids = albumids
self.cli = deezerclient
self.data = []
self.construct_items()
self.main_layout = QVBoxLayout(self)
self.construct_list()
def construct_items(self):
for album in self.albumids:
dt = {}
album = self.cli.get_album(album)
dt['name'] = album.title
dt['artist'] = album.artist
dt['length'] = album.duration
pix = QPixmap()
pix.loadFromData(api.download_albumcover_s(self.cli, album.id))
dt['icon-pixmap'] = pix
dt['dpy-album'] = album
self.data.append(dt)
def construct_list(self):
for album in self.data:
row = QHBoxLayout()
icon_label = QLabel()
icon_label.setPixmap(album['icon-pixmap'])
icon_label.mousePressEvent = lambda event, album_id=album['dpy-album'].id: (
self.album_clicked.emit(album_id))
text_label = QLabel(f"<b>{album['name']}</b> -")
text_label.mousePressEvent = lambda event, album_id=album['dpy-album'].id: (
self.album_clicked.emit(album_id))
artist_label = QLabel(album['artist'].name)
artist_label.mousePressEvent = lambda event, artist_id=album['artist'].id: (
self.artist_clicked.emit(artist_id))
duration = QLabel(f" (<i>{round(album['length'] / 60)} min long</i>)")
row.addWidget(icon_label)
row.addWidget(text_label)
row.addWidget(artist_label)
row.addWidget(duration)
row.addStretch()
self.main_layout.addLayout(row)
class QAlbumHList(QWidget):
album_clicked = pyqtSignal(int)
def __init__(self, albumids: list[int], deezerclient: api.deezer.Client):
super().__init__()
self.albumids = albumids
self.cli = deezerclient
self.data = []
self.construct_items()
self.main_layout = QHBoxLayout(self)
self.construct_list()
def construct_items(self):
for album in self.albumids:
dt = {}
album = self.cli.get_album(album)
dt['name'] = album.title
dt['artist'] = album.artist
dt['id'] = album.id
pix = QPixmap()
pix.loadFromData(api.download_albumcover_m(self.cli, album.id))
dt['icon-pixmap'] = pix
self.data.append(dt)
def construct_list(self):
for album in self.data:
icon_label = QLabel()
icon_label.setPixmap(album['icon-pixmap'])
icon_label.mousePressEvent = lambda event, album_id=album['id']: self.album_clicked.emit(album_id)
icon_label.setToolTip(f"{album['name']} - {album['artist'].name}")
self.main_layout.addWidget(icon_label)
class QHClickList(QWidget):
action = pyqtSignal()
def __init__(self, title: str, pixmap: QPixmap, lasttext: str, hovertext: str):
super().__init__()
self.main_layout = QHBoxLayout(self)
self.pixmap = pixmap
self.title = title
self.lasttext = '(' + lasttext + ')'
self.hovertext = hovertext
self.construct_list()
def construct_list(self):
title = QLabel('<b>' + self.title + '</b')
title.mousePressEvent = lambda event: self.action.emit()
icon_label = QLabel()
icon_label.setPixmap(self.pixmap)
icon_label.setToolTip(self.hovertext)
icon_label.mousePressEvent = lambda event: self.action.emit()
last_label = QLabel(self.lasttext)
last_label.mousePressEvent = lambda event: self.action.emit()
self.main_layout.addWidget(icon_label)
self.main_layout.addWidget(title)
self.main_layout.addWidget(last_label)
self.main_layout.addStretch()
class QArtistList(QWidget):
artist_clicked = pyqtSignal(int)
def __init__(self, artistids: list[int], deezerclient: api.deezer.Client):
super().__init__()
self.artistids = artistids
self.cli = deezerclient
self.data = []
self.construct_items()
self.main_layout = QVBoxLayout(self)
self.construct_list()
def construct_items(self):
for artist in self.artistids:
dt = {}
artist = self.cli.get_artist(artist)
dt['name'] = artist.name
pix = QPixmap()
pix.loadFromData(api.requests.get(artist.picture_small).content)
dt['icon-pixmap'] = pix
dt['dpy-artist'] = artist
self.data.append(dt)
def construct_list(self):
for artist in self.data:
row = QHBoxLayout()
icon_label = QLabel()
icon_label.setPixmap(artist['icon-pixmap'])
icon_label.mousePressEvent = lambda event, artist_id=artist['dpy-artist'].id: (
self.artist_clicked.emit(artist_id))
text_label = QLabel(f"<b>{artist['name']}</b>")
text_label.mousePressEvent = lambda event, artist_id=artist['dpy-artist'].id: (
self.artist_clicked.emit(artist_id))
row.addWidget(icon_label)
row.addWidget(text_label)
row.addStretch()
self.main_layout.addLayout(row)
class QTrackList(QWidget):
artist_clicked = pyqtSignal(int)
album_clicked = pyqtSignal(int)
track_clicked = pyqtSignal(int)
toggle_fav = pyqtSignal(int)
add_queue = pyqtSignal(int)
add_playlist = pyqtSignal(int)
def __init__(self, trackids: list[int], deezerclient: api.deezer.Client, show_album: bool = True,
show_icons: bool = True, show_artist: bool = True):
super().__init__()
self.trackids = trackids
self.cli = deezerclient
self.data = []
self.show_album = show_album
self.show_icons = show_icons
self.show_artist = show_artist
self.favorites: list[int] = api.conv_paginated_ids(deezerclient.get_user_tracks())
self.construct_items()
self.main_layout = QVBoxLayout(self)
self.construct_list()
def construct_items(self):
for trackid in self.trackids:
dt = {}
track = self.cli.get_track(trackid)
if trackid > 0 and track.album.id != 0:
try:
dt['name'] = track.title
dt['artist'] = track.artist
dt['length'] = track.duration
dt['album'] = track.album
dt['album-art'] = track.album.artist
if self.show_icons:
pix = QPixmap()
pix.loadFromData(api.download_albumcover_s(self.cli, track.album.id))
dt['icon-pixmap'] = pix
dt['dpy-track'] = track
self.data.append(dt)
except api.deezer.exceptions.DeezerErrorResponse:
print('[E> Failed processing ' + str(trackid) + ' on deezers end')
print(f'[TRB> AlbumId: {track.album.id} ArtistId {track.artist.id}')
def update_fav(self):
self.favorites = api.conv_paginated_ids(self.cli.get_user_tracks())
def construct_list(self):
for track in self.data:
def openmenu():
self.update_fav()
menu = QMenu(self)
if track['dpy-track'].id in self.favorites:
fav = menu.addAction('Remove from &favorites')
else:
fav = menu.addAction('Add to &favorites')
queue = menu.addAction('Add to &queue')
playlist = menu.addAction('Add to &playlist')
fav.triggered.connect(lambda event, tid=track['dpy-track'].id: self.toggle_fav.emit(tid))
queue.triggered.connect(lambda event, tid=track['dpy-track'].id: self.add_queue.emit(tid))
playlist.triggered.connect(lambda event, tid=track['dpy-track'].id: self.add_playlist.emit(tid))
buttonpos = QCursor.pos()
menu.exec_(buttonpos)
row = QHBoxLayout()
text_label = QLabel(f"<b>{track['name']}</b> -")
text_label.mousePressEvent = lambda event, track_id=track['dpy-track'].id: (
self.track_clicked.emit(track_id))
duration = QLabel(f" (<i>{round(track['length'] / 60)} min long</i>)")
show_menu_button = SVGButton('more')
show_menu_button.setFixedSize(30, 30)
show_menu_button.clicked.connect(openmenu)
if self.show_icons:
icon_label = QLabel()
icon_label.setPixmap(track['icon-pixmap'])
icon_label.mousePressEvent = lambda event, track_id=track['dpy-track'].id: (
self.track_clicked.emit(track_id))
row.addWidget(icon_label)
row.addWidget(text_label)
if self.show_artist:
artist_label = QLabel(track['artist'].name)
artist_label.mousePressEvent = lambda event, artist_id=track['artist'].id: (
self.artist_clicked.emit(artist_id))
row.addWidget(artist_label)
row.addWidget(duration)
row.addStretch()
if self.show_album:
album_label = QLabel(f"in <b>{track['album'].title}</b> by <i>{track['album-art'].name}</i>")
album_label.mousePressEvent = lambda event, album_id=track['album'].id: (
self.album_clicked.emit(album_id))
row.addWidget(album_label)
row.addWidget(show_menu_button)
self.main_layout.addLayout(row)
class QPlayList(QWidget):
playlist_clicked = pyqtSignal(int)
def __init__(self, listids: list[int], deezerclient: api.deezer.Client):
super().__init__()
self.listids = listids
self.cli = deezerclient
self.data = []
self.construct_items()
self.main_layout = QVBoxLayout(self)
self.construct_list()
def construct_items(self):
for playlist in self.listids:
dt = {}
playlist = self.cli.get_playlist(playlist)
dt['name'] = playlist.title
dt['length'] = playlist.duration
dt['author'] = playlist.creator
pix = QPixmap()
pix.loadFromData(api.requests.get(playlist.picture_small).content)
dt['icon-pixmap'] = pix
dt['dpy-track'] = playlist
self.data.append(dt)
def construct_list(self):
for playlist in self.data:
row = QHBoxLayout()
icon_label = QLabel()
icon_label.setPixmap(playlist['icon-pixmap'])
icon_label.mousePressEvent = lambda event, artist_id=playlist['author'].id: (
self.playlist_clicked.emit(artist_id))
text_label = QLabel(f"<b>{playlist['name']}</b> -")
text_label.mousePressEvent = lambda event, artist_id=playlist['author'].id: (
self.playlist_clicked.emit(artist_id))
artist_label = QLabel(playlist['author'].name)
duration = QLabel(f" (<i>{round(playlist['length'] / 60)} min long</i>)")
row.addWidget(icon_label)
row.addWidget(text_label)
row.addWidget(artist_label)
row.addWidget(duration)
row.addStretch()
self.main_layout.addLayout(row)
class SVGButton(QPushButton):
def __init__(self, svg_filename):
super().__init__()
self.text_color = self.palette().color(self.foregroundRole())
self.svg_filename = ''
self.icon = QIcon()
self.set_icon(svg_filename)
def set_icon(self, svg_filename):
self.svg_filename = f"{APP_DATA_PATH}svgs/{svg_filename}.svg"
self.icon = QIcon(create_colored_svg(self.svg_filename, self.text_color))
self.setIcon(self.icon)
class SVGLabel(QLabel):
def __init__(self, svg_filename):
super().__init__()
self.text_color = self.palette().color(self.foregroundRole())
self.svg_filename = ''
self.icon = QIcon()
self.set_icon(svg_filename)
def set_icon(self, svg_filename):
self.svg_filename = f"{APP_DATA_PATH}svgs/{svg_filename}.svg"
self.setPixmap(create_colored_svg(self.svg_filename, self.text_color))
class ProgressDialog(QDialog):
def __init__(self, text):
super().__init__()
self.setWindowIcon(QIcon(APP_DATA_PATH + 'deezium.png'))
self.setWindowTitle(APP_NAME)
self.setFixedHeight(55)
layout = QVBoxLayout()
self.label = QLabel(text)
self.progress_bar = QProgressBar()
self.progress_bar.setRange(0, 0)
layout.addWidget(self.label)
layout.addWidget(self.progress_bar)
self.setLayout(layout)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle(APP_NAME)
self.setWindowIcon(QIcon(APP_DATA_PATH + 'deezium.png'))
self.setMinimumSize(800, 600)
self.deezpy_session = None
self.deezdw_session = None
self.logged_in: bool = False
self.play_currenttrack = None
self.play_looping: int = 0
self.closeLocked: bool = False
self.player = QMediaPlayer(self)
self.player.setVolume(75)
self.play_currentlist = []
self.history = []
self.play_widget = QWidget()
self.play_layout = QHBoxLayout()
self.play_widget.setLayout(self.play_layout)
self.play_nowplaying = QLabel()
self.play_seeker = QSlider(1)
self.play_seeker.setMinimumWidth(400)
self.play_pauseb = SVGButton('play')
self.play_pauseb.setFixedSize(30, 30)
self.play_forb = SVGButton('next')
self.play_preb = SVGButton('prev')
self.play_forb.setFixedSize(30, 30)
self.play_preb.setFixedSize(30, 30)
self.play_loopb = SVGButton('repeat')
self.play_loopb.setFixedSize(30, 30)
self.play_state = QLabel()
self.play_volume_indicator = SVGLabel('volume')
self.play_volume = QSlider(1)
self.play_volume.setRange(10, 100)
self.play_volume.setValue(75)
self.play_volume.setMinimumWidth(50)
self.play_volume.setMaximumWidth(150)
self.play_pauseb.clicked.connect(self.playback)
self.player.stateChanged.connect(self.update_player)
self.player.positionChanged.connect(self.update_seeker)
self.player.durationChanged.connect(self.set_seeker)
self.player.mediaStatusChanged.connect(self.update_mediastate)
self.play_seeker.sliderMoved.connect(self.set_position)
self.play_volume.sliderMoved.connect(self.set_volume)
self.play_forb.clicked.connect(self.skip_forward)
self.play_preb.clicked.connect(self.skip_backward)
self.play_loopb.clicked.connect(self.toggle_loop)
self.play_layout.addWidget(self.play_seeker)
self.play_layout.addWidget(self.play_state)
self.play_layout.addStretch()
self.play_layout.addWidget(self.play_nowplaying)
self.play_layout.addWidget(self.play_preb)
self.play_layout.addWidget(self.play_pauseb)
self.play_layout.addWidget(self.play_forb)
self.play_layout.addStretch()
self.play_layout.addWidget(self.play_volume_indicator)
self.play_layout.addWidget(self.play_volume)
self.play_layout.addWidget(self.play_loopb)
print('[D> Main loading')
self.update_config()
if bool(self.config_arl) and bool(self.config_aro):
self.login()
else:
self.createLoginPage()
def read_playstate(self):
if os.path.exists(os.path.expanduser('~/.config/deezium/lastplay')):
try:
with open(os.path.expanduser('~/.config/deezium/lastplay'), 'r') as f:
ldata = f.read()
data = json.loads(ldata)
self.play_currentlist = data['current_play']
self.streamtrackid(data['current_id'], start=False)
self.play_looping = data['looping']
self.update_loopbutton()
self.player.setPosition(data['position'])
self.player.setVolume(data['volume'])
self.play_volume.setValue(data['volume'])
self.play_seeker.setValue(data['position'])
except KeyError:
print('[E> Invalid State removed')
os.remove(os.path.expanduser('~/.config/deezium/lastplay'))
def update_mediastate(self, mstate):
if mstate == QMediaPlayer.EndOfMedia:
if self.play_looping == 2:
self.player.setPosition(0)
self.player.play()
elif self.play_currenttrack == self.play_currentlist[-1]:
if self.play_looping == 1:
self.skip_forward()
else:
self.streamtrackid(self.play_currentlist[0], start=False)
else:
self.skip_forward()
def toggle_loop(self):
self.play_looping = (self.play_looping + 1) % 3
self.update_loopbutton()
def update_loopbutton(self):
if self.play_looping == 0:
self.play_loopb.set_icon('repeat')
elif self.play_looping == 1:
self.play_loopb.set_icon('repeat_on')
elif self.play_looping == 2:
self.play_loopb.set_icon('repeat_one')
def skip_forward(self, by=1):
if not by:
by = 1
currenti = self.play_currentlist.index(int(self.play_currenttrack))
self.streamtrackid(self.play_currentlist[(currenti + by) % len(self.play_currentlist)])
def skip_backward(self, by=1):
if not by:
by = 1
currenti = self.play_currentlist.index(int(self.play_currenttrack))
self.streamtrackid(self.play_currentlist[currenti - by])
def streamtrackid(self, tid, start=True, historize=True):
self.play_currenttrack = tid
path = api.download_track(self.deezdw_session, tid)
self.player.setMedia(QMediaContent(QUrl.fromLocalFile(path)))
if start:
self.player.play()
self.update_player(self.player.state())
bg = api.calc_background_color(api.download_albumcover_m(self.deezpy_session, self.deezpy_session.get_track(tid).album.id))
fg = api.calc_foreground_color(bg)
self.play_widget.setStyleSheet(f'background-color: {bg}; color: {fg}')
if historize:
albumid = self.deezpy_session.get_track(tid).album.id
self.insert_history(albumid)
# self.update_metadata()
def insert_history(self, tid):
if tid in self.history:
self.history.remove(tid)
self.history.insert(0, tid)
self.history = self.history[:10]
self.save_history()
def playtrack(self, tid):
self.play_currentlist = [tid]
self.streamtrackid(tid, start=True)
self.play_looping = 0
self.update_loopbutton()
def playalbums(self, ids, first=None):
self.play_currentlist = ids
if not first:
self.streamtrackid(ids[0], start=True)
else:
self.streamtrackid(first, start=True)
self.play_looping = 0
self.update_loopbutton()
def playlist(self, ids, first=None):
self.play_currentlist = ids
if not first:
self.streamtrackid(ids[0], start=True)
else:
self.streamtrackid(first, start=True)
self.play_looping = 1
self.update_loopbutton()
def toggle_favorite_track(self, tid):
def toggle():
ids = api.conv_paginated_ids(self.deezpy_session.get_user_tracks())
if tid in ids:
self.deezpy_session.remove_user_track(tid)
else:
self.deezpy_session.add_user_track(tid)
# deepcode ignore MissingAPI: it is required to keep the application running, while the list is converted,
# as this takes a little moment
thread = threading.Thread(target=toggle)
thread.start()
def update_player(self, state):
if state == QMediaPlayer.PlayingState:
self.play_pauseb.set_icon('pause')
else:
self.play_pauseb.set_icon('play')
index = self.play_currentlist.index(self.play_currenttrack)
self.play_preb.setDisabled(index == 0)
self.play_forb.setDisabled((index == len(self.play_currentlist)) or (self.play_looping == 2))
if self.play_currenttrack:
track = self.deezpy_session.get_track(self.play_currenttrack)
self.play_nowplaying.setText(f'<b>{track.title}</b> - {track.artist.name}')
def set_seeker(self, lenght):
def buffer():
api.download_track(self.deezdw_session, nxtid)
self.play_seeker.setRange(0, lenght)
index = self.play_currentlist.index(self.play_currenttrack) + 1
if self.play_looping != 2 and index != len(self.play_currentlist):
nxtid = self.play_currentlist[index]
# file deepcode ignore MissingAPI: it is required to keep the application running, while the next song is buffering
buffer_thread = threading.Thread(target=buffer)
buffer_thread.start()
def set_volume(self, vol):
self.player.setVolume(vol)
def update_seeker(self, duration):
"""Updates the Timestamp next to the seeker to the correct time."""
self.play_seeker.setValue(duration)
self.play_state.setText(api.ms_to_str(duration) + '/' + api.ms_to_str(self.player.duration()))
def playback(self):
if self.player.state() == QMediaPlayer.PlayingState:
self.player.pause()
else:
self.player.play()
calbum = self.deezpy_session.get_track(self.play_currenttrack).album.id
self.insert_history(calbum)
def set_position(self, position):
self.player.setPosition(position)
def login(self):
"""Prepares the player to load the main page and loads old playing state"""
print('[D> Initializing Sessions')
self.init_sessions()
print('[D> Creating Main Page')
try:
self.createMainPage()
except api.deezer.exceptions.DeezerErrorResponse:
os.remove(os.path.expanduser('~/.config/deezium/aro.dat'))
self.update_config()
self.createLoginFailedPage()
self.logged_in = True
print('[D> Restoring state')
self.read_playstate()
def init_sessions(self):
"""Initializes the two online api sessions and if necessary kick back to log in"""
os.makedirs(os.path.expanduser('~/.cache/deezium/'), exist_ok=True)
os.chdir(os.path.expanduser('~/.cache/deezium/'))
logout = False
if not self.deezdw_session:
try:
self.deezdw_session = api.deezloader2.Login2(self.config_arl)
except ValueError:
os.remove(os.path.expanduser('~/.config/deezium/arl.dat'))
logout = True
except deezloader.exceptions.BadCredentials:
os.remove(os.path.expanduser('~/.config/deezium/arl.dat'))
logout = True
if not self.deezpy_session:
try:
self.deezpy_session = api.deezer.Client(access_token=self.config_aro)
except api.deezer.exceptions.DeezerErrorResponse:
os.remove(os.path.expanduser('~/.config/deezium/aro.dat'))
logout = True
if logout:
self.update_config()
self.createLoginFailedPage()
def update_config(self):
"""READs the config and updates the two token variables accordingly"""
self.config_aro = api.get_oauth_token()
self.config_arl = api.get_login_token()
if os.path.exists(os.path.expanduser('~/.config/deezium/history')):
with open(os.path.expanduser('~/.config/deezium/history'), 'r') as f:
self.history = json.loads(f.read())
print('[D> Config read')
@staticmethod
def logout() -> None:
"""Logs out and clears the stored authentication data"""
api.logout()
subprocess.Popen(os.path.abspath(inspect.getsourcefile(lambda: 0)))
sys.exit()
def run_oauth(self) -> None:
"""Runs the web oauth server for deezer login"""
api.gen_oauth_token(APP_DATA_PATH)
self.update_config()
def createLoginFailedPage(self, errcode: str = ''):
"""Creates the error page for the login"""
main_widget = QWidget()
main_layout = QHBoxLayout(main_widget)
v_layout = QVBoxLayout()
main_layout.addStretch()
main_layout.addLayout(v_layout)
main_layout.addStretch()
title = QLabel("Something wen't wrong")
title.setFont(QFont(title.font().family(), 15))
subtitle = QLabel()
if errcode:
subtitle.setText('Error Code: ' + errcode)
else:
subtitle.setText('An unknown Error occured.')
backbutton = QPushButton('Back')
backbutton.clicked.connect(self.createLoginPage)
v_layout.addStretch()
v_layout.addWidget(title)
v_layout.addWidget(subtitle)
v_layout.addWidget(backbutton)
v_layout.addStretch()
self.setCentralWidget(main_widget)
def createWebLoginPage(self):
"""Creates the web-based login page"""
def process_return(html):
"""Runs when the html was fully loaded. Processes the html to find the success or error message."""
if html is None:
self.closeLocked = False
return
match = re.match('Error: ([a-zA-Z]*). You may close this tab now', html)
if html == 'Valid. You may close this tab now':
self.closeLocked = False
self.update_config()
if not bool(self.config_arl):
dialog = NoArlDialog()
if dialog.exec_() == QDialog.Accepted:
self.update_config()
else:
self.close()
self.login()
elif html == 'Something went wrong. You may close this tab now':
self.closeLocked = False
self.createLoginFailedPage()
elif match:
self.closeLocked = False
self.createLoginFailedPage(match.groups()[0])
def process_added_cookie(cookie):
"""Processes a cookie of the cookie store to find a deezer arl cookie to store and use for login later"""
cookie = QNetworkCookie(cookie)
name = bytearray(cookie.name()).decode()
domain = cookie.domain()
value = bytearray(cookie.value()).decode()
if name == 'arl' and domain == '.deezer.com' and len(value) == 192:
print('[D> Arl found and imported.')
with open(os.path.expanduser('~/.config/deezium/arl.dat'), 'w') as f:
f.write(value)
def load_finished():
"""Runs when the html was fully loaded and calls the parser with parts out of the html"""
login_webengine.page().runJavaScript("document.getElementsByTagName('pre')[0].innerHTML;", process_return)
def abort():
"""Aborts the login process by sending one request to the oauth server to shut it down"""
try:
api.requests.get('http://localhost:3875')
except api.requests.exceptions.ConnectionError:
pass
self.createLoginPage()
main_widget = QWidget()
main_layout = QVBoxLayout(main_widget)
abort_button = QPushButton('Abort login')
abort_button.clicked.connect(abort)
login_webengine = QWebEngineView()
login_webengine.load(QUrl(
'https://connect.deezer.com/oauth/auth.php?app_id=663691&redirect_uri=http://localhost:3875/&perms=basic_access,email,offline_access,manage_library,manage_community,delete_library,listening_history'))
login_webengine.loadFinished.connect(load_finished)
login_webengine.page().profile().cookieStore().cookieAdded.connect(process_added_cookie)
main_layout.addWidget(abort_button)
main_layout.addWidget(login_webengine)
self.setCentralWidget(main_widget)
def createLoginPage(self):
"""Creates the login page"""
def login():
"""Prepares and runs the background job for the oauth server"""
self.closeLocked = True
# file deepcode ignore MissingAPI: this is required, because the application has to run the login server in the background, to not freeze the gui.
login_thread = threading.Thread(target=self.run_oauth)
login_thread.start()
self.createWebLoginPage()
login_widget = QWidget()
login_mlayout = QHBoxLayout()
login_layout = QVBoxLayout()
login2_layout = QVBoxLayout()
login_widget.setLayout(login_mlayout)
login_mlayout.addStretch()
login_mlayout.addLayout(login_layout)
login_mlayout.addLayout(login2_layout)
login_mlayout.addStretch()
group_box = QGroupBox('Login')
group_layout = QVBoxLayout()
group_box.setLayout(group_layout)
login_layout.addStretch()
login2_layout.addStretch()
title_layout = QHBoxLayout()
title_layout.addStretch()
logo_layout = QHBoxLayout()
logo = QLabel()
logop = QPixmap(APP_DATA_PATH + 'deezium256.png')
logo.setPixmap(logop)
login_title = QLabel(APP_NAME)
login_title.setFont(QFont(login_title.font().family(), 20))
title_layout.addWidget(login_title)
title_layout.addStretch()
logo_layout.addStretch()
logo_layout.addWidget(logo)
logo_layout.addStretch()
group_layout.addLayout(logo_layout)
group_layout.addLayout(title_layout)
login_button = QPushButton('Login with Deezer')
login_button.setIcon(QIcon(APP_DATA_PATH + 'deezer_logo.png'))
group_layout.addWidget(login_button)
disclaimer = QGroupBox('Disclaimer')
dis_layout = QVBoxLayout()
dis_layout.addWidget(QLabel('<b>NOT AFFILIATED WITH DEEZER</b>'))
dis_layout.addWidget(QLabel('The Author of this program is'))
dis_layout.addWidget(QLabel('not responsible for the usage'))
dis_layout.addWidget(QLabel('of this program by other people.'))
dis_layout.addWidget(QLabel())
dis_layout.addWidget(QLabel("The Author does not recommend"))
dis_layout.addWidget(QLabel("you doing this illegally or"))
dis_layout.addWidget(QLabel("against Deezer's terms of service."))
dis_layout.addWidget(QLabel())
dis_layout.addWidget(QLabel("Seriously please buy an subscription"))
dis_layout.addWidget(QLabel("when you use this program. The artists also"))
dis_layout.addWidget(QLabel("want money for their work. Thank you."))
disclaimer.setLayout(dis_layout)
login_layout.addWidget(group_box)
login2_layout.addWidget(disclaimer)
login_layout.addStretch()
login2_layout.addStretch()
login_button.clicked.connect(login)
self.setCentralWidget(login_widget)
def createMainPage(self):
"""Creates the main page or "home" page or "favorites" page"""
def search():
query = searchbar.text()
self.createSearchresultPage(query)
def album_clicked(aid):
self.createAlbumoverviewPage(aid)
def show_ftracks():
self.createPlaylistPage(f_tracks)
main_widget = QWidget()
main_layout = QVBoxLayout()
title_layout = QHBoxLayout()
title_text = QLabel('Your Favorites')
title_text.setFont(QFont(title_text.font().family(), 16))
title_layout.addWidget(title_text)
title_layout.addStretch()
searchbar = QLineEdit()
searchbar.setPlaceholderText('Search for music and audiobooks')
searchbutton = QPushButton('Search')
settings_button = SVGButton('settings')
settings_button.setFixedSize(30, 30)
settings_button.clicked.connect(self.createSettingsPage)
title_layout.addWidget(searchbar)
title_layout.addWidget(searchbutton)
title_layout.addWidget(settings_button)
searchbar.returnPressed.connect(search)
searchbutton.clicked.connect(search)
main_layout.addLayout(title_layout)
scroll_area = QScrollArea()
scroll_area.setWidgetResizable(True)
scroll_area.setFrameShape(QFrame.NoFrame)
scroll_widget = QWidget()
scroll_layout = QVBoxLayout()
scroll_widget.setLayout(scroll_layout)
scroll_area.setWidget(scroll_widget)
history_label = QLabel('History')
history_label.setFont(QFont(history_label.font().family(), 14))
if self.history:
history_widget = QScrollArea()
history_widget.setWidgetResizable(True)
history_swidget = QAlbumHList(self.history, deezerclient=self.deezpy_session)
history_swidget.album_clicked.connect(album_clicked)
history_widget.setWidget(history_swidget)
else:
history_widget = QLabel("There's no history here. So you have to write history now, then.")
f_title = QLabel('Favorites')
f_title.setFont(QFont(history_label.font().family(), 14))
f_albums = self.deezpy_session.get_user_albums()
f_album_p = QPixmap()
if f_albums:
f_album_p.loadFromData(api.download_albumcover_s(self.deezpy_session, f_albums[0].id))
f_album_w = QHClickList('Albums', f_album_p, f'{f_albums.total} albums total',
f'{f_albums[0].title} - {f_albums[0].artist.name}')
f_tracks = self.deezpy_session.get_user_tracks()
f_track_p = QPixmap()
r_track = random.choice(f_tracks)
if f_tracks:
try:
f_track_p.loadFromData(api.download_albumcover_s(self.deezpy_session, r_track.album.id))
except api.deezer.exceptions.DeezerErrorResponse:
print('[W> Loading of f_track_p failed')
f_track_w = QHClickList('Tracks', f_track_p, f'{f_tracks.total} tracks total',
f'{r_track.title} - {r_track.artist.name}')
f_track_w.action.connect(show_ftracks)
scroll_layout.addWidget(history_label)
scroll_layout.addWidget(history_widget)
scroll_layout.addWidget(f_title)
scroll_layout.addWidget(f_album_w)
scroll_layout.addWidget(f_track_w)
scroll_layout.addStretch()
main_layout.addWidget(scroll_area)
main_layout.addWidget(self.play_widget)
main_widget.setLayout(main_layout)
self.setCentralWidget(main_widget)
def createSearchresultPage(self, query):
"""Searches deezer and creates the search results page"""
def search():
query = searchbar.text()
self.createSearchresultPage(query)
def album_clicked(aid):
self.createAlbumoverviewPage(aid)