Refactorize code, unify class names, add documentation
This commit is contained in:
parent
6a99d4198c
commit
c2561e87ba
13
form.ui
13
form.ui
@ -31,6 +31,13 @@
|
|||||||
<property name="windowTitle">
|
<property name="windowTitle">
|
||||||
<string>Open FM</string>
|
<string>Open FM</string>
|
||||||
</property>
|
</property>
|
||||||
|
<property name="windowIcon">
|
||||||
|
<iconset>
|
||||||
|
<normaloff>logo.png</normaloff>logo.png</iconset>
|
||||||
|
</property>
|
||||||
|
<property name="locale">
|
||||||
|
<locale language="Polish" country="Poland"/>
|
||||||
|
</property>
|
||||||
<widget class="QWidget" name="centralwidget">
|
<widget class="QWidget" name="centralwidget">
|
||||||
<widget class="QWidget" name="verticalLayoutWidget">
|
<widget class="QWidget" name="verticalLayoutWidget">
|
||||||
<property name="geometry">
|
<property name="geometry">
|
||||||
@ -45,7 +52,7 @@
|
|||||||
<item>
|
<item>
|
||||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||||
<item>
|
<item>
|
||||||
<widget class="QListWidget" name="groupslistWidget">
|
<widget class="QListWidget" name="groupsListWidget">
|
||||||
<property name="minimumSize">
|
<property name="minimumSize">
|
||||||
<size>
|
<size>
|
||||||
<width>130</width>
|
<width>130</width>
|
||||||
@ -61,14 +68,14 @@
|
|||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QListWidget" name="stationslistWidget"/>
|
<widget class="QListWidget" name="stationsListWidget"/>
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
<layout class="QHBoxLayout" name="horizontalLayout_2">
|
||||||
<item>
|
<item>
|
||||||
<widget class="QToolButton" name="toolButton">
|
<widget class="QToolButton" name="playbackToolButton">
|
||||||
<property name="text">
|
<property name="text">
|
||||||
<string>...</string>
|
<string>...</string>
|
||||||
</property>
|
</property>
|
||||||
|
118
mainwindow.py
118
mainwindow.py
@ -14,85 +14,115 @@ from ui_form import Ui_MainWindow
|
|||||||
import json
|
import json
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
API_URL = "https://open.fm/radio/api/v2/ofm/stations_slug.json"
|
||||||
|
DEFAULT_VOLUME = 70
|
||||||
|
|
||||||
|
|
||||||
class MainWindow(QMainWindow):
|
class MainWindow(QMainWindow):
|
||||||
|
"""MainWindow Class."""
|
||||||
|
|
||||||
def __init__(self, parent=None):
|
def __init__(self, parent=None):
|
||||||
|
"""Initialize UI, audio player and handlers."""
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
self.ui = Ui_MainWindow()
|
self.ui = Ui_MainWindow()
|
||||||
self.ui.setupUi(self)
|
self.ui.setupUi(self)
|
||||||
self.stations_slug = self.getData()
|
self.__stations_data = self.getStationsData()
|
||||||
self.__player = QMediaPlayer()
|
self.__player = QMediaPlayer()
|
||||||
self.__audio = QAudioOutput()
|
self.__audio = QAudioOutput()
|
||||||
self.__player.setAudioOutput(self.__audio)
|
self.__player.setAudioOutput(self.__audio)
|
||||||
self.setVolume()
|
self.setVolume(DEFAULT_VOLUME)
|
||||||
self.getGroups()
|
self.printGroups()
|
||||||
self.ui.groupslistWidget.itemClicked.connect(self.getStations)
|
self.ui.groupsListWidget.itemClicked.connect(self.printStations)
|
||||||
self.ui.stationslistWidget.itemClicked.connect(self.playRadio)
|
self.ui.stationsListWidget.itemClicked.connect(self.playRadio)
|
||||||
self.ui.toolButton.clicked.connect(self.togglePlayer)
|
self.ui.playbackToolButton.clicked.connect(self.togglePlayer)
|
||||||
self.ui.volumeHorizontalSlider.valueChanged.connect(self.setVolume)
|
self.ui.volumeHorizontalSlider.valueChanged.connect(self.setVolume)
|
||||||
self.ui.volumeToolButton.clicked.connect(self.toggleMute)
|
self.ui.volumeToolButton.clicked.connect(self.toggleMute)
|
||||||
|
|
||||||
def getGroups(self):
|
def getStationsData(self) -> dict:
|
||||||
for el in self.stations_slug["groups"]:
|
"""Get JSON data from API and convert it to dict."""
|
||||||
self.ui.groupslistWidget.addItem(el["name"])
|
resp = requests.get(API_URL)
|
||||||
|
|
||||||
def getStations(self):
|
|
||||||
group = self.ui.groupslistWidget.selectedItems()[0].text()
|
|
||||||
group_id = None
|
|
||||||
for i in self.stations_slug["groups"]:
|
|
||||||
if i["name"] == group:
|
|
||||||
group_id = i["id"]
|
|
||||||
|
|
||||||
self.ui.stationslistWidget.clear()
|
|
||||||
for ch in self.stations_slug["channels"]:
|
|
||||||
if ch["group_id"] == group_id:
|
|
||||||
self.ui.stationslistWidget.addItem(ch["name"])
|
|
||||||
|
|
||||||
def getData(self) -> dict:
|
|
||||||
resp = requests.get("https://open.fm/radio/api/v2/ofm/stations_slug.json")
|
|
||||||
if resp.status_code not in range(200, 299 + 1):
|
if resp.status_code not in range(200, 299 + 1):
|
||||||
error_box = QMessageBox.critical(
|
error_box = QMessageBox.critical(
|
||||||
self, "Błąd",
|
self,
|
||||||
|
"Błąd",
|
||||||
f"Błąd połączenia o kodzie: {resp.status_code}",
|
f"Błąd połączenia o kodzie: {resp.status_code}",
|
||||||
QMessageBox.Cancel
|
QMessageBox.Cancel,
|
||||||
)
|
)
|
||||||
error_box.exec()
|
error_box.exec()
|
||||||
else:
|
else:
|
||||||
return json.loads(resp.text)
|
return json.loads(resp.text)
|
||||||
|
|
||||||
def setVolume(self, volume: int = None):
|
def printGroups(self) -> None:
|
||||||
if volume is None:
|
"""Print groups (categories) in groupsListWidget."""
|
||||||
|
self.ui.groupsListWidget.addItems(
|
||||||
|
[e["name"] for e in self.__stations_data["groups"]]
|
||||||
|
)
|
||||||
|
|
||||||
|
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()
|
||||||
|
self.ui.stationsListWidget.addItems(
|
||||||
|
[
|
||||||
|
e["name"]
|
||||||
|
for e in self.__stations_data["channels"]
|
||||||
|
if e["group_id"] == group_id
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
def setVolume(self, volume: int = None) -> None:
|
||||||
|
"""Set playback volume to given number or slider value."""
|
||||||
|
if not volume:
|
||||||
volume = self.ui.volumeHorizontalSlider.value()
|
volume = self.ui.volumeHorizontalSlider.value()
|
||||||
self.__audio.setVolume(volume / 100)
|
self.__audio.setVolume(volume / 100)
|
||||||
|
|
||||||
def toggleMute(self):
|
def toggleMute(self) -> None:
|
||||||
|
"""Toggle playback volume between 0 and DEFAULT_VOLUME."""
|
||||||
if self.ui.volumeHorizontalSlider.value() == 0:
|
if self.ui.volumeHorizontalSlider.value() == 0:
|
||||||
self.ui.volumeHorizontalSlider.setValue(70)
|
self.ui.volumeHorizontalSlider.setValue(DEFAULT_VOLUME)
|
||||||
self.ui.volumeToolButton.setIcon(QIcon.fromTheme("audio-volume-medium"))
|
self.ui.volumeToolButton.setIcon(
|
||||||
self.setVolume(70)
|
QIcon.fromTheme("audio-volume-medium")
|
||||||
|
)
|
||||||
|
self.setVolume(DEFAULT_VOLUME)
|
||||||
else:
|
else:
|
||||||
self.ui.volumeHorizontalSlider.setValue(0)
|
self.ui.volumeHorizontalSlider.setValue(0)
|
||||||
self.ui.volumeToolButton.setIcon(QIcon.fromTheme("audio-volume-muted"))
|
self.ui.volumeToolButton.setIcon(
|
||||||
|
QIcon.fromTheme("audio-volume-muted")
|
||||||
|
)
|
||||||
self.setVolume(0)
|
self.setVolume(0)
|
||||||
|
|
||||||
def playRadio(self):
|
def playRadio(self) -> None:
|
||||||
station = self.ui.stationslistWidget.selectedItems()[0].text()
|
"""Play station selected by user."""
|
||||||
|
station = self.ui.stationsListWidget.selectedItems()[0].text()
|
||||||
stream_url = None
|
stream_url = None
|
||||||
for ch in self.stations_slug["channels"]:
|
for e in self.__stations_data["channels"]:
|
||||||
if ch["name"] == station:
|
if e["name"] == station:
|
||||||
stream_url = f"http://stream.open.fm/{ch['id']}"
|
stream_url = f"http://stream.open.fm/{e['id']}"
|
||||||
|
|
||||||
self.__player.setSource(QUrl(stream_url))
|
self.__player.setSource(QUrl(stream_url))
|
||||||
self.ui.toolButton.setIcon(QIcon.fromTheme("media-playback-start"))
|
self.ui.playbackToolButton.setIcon(
|
||||||
|
QIcon.fromTheme("media-playback-start")
|
||||||
|
)
|
||||||
self.togglePlayer()
|
self.togglePlayer()
|
||||||
|
|
||||||
def togglePlayer(self):
|
def togglePlayer(self) -> None:
|
||||||
if self.__player.playbackState() == QMediaPlayer.PlaybackState.PlayingState:
|
"""Toggle playback (play/stop)."""
|
||||||
|
pb_state = QMediaPlayer.PlaybackState
|
||||||
|
if self.__player.playbackState() == pb_state.PlayingState:
|
||||||
self.__player.stop()
|
self.__player.stop()
|
||||||
self.ui.toolButton.setIcon(QIcon.fromTheme("media-playback-start"))
|
self.ui.playbackToolButton.setIcon(
|
||||||
elif self.__player.playbackState() == QMediaPlayer.PlaybackState.StoppedState:
|
QIcon.fromTheme("media-playback-start")
|
||||||
|
)
|
||||||
|
elif self.__player.playbackState() == pb_state.StoppedState:
|
||||||
self.__player.play()
|
self.__player.play()
|
||||||
self.ui.toolButton.setIcon(QIcon.fromTheme("media-playback-stop"))
|
self.ui.playbackToolButton.setIcon(
|
||||||
|
QIcon.fromTheme("media-playback-stop")
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -1,3 +1,3 @@
|
|||||||
{
|
{
|
||||||
"files": ["mainwindow.py", "form.ui"]
|
"files": ["mainwindow.py","form.ui"]
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user