2023-07-10 17:04:47 +02:00
|
|
|
# coding=utf-8
|
2023-06-11 22:50:01 +02:00
|
|
|
|
|
|
|
import json
|
2023-07-11 15:29:37 +02:00
|
|
|
|
2023-06-11 22:50:01 +02:00
|
|
|
import requests
|
2023-07-11 15:29:37 +02:00
|
|
|
from PySide6.QtCore import QUrl
|
|
|
|
from PySide6.QtMultimedia import QAudioOutput, QMediaPlayer
|
|
|
|
from PySide6.QtWidgets import QMainWindow, QMessageBox, QStyle
|
|
|
|
|
2023-07-08 20:20:56 +02:00
|
|
|
from . import API_URL, DEFAULT_VOLUME, Ui_MainWindow
|
2023-06-15 00:12:32 +02:00
|
|
|
|
2023-06-11 22:50:01 +02:00
|
|
|
|
|
|
|
class MainWindow(QMainWindow):
|
2023-06-15 00:12:32 +02:00
|
|
|
"""MainWindow Class."""
|
|
|
|
|
2023-06-11 22:50:01 +02:00
|
|
|
def __init__(self, parent=None):
|
2023-06-15 00:12:32 +02:00
|
|
|
"""Initialize UI, audio player and handlers."""
|
2023-06-11 22:50:01 +02:00
|
|
|
super().__init__(parent)
|
|
|
|
self.ui = Ui_MainWindow()
|
|
|
|
self.ui.setupUi(self)
|
2023-06-15 00:32:41 +02:00
|
|
|
volume_icon = self.style().standardIcon(QStyle.SP_MediaVolume)
|
|
|
|
playback_icon = self.style().standardIcon(QStyle.SP_MediaPlay)
|
|
|
|
self.ui.volumeToolButton.setIcon(volume_icon)
|
|
|
|
self.ui.playbackToolButton.setIcon(playback_icon)
|
|
|
|
|
2023-06-15 00:12:32 +02:00
|
|
|
self.__stations_data = self.getStationsData()
|
2023-06-12 23:16:24 +02:00
|
|
|
self.__player = QMediaPlayer()
|
|
|
|
self.__audio = QAudioOutput()
|
|
|
|
self.__player.setAudioOutput(self.__audio)
|
2023-06-15 00:12:32 +02:00
|
|
|
self.printGroups()
|
2023-06-15 00:32:41 +02:00
|
|
|
|
2023-07-11 16:24:53 +02:00
|
|
|
self.setVolume(DEFAULT_VOLUME)
|
|
|
|
self.ui.volumeHorizontalSlider.setValue(DEFAULT_VOLUME)
|
|
|
|
|
2023-06-15 00:12:32 +02:00
|
|
|
self.ui.groupsListWidget.itemClicked.connect(self.printStations)
|
|
|
|
self.ui.stationsListWidget.itemClicked.connect(self.playRadio)
|
|
|
|
self.ui.playbackToolButton.clicked.connect(self.togglePlayer)
|
2023-06-14 16:22:48 +02:00
|
|
|
self.ui.volumeHorizontalSlider.valueChanged.connect(self.setVolume)
|
|
|
|
self.ui.volumeToolButton.clicked.connect(self.toggleMute)
|
2023-06-11 22:50:01 +02:00
|
|
|
|
2023-06-15 00:12:32 +02:00
|
|
|
def getStationsData(self) -> dict:
|
|
|
|
"""Get JSON data from API and convert it to dict."""
|
2023-07-11 18:27:45 +02:00
|
|
|
try:
|
|
|
|
resp = requests.get(API_URL)
|
|
|
|
if resp.status_code not in range(200, 299 + 1):
|
|
|
|
raise requests.exceptions.ConnectionError()
|
|
|
|
else:
|
|
|
|
return json.loads(resp.text)
|
|
|
|
except requests.exceptions.ConnectionError:
|
2023-06-14 15:57:46 +02:00
|
|
|
error_box = QMessageBox.critical(
|
2023-06-15 00:12:32 +02:00
|
|
|
self,
|
|
|
|
"Błąd",
|
2023-07-11 18:27:45 +02:00
|
|
|
"Brak połączenia z serwerem.",
|
2023-06-15 00:12:32 +02:00
|
|
|
QMessageBox.Cancel,
|
2023-06-14 15:57:46 +02:00
|
|
|
)
|
|
|
|
error_box.exec()
|
|
|
|
|
2023-06-15 00:12:32 +02:00
|
|
|
def printGroups(self) -> None:
|
|
|
|
"""Print groups (categories) in groupsListWidget."""
|
|
|
|
self.ui.groupsListWidget.addItems(
|
2023-07-10 17:04:47 +02:00
|
|
|
[e["name"] for e in self.__stations_data["groups"]])
|
2023-06-15 00:12:32 +02:00
|
|
|
|
|
|
|
def printStations(self) -> None:
|
|
|
|
"""Print stations (channels) in stationsListWidget."""
|
|
|
|
group = self.ui.groupsListWidget.selectedItems()[0].text()
|
|
|
|
group_id = None
|
|
|
|
for e in self.__stations_data["groups"]:
|
|
|
|
if e["name"] == group:
|
|
|
|
group_id = e["id"]
|
|
|
|
|
|
|
|
self.ui.stationsListWidget.clear()
|
2023-07-10 17:04:47 +02:00
|
|
|
self.ui.stationsListWidget.addItems([
|
|
|
|
e["name"] for e in self.__stations_data["channels"]
|
|
|
|
if e["group_id"] == group_id
|
|
|
|
])
|
2023-06-15 00:12:32 +02:00
|
|
|
|
|
|
|
def setVolume(self, volume: int = None) -> None:
|
|
|
|
"""Set playback volume to given number or slider value."""
|
|
|
|
if not volume:
|
2023-06-14 16:22:48 +02:00
|
|
|
volume = self.ui.volumeHorizontalSlider.value()
|
|
|
|
self.__audio.setVolume(volume / 100)
|
|
|
|
|
2023-06-15 00:12:32 +02:00
|
|
|
def toggleMute(self) -> None:
|
|
|
|
"""Toggle playback volume between 0 and DEFAULT_VOLUME."""
|
2023-06-14 16:22:48 +02:00
|
|
|
if self.ui.volumeHorizontalSlider.value() == 0:
|
2023-07-11 17:53:51 +02:00
|
|
|
self.ui.volumeHorizontalSlider.setValue(self.previous_volume)
|
2023-06-15 00:32:41 +02:00
|
|
|
icon = self.style().standardIcon(QStyle.SP_MediaVolume)
|
2023-07-11 17:53:51 +02:00
|
|
|
self.setVolume(self.previous_volume)
|
2023-06-14 16:22:48 +02:00
|
|
|
else:
|
2023-07-11 17:53:51 +02:00
|
|
|
self.previous_volume = self.__audio.volume() * 100
|
2023-06-14 16:22:48 +02:00
|
|
|
self.ui.volumeHorizontalSlider.setValue(0)
|
2023-06-15 00:32:41 +02:00
|
|
|
icon = self.style().standardIcon(QStyle.SP_MediaVolumeMuted)
|
2023-06-14 16:22:48 +02:00
|
|
|
self.setVolume(0)
|
|
|
|
|
2023-06-15 00:32:41 +02:00
|
|
|
self.ui.volumeToolButton.setIcon(icon)
|
|
|
|
|
2023-06-15 00:12:32 +02:00
|
|
|
def playRadio(self) -> None:
|
|
|
|
"""Play station selected by user."""
|
|
|
|
station = self.ui.stationsListWidget.selectedItems()[0].text()
|
2023-06-12 23:16:24 +02:00
|
|
|
stream_url = None
|
2023-06-15 00:12:32 +02:00
|
|
|
for e in self.__stations_data["channels"]:
|
|
|
|
if e["name"] == station:
|
|
|
|
stream_url = f"http://stream.open.fm/{e['id']}"
|
2023-06-12 23:16:24 +02:00
|
|
|
|
|
|
|
self.__player.setSource(QUrl(stream_url))
|
2023-07-11 18:38:44 +02:00
|
|
|
self.setWindowTitle(f"Open FM - {station}")
|
2023-06-15 12:12:17 +02:00
|
|
|
|
|
|
|
# Required to avoid crashing. For some reason if you want to change
|
|
|
|
# the station for the first time, you need to stop and resume playback.
|
|
|
|
# If you won't, application would crash.
|
|
|
|
for _ in range(3):
|
|
|
|
self.togglePlayer()
|
2023-06-13 00:06:24 +02:00
|
|
|
|
2023-06-15 00:12:32 +02:00
|
|
|
def togglePlayer(self) -> None:
|
|
|
|
"""Toggle playback (play/stop)."""
|
|
|
|
pb_state = QMediaPlayer.PlaybackState
|
|
|
|
if self.__player.playbackState() == pb_state.PlayingState:
|
2023-06-13 00:06:24 +02:00
|
|
|
self.__player.stop()
|
2023-06-15 00:32:41 +02:00
|
|
|
icon = self.style().standardIcon(QStyle.SP_MediaPlay)
|
2023-06-15 00:12:32 +02:00
|
|
|
elif self.__player.playbackState() == pb_state.StoppedState:
|
2023-06-13 00:06:24 +02:00
|
|
|
self.__player.play()
|
2023-06-15 00:32:41 +02:00
|
|
|
icon = self.style().standardIcon(QStyle.SP_MediaStop)
|
2023-06-13 00:06:24 +02:00
|
|
|
else:
|
|
|
|
pass
|
2023-06-12 23:16:24 +02:00
|
|
|
|
2023-06-15 00:32:41 +02:00
|
|
|
self.ui.playbackToolButton.setIcon(icon)
|