-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyRadio.py
1387 lines (1219 loc) · 54.7 KB
/
myRadio.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
# -*- coding: utf-8 -*-
import os
import sys
import requests
import editor_intern
from PyQt5.QtCore import (QUrl, pyqtSignal, Qt, QMimeData, QSize, QPoint, QProcess,
QStandardPaths, QFile, QDir, QSettings, QByteArray, QEvent)
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QSlider,
QMainWindow, QFileDialog, QMenu, qApp, QAction, QLineEdit,
QVBoxLayout, QHBoxLayout, QComboBox, QLabel, QSpacerItem, QSizePolicy,
QMessageBox, QPlainTextEdit, QSystemTrayIcon, QInputDialog, QToolButton, )
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
from PyQt5.QtGui import QIcon, QPixmap, QTextOption, QTextCursor
changed = pyqtSignal(QMimeData)
btnwidth = 100
class Editor(QWidget):
def __init__(self):
super(Editor, self).__init__()
self.setAttribute(Qt.WA_QuitOnClose, False)
self.radiofile = ""
self.radio_editor = QPlainTextEdit()
self.radio_editor.textChanged.connect(self.setModified)
self.layout = QVBoxLayout()
self.layout.addWidget(self.radio_editor)
self.setLayout(self.layout)
self.setGeometry(0, 0, 650, 500)
self.setStyleSheet("QPlainTextEdit {background-color: #eeeeec;}")
def closeEvent(self, event):
if self.isModified:
quit_msg = "<b>File has been changed. <br> Save it?</ b>\
<br><br><span style='color: #a40000;'>new stations are available the next time myRadio is started</span>"
reply = QMessageBox.question(None, 'Speichern',
quit_msg, QMessageBox.Yes, QMessageBox.No)
if reply == QMessageBox.Yes:
if self.saveFile():
print("File saved\nclosing editor")
event.accept()
else:
print("Editor closed")
def saveFile(self):
t = os.linesep.join([s for s in self.radio_editor.toPlainText().splitlines() if s])
t += '\n'
with open(self.radiofile, 'w', encoding='utf8') as f:
f.write(t)
f.close()
return True
def setModified(self):
self.isModified = True
class MainWin(QMainWindow):
def __init__(self):
super(MainWin, self).__init__()
imgstring = b"iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAFG3pUWHRSYXcgcHJv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up1wAAAABJRU5ErkJggg=="
pixmap = QPixmap()
pixmap.loadFromData(QByteArray.fromBase64(imgstring))
self.er_icon = QIcon(pixmap)
self.settings = QSettings("myRadio", "settings")
self.setStyleSheet(mystylesheet(self))
dir = os.path.dirname(sys.argv[0])
os.chdir(dir)
self.radiofile = os.path.join(dir, "myradio.txt")
self.radioNames = []
self.radiolist = []
self.exclchannels = []
self.radioStations = ""
self.exclradioStations = ""
self.rec_name = ""
self.rec_url = ""
self.old_meta = ""
self.current_station_name = ""
self.current_station_url = ""
self.notificationsEnabled = True
self.wg = QWidget()
self.fr = RadioFinder()
self.flabel = QLabel("Radio-Finder")
self.layout = QVBoxLayout()
self.layout.setContentsMargins(10 ,6, 10, 6)
self.layout1 = QHBoxLayout()
self.outfile = QStandardPaths.standardLocations(QStandardPaths.TempLocation)[0] + "/radio.mp3"
self.recording_enabled = False
self.is_recording = False
self.play_btn = QPushButton("Play", self)
self.play_btn.setFixedWidth(btnwidth)
self.play_btn.setFlat(True)
self.play_btn.setIcon(QIcon.fromTheme("media-playback-start"))
self.layout1.addWidget(self.play_btn)
self.stop_btn = QPushButton("Stop", self)
self.stop_btn.setFixedWidth(btnwidth)
self.stop_btn.setFlat(True)
self.stop_btn.setIcon(QIcon.fromTheme("media-playback-stop"))
self.layout1.addWidget(self.stop_btn)
### record
self.rec_btn = QPushButton("Record", self)
self.rec_btn.setFixedWidth(btnwidth)
self.rec_btn.setFlat(True)
self.rec_btn.setIcon(QIcon.fromTheme("media-record"))
self.rec_btn.clicked.connect(self.recordRadio1)
self.rec_btn.setToolTip("start recording")
self.layout1.addWidget(self.rec_btn)
### stop record
self.stoprec_btn = QPushButton("Stop Rec", self)
self.stoprec_btn.setFixedWidth(btnwidth + 60)
self.stoprec_btn.setFlat(True)
self.stoprec_btn.setIcon(QIcon.fromTheme("media-playback-stop"))
self.stoprec_btn.clicked.connect(self.stop_recording)
self.stoprec_btn.setToolTip("stop recording")
self.layout1.addWidget(self.stoprec_btn)
### edit Radiio List Text
self.edit_btn = QPushButton("", self)
self.edit_btn.setFixedWidth(26)
self.edit_btn.setFlat(True)
self.edit_btn.setToolTip("channel editor")
self.edit_btn.setIcon(QIcon.fromTheme("preferences-system"))
self.edit_btn.clicked.connect(self.edit_Channels_Table)
self.layout1.addWidget(self.edit_btn)
### hide Main Window
self.hide_btn = QPushButton("", self)
self.hide_btn.setFixedWidth(26)
self.hide_btn.setFlat(True)
self.hide_btn.setToolTip("hide main window")
self.hide_btn.setIcon(QIcon.fromTheme("window-hide"))
self.hide_btn.clicked.connect(self.showMain)
self.layout1.addWidget(self.hide_btn)
spc1 = QSpacerItem(6, 10, QSizePolicy.Expanding, QSizePolicy.Maximum)
self.level_sld = QSlider(self)
self.level_sld.setTickPosition(1)
self.level_sld.setOrientation(Qt.Horizontal)
self.level_sld.setValue(65)
self.level_lbl = QLabel(self)
self.level_lbl.setAlignment(Qt.AlignHCenter)
self.level_lbl.setText("Volume 65")
self.layout.addLayout(self.layout1)
self.layout.addItem(spc1)
self.layout.addWidget(self.level_sld)
self.layout.addWidget(self.level_lbl)
self.player = RadioPlayer(self)
self.player.metaDataChanged.connect(self.metaDataChanged)
self.player.error.connect(self.handleError)
self.play_btn.clicked.connect(self.togglePlay)
self.stop_btn.clicked.connect(self.stop_preview)
self.level_sld.valueChanged.connect(self.set_sound_level)
self.process = QProcess()
self.process.started.connect(self.getPID)
self.wg.setLayout(self.layout)
self.setCentralWidget(self.wg)
self.stoprec_btn.setVisible(False)
self.readStations()
self.createStatusBar()
self.setAcceptDrops(True)
self.setWindowTitle("myRadio")
self.tIcon = QIcon(os.path.join(os.path.dirname(sys.argv[0]), "radio_bg.png"))
self.setWindowIcon(self.tIcon)
self.stationActs = []
self.setMinimumHeight(180)
self.setFixedWidth(600)
self.move(0, 30)
# Init tray icon
trayIcon = QIcon(self.tIcon)
self.trayIcon = QSystemTrayIcon()
self.trayIcon.setIcon(trayIcon)
self.trayIcon.installEventFilter(self)
self.trayIcon.activated.connect(self.setTrayTrigger)
self.trayIcon.show()
self.metaLabel = QLabel()
self.geo = self.geometry()
self.findRadioAction = QAction(QIcon.fromTheme("edit-find"), "Find radio stations",
triggered = self.findRadio)
self.editAction = QAction(QIcon.fromTheme("preferences-system"), "Edit channel list (text)",
triggered = self.edit_Channels_Text)
self.editActionTable = QAction(QIcon.fromTheme("preferences-system"), "Edit channel list (table)",
triggered = self.edit_Channels_Table)
self.showWinAction = QAction(QIcon.fromTheme("view-restore"), "Show main window", triggered = self.showMain)
self.notifAction = QAction(QIcon.fromTheme("dialog-information"), "Switch off tray messages", triggered = self.toggleNotif)
self.togglePlayerAction = QAction("Stop playback", triggered = self.togglePlay)
self.togglePlayerAction.setIcon(QIcon.fromTheme("media-playback-stop"))
self.recordAction = QAction(QIcon.fromTheme("media-record"), "Record station", triggered = self.recordRadio1)
self.stopRecordAction = QAction(QIcon.fromTheme("media-playback-stop"), "Stop recording",
triggered = self.stop_recording)
self.findExecutable()
self.readSettings()
self.makeTrayMenu()
self.createWindowMenu()
if QSystemTrayIcon.isSystemTrayAvailable():
print("System Tray Icon is available")
else:
print("System tray icon is not available")
if self.player.state() == QMediaPlayer.StoppedState:
self.togglePlayerAction.setText("Start playback")
self.togglePlayerAction.setIcon(QIcon.fromTheme("media-playback-start"))
elif self.player.state() == QMediaPlayer.PlayingState:
self.togglePlayerAction.setText("Stop playback")
self.togglePlayerAction.setIcon(QIcon.fromTheme("media-playback-stop"))
self.flabel.setFixedSize(180, 26)
self.flabel.setStyleSheet("QLabel {font-size: 11px; font-weight: bold; \
color: #555753} QLabel::hover {color: #eeeeec; background-color: #1c87c9; border: 1px solid #babdb6;}")
self.flabel.mousePressEvent = self.labelClicked
self.flabel.setAlignment(Qt.AlignCenter)
self.layout.addWidget(self.flabel, 0, Qt.AlignCenter)
self.findRadio()
def eventFilter(self, source, event):
if source is self.trayIcon:
if event.type() == QEvent.Wheel:
vol = self.level_sld.value()
if event.angleDelta().y() > 1:
self.level_sld.setValue(int(vol) + 5)
else:
self.level_sld.setValue(int(vol) - 5)
self.showTrayMessage("myRadio", f"Volume {self.level_sld.value()}", self.tIcon, 1000)
return super(MainWin, self).eventFilter(source, event)
def setTrayTrigger(self, reason):
if reason == QSystemTrayIcon.DoubleClick:
self.showMainfromTray()
elif reason == QSystemTrayIcon.MiddleClick:
self.muteFromTray()
def muteFromTray(self):
if self.player.isMuted():
self.player.setMuted(False)
print("Player not muted")
else:
self.player.setMuted(True)
print("Player muted")
def showTrayMessage(self, title, message, icon, timeout = 4000):
self.trayIcon.showMessage(title, message, icon, timeout)
def findRadio(self):
self.fr.setStyleSheet(mystylesheet(self))
self.fr.startButton.clicked.connect(lambda: self.player.stop())
self.fr.stopButton.clicked.connect(lambda: self.playRadioStation())
self.fr.findfield.setFocus()
self.layout.addWidget(self.fr)
self.fg = QVBoxLayout()
self.fg.addWidget(self.fr)
self.layout.addLayout(self.fg)
self.setFixedHeight(600)
vol = self.level_sld.value()
self.fr.player.setVolume(vol)
self.flabel.setText("Show radio search")
self.fr.hide()
self.setFixedHeight(260)
def labelClicked(self, event):
if self.fr.isVisible():
self.fr.hide()
self.setFixedHeight(260)
self.flabel.setText("Show radio search")
else:
self.setFixedHeight(600)
self.fr.show()
self.flabel.setText("Hide radio search")
def handleError(self):
print("Fehler: " + self.player.errorString())
self.showTrayMessage("Error", self.player.errorString(), self.tIcon, 3000)
self.msglbl.setText(f"Fehler:\n{self.player.errorString()}")
def togglePlay(self):
if self.togglePlayerAction.text() == "Stop playback":
self.stop_preview()
self.togglePlayerAction.setText("Start playback")
self.togglePlayerAction.setIcon(QIcon.fromTheme("media-playback-start"))
else:
self.playRadioStation()
self.togglePlayerAction.setText("Stop playback")
self.togglePlayerAction.setIcon(QIcon.fromTheme("media-playback-stop"))
def getURLfromPLS(self, inURL):
print("prüfe", inURL)
response = requests.get(inURL)
html = response.text.replace("https", "http").splitlines()
playlist = []
for line in html:
if line.startswith("File") == True:
list = line.split("=", 1)
playlist.append(list[1])
print("URL:", playlist[0])
return(playlist[0])
def getURLfromM3U(self, inURL):
print("prüfe", inURL)
response = requests.get(inURL)
html = response.text.replace("https", "http").splitlines()
playlist = []
for line in html:
if not line.startswith("#") and len(line) > 0 and line.startswith("http"):
playlist.append(line)
print("URL:", playlist[0])
return(playlist[0])
def createWindowMenu(self):
self.tb = self.addToolBar("Menu")
self.tb_menu = QMenu()
self.tb.setIconSize(QSize(20, 20))
####################################
toolButton = QToolButton()
toolButton.setIcon(self.tIcon)
toolButton.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
toolButton.setText(" Menü")
toolButton.setMenu(self.tray_menu)
toolButton.setPopupMode(QToolButton.InstantPopup)
self.tb.addWidget(toolButton)
self.tb.setContextMenuPolicy(Qt.PreventContextMenu)
self.tb.setMovable(False)
self.tb.setAllowedAreas(Qt.TopToolBarArea)
def makeTrayMenu(self):
self.tray_menu = QMenu()
self.tray_menu.addAction(self.togglePlayerAction)
self.tray_menu.setStyleSheet("font-size: 7pt;")
self.exclradio_menu = self.tray_menu.addMenu("Exclusive Radio")
self.exclradio_menu.setIcon(self.er_icon)
i = 0
##### submenus from categories ##########
t = os.linesep.join([s for s in self.radioStations.splitlines() if s])
t += '\n'
b = t.splitlines()
for x in range(0, len(b)):
line = b[x]
while True:
if line.startswith("--"):
chm = self.tray_menu.addMenu(line.replace("-- ", "").replace(" --", ""))
chm.setIcon(self.tIcon)
break
continue
elif not line.startswith("--"):
ch = line.partition(",")[0]
self.stationActs.append(QAction(QIcon.fromTheme("browser"), ch, triggered = self.openTrayStation))
self.stationActs[i].setData(str(i))
chm.addAction(self.stationActs[i])
i += 1
break
##### exclusive radio submenus from categories ##########
t = os.linesep.join([s for s in self.exclradioStations.splitlines() if s])
t += '\n'
excl_list = t.splitlines()
for x in range(len(excl_list)):
line = excl_list[x]
while True:
if line.startswith("--"):
exm = self.exclradio_menu.addMenu(line.replace("-- ", "").replace(" --", ""))
exm.setIcon(self.er_icon)
break
continue
elif not line.startswith("--"):
ch = line.partition(",")[0]
self.stationActs.append(QAction(QIcon(self.er_icon), ch, triggered = self.openTrayStation))
self.stationActs[i].setData(str(i))
exm.addAction(self.stationActs[i])
i += 1
break
####################################
self.tray_menu.addSeparator()
if not self.is_recording:
if not self.current_station_name.startswith("--"):
self.tray_menu.addAction(self.recordAction)
self.recordAction.setText(f"start recording of {self.current_station_name}")
if self.is_recording:
self.tray_menu.addAction(self.stopRecordAction)
self.tray_menu.addSeparator()
self.tray_menu.addAction(self.editAction)
self.tray_menu.addAction(self.editActionTable)
self.tray_menu.addAction(self.showWinAction)
self.tray_menu.addSeparator()
self.tray_menu.addAction(self.notifAction)
self.tray_menu.addSeparator()
exitAction = self.tray_menu.addAction(QIcon.fromTheme("application-exit"), "Exit")
exitAction.triggered.connect(self.exitApp)
self.trayIcon.setContextMenu(self.tray_menu)
def showMain(self):
if self.isVisible() ==False:
self.showWinAction.setText("Hide main window")
self.setVisible(True)
print("Main window is displayed")
elif self.isVisible() ==True:
self.showWinAction.setText("Show main window")
self.setVisible(False)
print("Main window is not displayed")
def showMainfromTray(self):
buttons = qApp.mouseButtons()
if buttons == Qt.LeftButton:
self.showMain()
def toggleNotif(self):
if self.notifAction.text() == "Switch off tray messages":
self.notifAction.setText("Activate tray messages")
self.notificationsEnabled = False
elif self.notifAction.text() == "Activate tray messages":
self.notifAction.setText("Switch off tray messages")
self.notificationsEnabled = True
print("Notifications", self.notificationsEnabled )
self.metaDataChanged()
def openTrayStation(self):
action = self.sender()
if action:
ind = action.data()
name = action.text()
ch_number = int(ind)
self.current_station_name = name
self.current_station_url = self.radiolist[ch_number]
print("switch to station:", ind, name, self.current_station_url)
self.player.set_media(self.radiolist[ch_number])
self.player.play()
self.set_running_player()
self.setWindowTitle(name)
def exitApp(self):
self.close()
QApplication.quit()
def message(self, message):
QMessageBox.information(
None, 'Message', message)
def closeEvent(self, e):
self.writeSettings()
print("write configuration file ...\nGoodbye ...")
QApplication.quit()
def readSettings(self):
print("read configuration file ...")
if self.settings.contains("pos"):
pos = self.settings.value("pos", QPoint(200, 200))
self.move(pos)
else:
self.move(0, 26)
if self.settings.contains("lastChannel"):
self.current_station_name = self.settings.value("lastChannel")
if self.settings.contains("lastURL"):
self.current_station_url = self.settings.value("lastURL")
if self.settings.contains("notifications"):
self.notificationsEnabled = self.settings.value("notifications")
if self.settings.value("notifications") == "false":
self.notificationsEnabled = False
self.notifAction.setText("Activate tray messages")
else:
self.notifAction.setText("Switch off tray messages")
self.notificationsEnabled = True
if self.settings.contains("windowstate"):
print(self.settings.value("windowstate"))
if self.settings.value("windowstate") == "Show main window":
self.show()
self.showWinAction.setText("Hide main window")
else:
self.hide()
self.showWinAction.setText("Show main window")
if self.settings.contains("volume"):
vol = self.settings.value("volume")
print("set volume to", vol)
self.level_sld.setValue(int(vol))
if self.settings.contains("muted"):
if self.settings.value("muted") == "false":
self.player.setMuted(False)
print("Player not muted")
else:
self.player.setMuted(True)
print("Player muted")
if self.settings.contains("playerstate"):
if self.settings.value("playerstate") == "0":
self.player.setMedia(QMediaContent(QUrl(self.current_station_url)))
self.player.stop()
self.togglePlayerAction.setText("Start playback")
else:
self.player.setMedia(QMediaContent(QUrl(self.current_station_url)))
self.player.play()
self.setWindowTitle(self.current_station_name)
self.set_running_player()
self.togglePlayerAction.setText("Stop playback")
def writeSettings(self):
self.settings.setValue("pos", self.pos())
self.settings.setValue("lastURL", self.current_station_url)
self.settings.setValue("lastChannel", self.current_station_name)
self.settings.setValue("notifications", self.notificationsEnabled)
if self.isVisible():
self.settings.setValue("windowstate", "Show main window")
else:
self.settings.setValue("windowstate", "Hide main window")
self.settings.setValue("volume", self.level_sld.value())
self.settings.setValue("muted", self.player.isMuted())
self.settings.setValue("playerstate", self.player.state())
self.settings.sync()
def readStations(self):
self.radiolist = []
dir = os.path.dirname(sys.argv[0])
self.radiofile = os.path.join(dir, "myradio.txt")
with open(self.radiofile, 'r') as f:
self.radioStations = f.read()
f.close()
t = os.linesep.join([s for s in self.radioStations.splitlines() if s])
for lines in t.splitlines():
if not lines.startswith("--"):
self.radiolist.append(lines.partition(",")[2])
# Exclusive Radio
self.exclradiofile = os.path.join(dir, "exclradio.txt")
with open(self.exclradiofile, 'r') as exclfile:
self.exclradioStations = exclfile.read()
exclfile.close()
t = os.linesep.join([s for s in self.exclradioStations.splitlines() if s])
for lines in t.splitlines():
if not lines.startswith("--"):
self.radiolist.append(lines.partition(",")[2])
def edit_Channels_Table(self):
print("edit_Channels_Table")
dir = os.path.dirname(sys.argv[0])
self.radiofile = os.path.join(dir, "myradio.txt")
if QFile.exists(self.radiofile):
self.editor = editor_intern.Viewer()
self.editor.open_channels(self.radiofile)
self.editor.show()
else:
self.msgbox('myradio.txt not found')
def edit_Channels_Text(self):
dir = os.path.dirname(sys.argv[0])
self.radiofile = os.path.join(dir, "myradio.txt")
self.showTrayMessage("Message", "Changes are available after restarting myRadio", self.tIcon)
self.edWin = Editor()
self.edWin.setWindowTitle("Channel Editor")
self.edWin.radiofile = self.radiofile
t = open(self.radiofile, 'r').read()
self.edWin.radio_editor.setPlainText(t)
self.edWin.isModified = False
self.edWin.show()
def keyPressEvent(self, e):
if e.key() == Qt.Key_F5:
self.playURL()
elif e.key() == Qt.Key_F1:
QMessageBox.information(self, "Information", "F5 -> Play url from the clipboard").exec_()
else:
e.accept()
def findExecutable(self):
wget = QStandardPaths.findExecutable("wget")
if wget != "":
print("%s %s %s" % ("wget found in ", wget, " *** Recordings possible"))
self.msglbl.setText("Recordings possible")
self.showTrayMessage("Note", "found wget\nRecordings possible", self.tIcon)
self.recording_enabled = True
else:
self.showTrayMessage("Note", "wget not found\nno recordings possible", self.tIcon)
print("wget not found\nno recordings possible")
self.recording_enabled = False
def remove_last_line_from_string(self, s):
return s[:s.rfind('\n')]
def createStatusBar(self):
self.msglbl = QLabel()
#self.msglbl.setFixedHeight(60)
self.msglbl.setTextInteractionFlags(Qt.TextSelectableByMouse)
self.msglbl.setWordWrap(True)
self.layout.addWidget(self.msglbl)
self.msglbl.setAlignment(Qt.AlignHCenter)
self.msglbl.setText("Welcome to myRadio")
def metaDataChanged(self):
if self.player.isMetaDataAvailable():
trackInfo = (self.player.metaData("Title"))
description = (self.player.metaData("Description"))
comment = (self.player.metaData("Comment"))
if trackInfo is None:
self.msglbl.setText(f"playing {self.current_station_name}")
new_trackInfo = ""
new_trackInfo = str(trackInfo)
if len(new_trackInfo) > 200:
new_trackInfo = str(new_trackInfo).partition('{"title":"')[2].partition('","')[0].replace('\n', " ")[:200]
if not new_trackInfo == "None":
self.msglbl.setText(new_trackInfo)
self.msglbl.adjustSize()
self.adjustSize()
else:
self.msglbl.setText(f"playing {self.current_station_name}")
mt = (f"Title:{new_trackInfo}\ndescription:{description}\ncomment:: {comment}")
if description == None:
mt = (f"Title:{new_trackInfo}\ncomment: {comment}")
if comment == None:
mt = (f"Title:{new_trackInfo}\ncomment: {description}")
if description == None and comment == None:
mt = (f"{new_trackInfo}")
if not mt == "None":
if self.notificationsEnabled:
if not mt == self.old_meta:
print(mt)
self.showTrayMessage("myRadio", mt, self.tIcon)
self.old_meta = mt
self.trayIcon.setToolTip(mt)
else:
self.trayIcon.setToolTip(mt)
self.old_meta = mt
else:
self.msglbl.setText(f"playing {self.current_station_name}")
def playRadioStation(self):
if self.player.state() == 0:
self.player.setMedia(QMediaContent(QUrl(self.current_station_url)))
self.player.play()
self.togglePlayerAction.setText("Stop playback")
self.togglePlayerAction.setIcon(QIcon.fromTheme("media-playback-stop"))
self.set_running_player()
if not self.current_station_url:
return
self.player.setMedia(QMediaContent(QUrl(self.current_station_url)))
self.set_running_player()
self.player.play()
if self.is_recording:
self.recordAction.setText(f"stop recording of {self.rec_name}")
self.recordAction.setIcon(QIcon.fromTheme("media-playback-stop"))
else:
self.recordAction.setText(f"start recording of {self.current_station_name}")
self.recordAction.setIcon(QIcon.fromTheme("media-record"))
self.msglbl.setText(f"playing {self.current_station_name}")
self.setWindowTitle(self.current_station_name)
def playURL(self):
clip = QApplication.clipboard()
if not clip.text().endswith(".pls") and not clip.text().endswith(".m3u"):
self.current_station_url = clip.text()
elif clip.text().endswith(".pls") :
print("is a pls")
url = self.getURLfromPLS(clip.text())
self.current_station_url = url
elif clip.text().endswith(".m3u") or clip.text().endswith(".m3u8"):
print("is a m3u")
url = self.getURLfromM3U(clip.text())
self.current_station_url = url
print(self.current_station_url)
if self.player.state() == 0:
self.set_running_player()
self.player.start()
return
if not self.current_station_url:
return
self.player.set_media(self.current_station_url)
self.set_running_player()
self.player.start()
self.msglbl.setText(f"playing {self.current_station_name}")
self.metaDataChanged()
def set_running_player(self):
self.play_btn.setEnabled(False)
self.stop_btn.setEnabled(True)
self.rec_btn.setEnabled(True)
def stop_preview(self):
self.player.stop()
self.play_btn.setEnabled(True)
self.stop_btn.setEnabled(False)
self.rec_btn.setEnabled(False)
self.msglbl.setText("stopped")
self.togglePlayerAction.setText("Start playback")
self.togglePlayerAction.setIcon(QIcon.fromTheme("media-playback-start"))
def set_sound_level(self, level):
self.player.set_sound_level(level)
self.level_lbl.setText("Volume " + str(level))
self.player.setVolume(level)
def update_volume_slider(self, level):
self.level_lbl.setText("Volume " + str(level))
self.level_sld.blockSignals(True)
self.level_sld.setValue(level)
self.level_lbl.setText("Volume " + str(level))
self.level_sld.blockSignals(False)
def recordRadio1(self):
if not self.is_recording:
self.deleteOutFile()
self.rec_url = self.current_station_url
self.rec_name = self.current_station_name
cmd = ("wget -q " + self.rec_url + " -O " + self.outfile)
print(cmd)
self.is_recording = True
self.process.startDetached(cmd)
self.recordAction.setText(f"stop recording of {self.rec_name}")
self.recordAction.setIcon(QIcon.fromTheme("media-playback-stop"))
self.rec_btn.setVisible(False)
self.stoprec_btn.setVisible(True)
else:
self.stop_recording()
def stop_recording(self):
if self.is_recording:
self.process.close()
print("stoppe Aufnahme")
self.is_recording = False
QProcess.execute("killall wget")
self.saveMovie()
self.stoprec_btn.setVisible(False)
self.rec_btn.setVisible(True)
self.recordAction.setText(f"start recording of {self.current_station_name}")
self.recordAction.setIcon(QIcon.fromTheme("media-record"))
else:
self.showTrayMessage("Note", "no recording started", self.tIcon)
def saveMovie(self):
if not self.is_recording:
print("Save recording")
infile = QFile(self.outfile)
savefile, _ = QFileDialog.getSaveFileName(None, "Save as...",
QDir.homePath() + "/Music/" + self.rec_name
.replace("-", " ").replace(" - ", " ") + ".mp3", "Audio (*.mp3)")
if (savefile != ""):
if QFile(savefile).exists:
QFile(savefile).remove()
print("%s %s" % ("saving", savefile))
if not infile.copy(savefile):
QMessageBox.warning(self, "Error",
"File %s:\n%s." % (savefile, infile.errorString()))
print("%s %s" % ("Process-State: ", str(self.process.state())))
if QFile(self.outfile).exists:
print("%s %s" % ("deleting file", self.outfile))
QFile(self.outfile).remove()
def deleteOutFile(self):
if QFile(self.outfile).exists:
print("%s %s" % ("deleting file", self.outfile))
QFile(self.outfile).remove()
print("%s %s" % (self.outfile, "removed"))
def getPID(self):
print("%s %s" % (self.process.pid(), self.process.processId()))
class RadioPlayer(QMediaPlayer):
def __init__(self, driver):
super(RadioPlayer, self).__init__()
self.driver = driver
self.url = None
self.auto_sound_level = True
self.is_running = False
self.volumeChanged.connect(self.on_volume_changed)
self.stateChanged.connect(self.on_state_changed)
def set_media(self, media):
if isinstance(media, QUrl):
self.url = media
elif isinstance(media, str):
self.url = QUrl(media)
self.setMedia(QMediaContent(self.url))
def set_sound_level(self, level):
self.auto_sound_level = False
self.setVolume(level)
def on_volume_changed(self, value):
if self.auto_sound_level:
self.update_volume_slider(value)
self.auto_sound_level = True
def on_state_changed(self, state):
if not state:
self.driver.stop_preview()
################################################
BASE_URL = "https://de1.api.radio-browser.info/"
endpoints = {
"countries": {1: "{fmt}/countries", 2: "{fmt}/countries/{filter}"},
"codecs": {1: "{fmt}/codecs", 2: "{fmt}/codecs/{filter}"},
"states": {
1: "{fmt}/states",
2: "{fmt}/states/{filter}",
3: "{fmt}/states/{country}/{filter}",
},
"languages": {1: "{fmt}/languages", 2: "{fmt}/languages/{filter}"},
"tags": {1: "{fmt}/tags", 2: "{fmt}/tags/{filter}"},
"stations": {1: "{fmt}/stations", 3: "{fmt}/stations/{by}/{search_term}"},
"playable_station": {3: "{ver}/{fmt}/url/{station_id}"},
"station_search": {1: "{fmt}/stations/search"},
}
def request(endpoint, **kwargs):
fmt = kwargs.get("format", "json")
if fmt == "xml":
content_type = f"application/{fmt}"
else:
content_type = f"application/{fmt}"
headers = {"content-type": content_type, "User-Agent": "getRadiolist/1.0"}
params = kwargs.get("params", {})
url = BASE_URL + endpoint
resp = requests.get(url, headers=headers, params=params)
if resp.status_code == 200:
if fmt == "xml":
return resp.text
return resp.json()
return resp.raise_for_status()
genres = """Classic Rock
Acoustic
Bluegrass
Country
Folk
Folk Rock
Grunge
Hard Rock
Blues
Oldies
60's
70's
80's
90's
Nachrichten
Swing
Pop
Rock
Classic
Beat
Metal
Techno
Disco
Schlager
Hits
Hip Hop"""
class EndPointBuilder:
def __init__(self, fmt="json"):
self.fmt = fmt
self._option = None
self._endpoint = None
@property
def endpoint(self):
return endpoints[self._endpoint][self._option]
def produce_endpoint(self, **parts):
self._option = len(parts)
self._endpoint = parts["endpoint"]
parts.update({"fmt": self.fmt})
return self.endpoint.format(**parts)
class RadioBrowser:
def __init__(self, fmt="json"):
self.fmt = fmt
self.builder = EndPointBuilder(fmt=self.fmt)
def countries(self, filter=""):
endpoint = self.builder.produce_endpoint(endpoint="countries")
return request(endpoint)
def codecs(self, filter=""):
endpoint = self.builder.produce_endpoint(endpoint="codecs")
return request(endpoint)
def states(self, country="", filter=""):
endpoint = self.builder.produce_endpoint(
endpoint="states", country=country, filter=filter
)
return request(endpoint)
def languages(self, filter=""):
endpoint = self.builder.produce_endpoint(endpoint="languages", filter=filter)
return request(endpoint)
def tags(self, filter=""):
endpoint = self.builder.produce_endpoint(endpoint="tags", filter=filter)
return request(endpoint)
def stations(self, **params):
endpoint = self.builder.produce_endpoint(endpoint="stations")
kwargs = {}
if params:
kwargs.update({"params": params})
return request(endpoint, **kwargs)
def stations_byid(self, id):
endpoint = self.builder.produce_endpoint(
endpoint="stations", by="byid", search_term=id
)
return request(endpoint)
def stations_byuuid(self, uuid):
endpoint = self.builder.produce_endpoint(
endpoint="stations", by="byuuid", search_term=uuid
)
return request(endpoint)
def stations_byname(self, name):
endpoint = self.builder.produce_endpoint(
endpoint="stations", by="byname", search_term=name
)
return request(endpoint)
def stations_bynameexact(self, nameexact):
endpoint = self.builder.produce_endpoint(
endpoint="stations", by="bynameexact", search_term=nameexact
)
return request(endpoint)
def stations_bycodec(self, codec):
endpoint = self.builder.produce_endpoint(
endpoint="stations", by="bycodec", search_term=codec
)
return request(endpoint)
def stations_bycodecexact(self, codecexact):
endpoint = self.builder.produce_endpoint(
endpoint="stations", by="bycodecexact", search_term=codecexact
)
return request(endpoint)
def stations_bycountry(self, country):
endpoint = self.builder.produce_endpoint(
endpoint="stations", by="bycountry", search_term=country
)
return request(endpoint)
def stations_bycountryexact(self, countryexact):
endpoint = self.builder.produce_endpoint(
endpoint="stations", by="bycountryexact", search_term=countryexact
)
return request(endpoint)
def stations_bystate(self, state):
endpoint = self.builder.produce_endpoint(
endpoint="stations", by="bystate", search_term=state
)
return request(endpoint)
def stations_bystateexact(self, stateexact):
endpoint = self.builder.produce_endpoint(
endpoint="stations", by="bystateexact", search_term=stateexact
)
return request(endpoint)
#
def stations_bylanguage(self, language):
endpoint = self.builder.produce_endpoint(
endpoint="stations", by="bylanguage", search_term=language
)