From 0b636215322c90b771f9f9895a68faaeae7f952c Mon Sep 17 00:00:00 2001 From: Wiktor Zykubek Date: Sun, 18 Jun 2023 16:32:57 +0200 Subject: [PATCH] Adjust and rename getStationsData method to handle other URLs --- openfm_qt/__init__.py | 1 - openfm_qt/mainwindow.py | 18 ++++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/openfm_qt/__init__.py b/openfm_qt/__init__.py index a39f6ab..00e7aa1 100644 --- a/openfm_qt/__init__.py +++ b/openfm_qt/__init__.py @@ -1,6 +1,5 @@ # This Python file uses the following encoding: utf-8 -API_URL = "https://open.fm/radio/api/v2/ofm/stations_slug.json" DEFAULT_VOLUME = 70 from .mainwindow import MainWindow diff --git a/openfm_qt/mainwindow.py b/openfm_qt/mainwindow.py index 8f164ae..30b0896 100644 --- a/openfm_qt/mainwindow.py +++ b/openfm_qt/mainwindow.py @@ -11,7 +11,7 @@ from PySide6.QtMultimedia import QMediaPlayer, QAudioOutput from ui_form import Ui_MainWindow import json import requests -from . import API_URL, DEFAULT_VOLUME +from . import DEFAULT_VOLUME class MainWindow(QMainWindow): @@ -27,7 +27,9 @@ class MainWindow(QMainWindow): self.ui.volumeToolButton.setIcon(volume_icon) self.ui.playbackToolButton.setIcon(playback_icon) - self.__stations_data = self.getStationsData() + self.__stations = self.getData( + "https://open.fm/radio/api/v2/ofm/stations_slug.json" + ) self.__player = QMediaPlayer() self.__audio = QAudioOutput() self.__player.setAudioOutput(self.__audio) @@ -40,9 +42,9 @@ class MainWindow(QMainWindow): self.ui.volumeHorizontalSlider.valueChanged.connect(self.setVolume) self.ui.volumeToolButton.clicked.connect(self.toggleMute) - def getStationsData(self) -> dict: + def getData(self, url) -> dict: """Get JSON data from API and convert it to dict.""" - resp = requests.get(API_URL) + resp = requests.get(url) if resp.status_code not in range(200, 299 + 1): error_box = QMessageBox.critical( self, @@ -57,14 +59,14 @@ class MainWindow(QMainWindow): def printGroups(self) -> None: """Print groups (categories) in radioGroupsListWidget.""" self.ui.radioGroupsListWidget.addItems( - [e["name"] for e in self.__stations_data["groups"]] + [e["name"] for e in self.__stations["groups"]] ) def printStations(self) -> None: """Print stations (channels) in stationsListWidget.""" group = self.ui.radioGroupsListWidget.selectedItems()[0].text() group_id = None - for e in self.__stations_data["groups"]: + for e in self.__stations["groups"]: if e["name"] == group: group_id = e["id"] @@ -72,7 +74,7 @@ class MainWindow(QMainWindow): self.ui.stationsListWidget.addItems( [ e["name"] - for e in self.__stations_data["channels"] + for e in self.__stations["channels"] if e["group_id"] == group_id ] ) @@ -100,7 +102,7 @@ class MainWindow(QMainWindow): """Play station selected by user.""" station = self.ui.stationsListWidget.selectedItems()[0].text() stream_url = None - for e in self.__stations_data["channels"]: + for e in self.__stations["channels"]: if e["name"] == station: stream_url = f"http://stream.open.fm/{e['id']}"